Explain if-else statement in C language

CServer Side ProgrammingProgramming

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.

Syntax

Refer the syntax given below −

if (condition){
   True block statement(s)
}else{
   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.

Example

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

 Live Demo

#include<stdio.h>
void main (){
   int a=4;
   printf("Enter the value of a: \n");
   scanf("%d",&a);
   if(a%2==1){
      printf("a is odd number \n");
   }else{
      printf("a is even number");
   }
}

Output

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
raja
Published on 22-Feb-2021 11:09:29
Advertisements