In this tutorial you will learn about the C++ if else Statement and its application with practical example.
C++ if else Statement
In C++, when we want to execute a block of code when if condition is true and another block of code when if condition is false, In such a case we use if…else statement.
C++ If…else Statement Flow Diagram
Syntax:-
1 2 3 4 5 |
if(condition){ // statements } else { // statements } |
Here, Condition is a Boolean expression that results in either True or False, if it results in True then statements inside if body are executed, if it results in False then statements inside else body are executed.
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> using namespace std; int main() { int a = 10; int b = 20; cout<<"W3Adda - C++ If else Statement"; if(a > b){ cout<<"\na is greater than b"; } else { cout<<"\nb is greater than a"; } return 0; } |
Output:-