0% found this document useful (0 votes)
8 views

C++ Lec9-If, Else Conditions

cpp

Uploaded by

itzzmehama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

C++ Lec9-If, Else Conditions

cpp

Uploaded by

itzzmehama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1

C++ If ... Else

C++ Conditions and If Statements

C++ supports the usual logical conditions from mathematics:

 Less than: a < b


 Less than or equal to: a <= b
 Greater than: a > b
 Greater than or equal to: a >= b
 Equal to a == b
 Not Equal to: a != b

You can use these conditions to perform different actions for different decisions.

C++ has the following conditional statements:

 Use if to specify a block of code to be executed, if a specified condition is true


 Use else to specify a block of code to be executed, if the same condition is false
 Use else if to specify a new condition to test, if the first condition is false
 Use switch to specify many alternative blocks of code to be executed

Programming C++ Samir Bilal Practical & Theoretical


2

The if Statement

Use the if statement to specify a block of C++ code to be executed if a condition is true.

Syntax

if (condition) {
// block of code to be executed if the condition is true
}

Note that if is in lowercase letters. Uppercase letters (If or IF) will generate an error.

In the example below, we test two values to find out if 20 is greater than 18. If the
condition is true, print some text:

Example

#include <iostream>

using namespace std;

int main() {

if (20 > 18) {

cout << "20 is greater than 18";

return 0;

We can also test variables:

Programming C++ Samir Bilal Practical & Theoretical


3

Example

#include <iostream>

using namespace std;

int main() {

int x = 20;

int y = 18;

if (x > y) {

cout << "x is greater than y";

return 0;

Example explained

In the example above we use two variables, x and y, to test whether x is greater than y (using the
> operator). As x is 20, and y is 18, and we know that 20 is greater than 18, we print to the screen
that "x is greater than y".

Programming C++ Samir Bilal Practical & Theoretical


4

C++ Else

The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax

if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}

Example

#include <iostream> Example explained


using namespace std;
In the example above, time (20) is
int main() { greater than 18, so the condition is
int time = 20; false. Because of this, we move on

if (time < 18) { to the else condition and print to


the screen "Good evening". If the
cout << "Good day.";
time was less than 18, the program
} else {
would print "Good day".
cout << "Good evening."; // Outputs "Good evening."

return 0; }
Programming C++ Samir Bilal Practical & Theoretical
5

C++ Else If

The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax

if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}

Example

#include <iostream>
using namespace std;
int main() {
int time = 22;
if (time < 10) {
cout << "Good morning.";
} else if (time < 20) {
cout << "Good day.";
} else {
cout << "Good evening."; // Outputs "Good evening."
}
return 0;
}
Programming C++ Samir Bilal Practical & Theoretical
6

Q1) C++ supports the usual logical conditions from mathematics what are them?

Q2) what is the syntax of if?

Q3) what is the syntax of if….else?

Q4) what is the syntax of else if?

Some Exercises:
1) Write a C++ program to read two numbers then print the minimum number between
both (if both numbers are equal print they are equals).

2) Write a C++ program to read a student subject mark, if the mark is greater or equals
to 50 print (Pass) else if it is smaller than 50 print (Fail).

3) Write a C++ program to read the number of work hours if it is less than or equals to 50
, the employee will not get to part time salary, if it is greater than 50 the employee will
get to 20$ as apart time work, the salary of an hour = 5$.

4) Write a C++ program to enter two numbers then test them if they are equals or not.

5) Write a C++ program to enter a number and test it then print (positive) if it is positive
else print (negative).

6) Write a C++ program to read a number then test it, if the number is even print (Even),
else if it is odd print (Odd).

7) Write a C++ program to test and print the following:

 Positive Even Number


 Negative Even Number
 Positive Odd Number
 Negative Odd Number

Programming C++ Samir Bilal Practical & Theoretical

You might also like