C++ If Statement

If Statement is used for decision making and control the flow of execution of statement.It is used with relational or logical statement.

Simple If


In Simple If, there is only one condition. If that condition is true, set of statement will be executed.
Syntax


if(condition)
{
  Statement 1;
}
Statement 2;



Here, Condition is any relational or logical expression.
If condition is true Statement 1 is executed.
if condition is false Statement 1 is not executed.
Statement 2 will be executed in both cases.

Program To show use of Simple If Statement.



#include<iostream.h>
#include<conio.h>
void main()
{
      int a=15,b=7;
      if(a>b)
      {
             cout<<"a is greater than b";
             cout<<"\nWorked";
      }
}



Output


a is greater than b
Worked

If Else Statement


In if else statement, there is only one condition depending upon the condition, a set of statement will be executed. If statement is true, set of statement will be executed or other set of statement will be executed if condition is false.

Syntax


if(condition)
{
    Statement 1;
}
else{
    Statement 2;
}
Statement 3;



Here, Condition is any relational or logical expression.
If condition is true Statement 1 will be executed
If condition is false Statement 2 will be executed
Statement 3 will be executed in both cases.

Program to Show use of if else statement.



#include<iostream.h>
#include<conio.h>
void main()
{
    int a=5,b=17;
    if(a>b)
    {
          cout<<"a is greater than b";
    }
    else{
          cout<<"a is not greater than b";
    } 
}



Output

a is not greater than b

Nested If Statement


In nested if else statement, one if statement is enclosed within another if statement.

Syntax


if(condition 1){
    if(condition 2){
        Statement 1;
    }
    else{
         Statement 2;
    }
}
else{
     if(condition 3){
         Statement 3;
      }
      else{
          Statement 4;
       }
}
Statement 5;


Here, condition 1, condition 2, condition 3 are any relational or logical statement.
Initially condition 1 will be checked, If it is true condition 2 will be checked.
If condition 2 is true, Statement 1 will be executed. If condition 2 is false, Statement 2 will be executed.
If condition 1 is false, condition 3 will be checked.
If condition 3 is true, Statement 3 will be executed. If condition 3 is false, Statement 4 will be executed.
Statement 5 will be executed.

Program To show use of nested if statement.



#include<iostream.h>
#include<conio.h>
void main()
{
     int a=12,b=7,c=17;
     if(a>b){
         if(a>c){
             cout<<"a is greatest";
         }
         else{
             cout<<"c is greatest";
         }
     }
     else{
         if(b>c){
             cout<<"b is greatest";
         }
         else{
             cout<<"c is greatest";
          }
     }
}



Output


c is greatest

If else if ladder Statement


In this if else if ladder statement, there are multiple conditions one after another.

Syntax


if(condition 1)
{
    Statement 1;
}
else if(condition 2)
{
    Statement 2;
}
else if (condition 3)
{
     Statement 3;
}
else if(condition 4)
{
    Statement 4;
}
:
:
else{
    Statement N;
}
Statement 5;


Here, condition 1, condition 2, condition 3 are any relational or logical expression.
Initially, condition 1 is checked . If it is true, Statement 1 will be executed.
If condition 1 is false, then condition 2 is checked . If condition 2 is true then Statement 2 is executed. and so on. If all condition are false then Statement N is executed.

Program to show use of if else if ladder statement.



#include<iostream.h>
#include<conio.h>
void main()
{
     clrscr();
     int a=0;
     if(a>0)
     {
         cout<<"\n a is +ve";
      }
      else if(a<0)
      {
          cout<<"\n a is -ve";
       }
       else
       {
           cout<<"\n a is zero";
       }
     getch();
}


Output


a is zero

Back To Top