Explain if-else statement in C language

If-else statement takes care of true as well as false conditions. ‘true block’ is executed, when the condition is true and ‘false block’ (or) ‘else block’ is executed, when the condition is false.


Refer the syntax given below −

if (condition){
   True block statement(s)
   False block statement(s)

Working of ifelse statement

  • In if else condition, if condition is true, it enter into true block statements, execute the operation and exits from the block.

  • If condition is false, it enter into else block, which is false block based on if condition, executes that else block and exist that else block.


Following is the C program to execute If and If Else conditional operators −

 Live Demo

void main (){
   int a=4;
   printf("Enter the value of a: 
");    scanf("%d",&a);    if(a%2==1){       printf("a is odd number
");    }else{       printf("a is even number");    } }


You will see the following output −

Run 1: Enter the value of a: 26
a is even number
Run 2: Enter the value of a: 53
a is odd number