C++ if…else statement

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++:

 if (condition) 
 {
  //code executed if condition true 
 }
 else
 {
  //code executed if condition false 
 }

If…else Flowchart:

if…else

Example of if…else statement in C++

#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++.

x is not less than 20;
value of x: 50