if else statement in C programming language

The if-else statement in C is used to perform the operations based on some specific condition. There are the following variants of if statement in C language.
  • if statement
  • if...else statement
  • else...if Ladder
  • Nested if...else statement

if statement : The if statement is used to check some given condition and perform some operations depending upon the correctness of that condition. The operations specified in if block are executed if and only if the given condition is true.

The syntax of an if statement in C programming language is :
if(condition){ 
      code to be executed;
}

Example :
#include<stdio.h>
int main()
{
      int a=10;
      if(a==10){
            printf("The condition is true");
      }
      return 0;
}

Output :
The condition is true


if...else statement : The if...else statement is used to perform two operations for a single condition.

The syntax of an if...else statement in C programming language is :
if(condition){
      code to be executed if condition is true;
}
else
{
      code to be executed if condition is false;
}

Example :
#include<stdio.h>
int main()
{
      int age=22;
      if(age>=18)
      {
            printf("You are eligible");
      }
      else
      {
            printf("You are not eligible");
      }
      return 0;
}

Output :
You are eligible


else...if Ladder : The else...if ladder statement is used in the scenario where there are multiple cases to be performed for different conditions. 

The syntax of else...if ladder statement in C programming language is :
if(condition_1){ 
      code to be executed if condition_1 is true;
}
else if(condition_2)

      code to be executed if condition_2 is true;

else if(condition_3)

      code to be executed if condition_3 is true;

... 
else

      code to be executed if all the conditions are false;
}

Example :
#include<stdio.h>
int main()
{
      int a, b;
      printf("Enter any two numbers : ");
      scanf("%d%d",&a, &b);
      if(a > b)
      {
            printf("%d is the greater than %d",a,b);
      }
      else if(a<b)
      {
            printf("%d is the less than %d",a,b);
      }
      else
      {
            printf("Both numbers are equal");
      }
      return 0;
}

Output :
Enter any two numbers : 5 8
5 is the less than 8


Nested if...else statement : In nested if...else statements, you can use one if or else if statement inside another if or else if statement(s).

The syntax of nested if....else statement in C programming language is :
if(condition){
      if(condition_1){
            code to be executed if condition_1 is true;
      }
      else
      {
             code to be executed if condition_1 is false;
       }
}
else
{
      code to be executed if condition is false;
}

Example :
#include<stdio.h>
int main()
{
     int a, b;
     printf("Enter any two numbers : ");
     scanf("%d%d",&a, &b);
     if(a >= b)
     {
          if(a==b){
               printf("Both are equal");
          }
          else
          {
               printf("%d is greater than %d",a,b);
          }
     }
     else
     {
          printf("%d is less than %d",a,b);
     }
     return 0;
}

Output :
Enter any two numbers : 7 1
7 is greater than 1

0 Comments