The if statement is used when the program is made to choose one statement.
single statement
block of statements
The general form of if statement is:
if (conditional expression)
statement;
where:
The if statement is used when the program is made to choose one statement only.
conditional expression is boolean expression that evaluates to a TRUE or FALSE value
statement(s) any single C++ statement
Consider the given program statements below:
x = 5;
if (x >= 5)
cout << "hello ";
cout << "good day";
OUTPUT:
hello good day
x = 4;
if (x >= 5)
cout << "hello ";
cout << "good day";
OUTPUT:
good day
x = 5;
if (x >= 5)
x *= 2;
x += 2;
cout << x;
OUTPUT:
12
x = 4;
if (x >= 5)
x *= 2;
x += 2;
cout << x;
OUTPUT:
6
The general form of the if statement with block of statement is:
if (conditional expression) {
statement 1;
.
.
.
statement n;
}
In an if statement, if the expression evaluates to TRUE, the statement or block of statements that forms the target will be executed. Otherwise, the program will ignore the statement or the block of statements. Consider the given program segments below:
x = 5;
if (x >= 5) {
cout << "hello ";
cout << "good day";
}
OUTPUT:
hello good day
x = 4;
if (x >= 5) {
cout << "hello ";
cout << "good day";
}
cout << "God Bless";
OUTPUT:
God Bless
x = 5;
if (x >= 5) {
x *= 2;
cout << x << endl;
}
cout << x << endl;
OUTPUT:
10
10
x = 4;
if (x >= 5) {
x *= 2;
x += 2;
}
cout << x;
OUTPUT:
4
Example 1:
/* A program that will determine if the grade inputted is a pass or failing grade. The passing grade is 75. */
#include <iostream>
using namespace std;
int grade;
int main() {
cout << "ENTER GRADE: ";
cin >> grade;
if (grade >= 75)
cout << grade << " is PASSED";
return 0;
}
//end of program
#include <iostream>
using namespace std;
int grade;
int main() {
cout << "ENTER GRADE: ";
cin >> grade;
if (grade >= 75) {
cout << grade << " is PASSED";
}
return 0;
}
//end of program
SAMPLE OUTPUT:
ENTER GRADE: 85
85 is PASSED
Example 2:
/* A program that will determine which of the first inputted number is bigger than the second inputted number. */
#include <iostream>
using namespace std;
int num1;
int num2;
int main() {
cout << "ENTER TWO NUMBERS: ";
cin >> num1 >> num2;
if (num1 > num2) {
cout << num1 << " is bigger than " << num2;
}
return 0;
}
//end of program
SAMPLE OUTPUT:
ENTER TWO NUMBERS: 5 3
5 is bigger than 3
In the above example if the inputted value for the variable num1 greater than the variable num2, the cout << statement that follows will be executed.
Here is a video tutorial further explaining about if statement:
The if-else statement is used when the program is made to choose of the two paths or statements.
if-else statement
The general form of if-else statement is:
if (conditional expression)
statement 1;
else
statement 2;
The general form of if-else statement with block of statements is:
if (conditional expression) {
statement(s);
} else {
statement(s);
}
statement 1 and statement 2 may either be a single statement or block of statements.
In an if-else statement, if the expression is TRUE, the statement or block of statements after the if statement will be executed, otherwise the statement or block of statements in the else statement will be executed.
Example 1:
/* A program that will add or subtract the two inputted numbers depending on the chosen option . */
#include <iostream>
using namespace std;
int operation;
int num1;
int num2;
int answer;
int main() {
cout << "Enter 1 (add) or 2 (subtract): ";
cin >> operation;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
if (operation == 1) {
answer = num1 + num2;
cout << "Sum: " << answer << endl;
} else {
answer = num1 - num2;
cout << "Difference: " << answer << endl;
}
return 0;
}
//end of program
SAMPLE OUTPUT (1):
Enter 1 (add) or 2 (subtract): 1
Enter first number: 8
Enter second number: 4
Sum: 12
SAMPLE OUTPUT (1):
Enter 1 (add) or 2 (subtract): 2
Enter first number: 8
Enter second number: 4
Difference: 4
Here is an animated video explaining about if-else statement:
The ladderized if-else statement is used to choose one and only one of the given choices. Only those conditional statements and expressions that were first proven true, are the ones to be chosen and its associated statements will be executed.
ladderized if-else statement
The general form of the if-else-if ladder statement is:
if (conditional expression 1) {
statement 1;
} else if (conditional expression 2) {
statement 2;
} else if (conditional expression n) {
statement 3;
}
//can put many else if conditions as you need
} else {
statement else;
}
where:
conditional expresion 1, conditional expresion 2 up to conditional expression n is relational or Boolean expression that evaluates to a TRUE or FALSE value
statement and statement 2 up to statement else may either be a single or block of statements
If an if-elseif ladder statement, the expressions are evaluated from the top downward. As soon as a true condition is found, the statement associated with it is executed, and the rest of the ladder will not be executed. If none of the condition is true, the final else will be executed.
The final else acts as a default condition. If all other conditions are false, the last else statement is performed. If the final else is not present, then no action takes place.
NOTE: The final else is optional, you may include this part if needed in the program or you may not include if not needed.
Example 1:
/*
Write a program to assist a teacher in calculating student grades at the end of the semester. It accepts a numerical grade as input, then it will display the character grade as output, based on the given grade:
RANGE EQUIVALENT GRADE
90 and above A
80-89 B
70-79 C
60-69 D
below 60 E
*/
#include <iostream>
using namespace std;
int grade;
int main() {
cout << "\nENTER GRADE: ";
cin >> grade;
if ((grade >= 90) && (grade <= 100)) {
cout << "\nYour equivalent grade is A.";
} else if ((grade >= 80) && (grade <= 89)) {
cout << "\nYour equivalent grade is B.";
} else if ((grade >= 70) && (grade <= 79)) {
cout << "\nYour equivalent grade is C.";
} else if((grade >= 60) && (grade <= 69)) {
cout << "\nYour equivalent grade is D.";
} else {
cout << "\nYour equivalent grade is E.";
}
return 0;
}
//end of program
SAMPLE OUTPUT:
ENTER GRADE: 95
Your equivalent grade is A
Example 2:
/*
Write a program that examines the value of a variable called temp. Then display the following messages , depending on the value assigned to temp.
TEMPERATURE MESSAGE
Less than 0 ICE
Between 0 and 100 WATER
Exceeds 100 STEAM
*/
#include <iostream>
using namespace std;
float temp;
int main() {
cout << "ENTER THE VALUE OF THE TEMPERATURE NOW.\n";
cin >> temp;
if (temp < 0) {
cout << "It is an ICE\n";
} else if ((temp >= 0) && (temp <= 100)) {
cout << "It is a WATER\n";
} else {
cout << "It is a STEAM\n";
}
return 0;
}
//end of program
SAMPLE OUTPUT:
ENTER THE VALUE OF THE TEMPERATURE NOW
75
It is a WATER
Check this video for more explanations and examples:
One of the confusing aspects of the if statement in any programming language is nested if. A nested if is an if statement that is the object of either an if or else. This is sometimes referred to as "an if within an if".
The reason that nested ifs are so confusing is that it can be difficult to know what else associated with what if.
Fortunately, C++ provides a very simple rule for resolving this type of situation. In C++, the "else linked to the closest preceding if that does not already have an else statement associate with it".
Nested-if statement
The general form of the nested if-else statement is:
if (conditional expression 1) {
if (conditional expression 1.1) {
statement 1.1;
} else {
statement 1.2;
}
} else {
statement 2;
}
where:
conditional expression 1 and conditional expression 1.1 is a relational or boolean expression that evaluates to a TRUE or FALSE value.
statement 1, statement 1.1, statement 1.2 and statement 2 may either be a single statement or a block of statements.
Example 1:
/* Write a program that reads in three numbers A, B and C and determine which is the largest. */
#include <iostream>
using namespace std;
int A;
int B;
int C;
int main() {
cout << "Enter three numbers:";
cin >> A >> B >> C;
if (A > B) {
if (A > C) {
cout << A <<" is the largest.\n";
} else {
cout << C <<" is the largest.\n";
}
} else {
if (B > C) {
cout << B << " is the largest.\n";
} else {
cout << C << " is the largest.\n";
}
}
return 0;
}
//end of program
SAMPLE OUTPUT:
Enter three numbers: 3 6 9
9 is the largest.
Another video for your reference about nested if statement