C++ if else statement: The C++ if else statement helps when if statement executes false we can use the else statement for executing. It is also used to control the flow of statements based on some conditions.
C++ if else statement Syntax and Flow Chart
Syntax:
if(condition){
//code if condition is true
}else{
//code if condition is false
}
C++ If Else Example:
#include<iostream> using namespace std; int main() { int i = 30; if(i < 10) cout<<"i is smaller than 10"; else cout<<"i is greater than 10"; return 0; }
Output:
i is greater than 10
Example 2
#include <iostream> using namespace std; int main () { int x = 30; //variable declaration //check the condition if( x < 20 ) { cout << "x is less than 20;" << endl;//if condition is true then print the following } else { cout<<"a is not less than 20;"<< endl;//if condition is false then print the following } cout<<"The value of x is : "<< x <<endl; return 0; }
Output:
a is not less than 20;
The value of x is: 30