C++ if statement

C++ if statement: The C++ if statement is a simple decision-making statement. It is used to check whether a certain block of statements will be executed (or) not. So, if a certain condition is true then a block of statements will be executed otherwise not. We can also this statement is the easiest and simple. And the if statement consists of a boolean expression followed by one (or) more statements.

C++ if statement Syntax

Syntax:

if(condition)
{
// Statements to execute if
// condition is true
}

Here, the code contains either true (or) false. If the boolean values are accepted by statements, if the value is true then the block of statements will be executed. Otherwise, it will exit. And also if you don’t place the braces ‘ { ‘ properly then it will execute the other statement.

FLOW CHART FOR IF STATEMENT

C++ if statement

C++ if statement Example:

#include <iostream>  
using namespace std;   
int main () {  
 int num = 10;    
   if(num % 2 == 0)    
   {    
     cout<<"It is an even number";    
   }   
  return 0;  
}

Output:

It is an even number

Example 2:

#include<iostream>
 using namespace std;
int main()
{
    int a = 10;
    if (a > 20)
    {
       cout<<"10 is less than 20";
     }
 
cout<<"Out of if block";
}

Output:

Out of if block