C++ if…else statement



If block sometimes followed with the optional else block. When the if condition becomes false then else block gets executed.

Syntax of C++ If-else :

if(boolean_expression)
{
   // True Block
}
else
{
   // False Block
}

If the boolean_expression becomes false then then else block will be executed. If the boolean expression becomes true then by default true block will be executed.

If Else Flowchart :

if else in cpp

Example : C++ If Else Statement

#include <iostream>
using namespace std;
 
int main ()
{
   // declare local variable
   int marks = 30;
 
   // check the boolean condition
   if( marks > 40 )
   {
       // if condition is true
       cout << "You are pass !!" << endl;
   }
   else
   {
       // if condition is false
       cout << "You are fail !!" << endl;
   }
 
   return 0;
}

Output :

You are fail !!