If the Boolean expression is true then the code inside the if
statement block is executed or if it is false then the code inside else
statement will be executed. Hence if..else statement.
The syntax of the if..else
statement in C++:
1 2 3 4 5 6 7 8 | if (condition) { //code executed if condition true } else { //code executed if condition false } |
If…else Flowchart:
Example of if…else statement in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; int main() { int x = 50; // check condition if (x < 20) { //condition is true cout << "x is less than 20;" << endl; } else { //condition is false cout << "x is not less than 20;" << endl; } cout << "value of x: " << x << endl; return 0; } |
The output of if…else statement in C++.
1 2 | x is not less than 20; value of x: 50 |