If statement

If statement is the conditional statement.

Conditional statement :- it means we can change flow of executing program based on condition in program.

It is the simple conditional statement in c++. It contains condition along with this and if followed with the condition , if this conditional is true then the statements followed by if statement is executed otherwise statement in if block are skipped.


12345678910111213if(condition){Statement 1;Statement 2;..Statement n;}

In above syntax , we write if keyword followed by condition in bracket (). and after that we write statement in block enclosed in curly braces { }. You can write any number of statement in if block enclosed in { } . if you want to execute only one statement in if block then you not need to write curly braces.In this case statement (one) followed by if block is executed.


123if(x==1)cout<<“x value 1”;

In above example, if statement checks value of x if it is equal to 1 then statement x value 1 is executed otherwise not.

Program to demonstrate if statement

1234567891011121314151617181920212223242526272829303132333435#include<iostream.h>#include<conio.h>void main (){int x;cout<<“conditional statement in c++”<<endl;cout<<“Enter x value”<<endl;cin>>x;if(x==1) //if statement to check condition{cout<<“x value is 1”; //executed if condition true}if (x==2){cout<<“x value is 2”;. //executed if condition true}getch();}

Output :-

Conditional statement in c++

Enter x value


x value is 1

Program description

  1. In above program we demonstrate if statement.
  2. First we taken input from user in variable x
  3. Then first if condition we check condition if(x==1) if user enters 1 then statement in if is executed.and if x value is one second condition get false so second statement if block is skipped.
  4. Next second if condition we check condition if (x==2) is user enters 2 then statement in if block is executed and as user entered 2 condition in first if (x==1 ) get false so statement in if block is skipped.
  5. If users enters input other than 1 or 2 both if statement get false and statement in both block get skipped.