if else - if, if else, nested and ladder

In this tutorial, We learn about different types of if else statements are simple if, if else, nested and ladder these all are decision making statement. We provide syntax for each, with examples.

Decision making contains much more are switch case, conditional operator and goto statement on this we learn in the next tutorial.

It is about dividing the program in the order of execution of certain statements based on some conditions. These are used when we have to set some limitations on statements based on conditions.

C if statement

This statement is also known as simple if statement. Let us see the general form or syntax f the if statement in c programming then explain it.

if( expression ) {
statement1;
statement2;
.
.
.
statementn;
}

How if statement work?

  • The if statement starts the evaluation of the condition expression in the () parenthesis. 
  • If the expression evaluates result True, then it will execute the statements written inside the if body. 
  • If the expression evaluates result False, then it will execute the statements written outside the if body. 

Example for if statement

#include<stdio.h>
int main()
{ 
  int n;
    printf("\n Enter a number:");
     scanf("%d",&n);
   if(n==0)
  {
    printf("zero",n);
  }
  return 0;
}

OUTPUT

 Enter a number:0
 zero


if else 

Syntax of the if else statement in c programming then explain it.

if( expression ) {
statement1;
statement2;
.
.
.
statementn;
}
else {
statement1;
statement2;
.
.
.
statementn;
}

How if else statement work?

  • The if statement starts the evaluation of the condition expression in the () parenthesis. 
  • If the expression evaluates result True, then it will execute the statements written inside the if body. 
  • If the expression evaluates result False, then it will execute the statements written inside the else body. 

Example for if else

#include<stdio.h>
int main()
{ 
  int n;
    printf("\n Enter a number:");
     scanf("%d",&n);
   if(n%2==0)
  {
    printf("%d is an even number",n);
  }
  else
  {
    printf("%d is an odd number",n);
  }
  return 0;
}

For an explanation of this even/odd program click here even/odd.

OUTPUT

 Enter a number:5
5 is an odd number

nested if else 

The general form or syntax of the nested if else statement in c programming is:

if( expression 1 ) { 
  if( expression 2 ) { 
  statements;
  }
  else{
  statements;
  }
}
  else {
  if( expression 3 ) { 
  statements;
  }
  else{
  statements;
  }
}

How nested if else statement work?

  • The if statement starts the evaluation of the condition expression 1 in the () parenthesis. 
  • If the expression evaluates result True, then it will execute the if statement was written inside the if body. If, expression 2 is true then run statements within it, otherwise else part run of if with expression 2.
  • If the expression evaluates result False, then it will execute the if statement was written inside the if body. If, expression 3 is true then run statements within it, otherwise else part run of if with expression 3.

Example C nested if else

#include<stdio.h>
int main()
{ 
 int year;
  printf("\n Enter a year:");
   scanf("%d",&year);
       if(year%100==0)
        {
          if(year%400==0)
             { 
               printf("%d is a leap year",year);
              }
          else
             {
               printf("%d is not a leap year",year);
             }
        }
       else
        { 
          if(year%4==0)
             {
               printf("%d is a leap year",year);
             }
          else
             {
               printf("%d is not a leap  year",year);
             }
        }
 return 0;
}

For an explanation of this program click here leap/non leap year.

OUTPUT

Enter a year:1900
1900 is not a leap year


else if ladder

Syntax of the else if ladder statement in c programming then explain it.

if ( expression1 ) {
   statements;
}
else if( expression2 ) {
   statements;
}
else if ( expression3 ) {
   statements;
}
.
.
else {
   statements;
}

How else if ladder statement work?

  • The if statement starts the evaluation of the condition expression 1 in the () parenthesis. 
  • The expression is tested from the top (of the ladder) downwards.
  • As soon as a true condition is found, the statement associated with it is executed.

Example else if ladder


#include<stdio.h>
int main()
{
  int i,m1,m2,m3,m4,m5,rollno,tot;
  float perc;
  char grade;
  for(i=1;i<=5;i++) 
  {
   printf("\n Enter the roll no:-");
   scanf("%d",&rollno);
   printf("\nenter marks in 5 subject");
   scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
   tot=m1+m2+m3+m4+m5;
   perc=(float)(tot/5.0);
    if(perc=90)
    {
     grade='A';
    }
    else
    {
      if(perc=80)
      {
       grade='B';
      }
      else
      {
     if(perc=70)
      {
       grade='C';
      }
      else
      { 
      if(perc=50)
      {
       grade='D';
      }
      else
      {
       if(perc=36)
       {
        grade='E';
       }        
       else
       {
        grade='F';
       } 
      }
     }
    }
   }
  printf("\nrollno of student =%d",rollno);
      printf("\nmarks in subject1=%d",m1);
      printf("\nmarks in subject2=%d",m2); 
      printf("\nmarks in subject3=%d",m3);
      printf("\nmarks in subject4=%d",m4);
      printf("\nmarks in subject5=%d",m5);
      printf("\ntotal marks in subject=%d",tot);
      printf("\ntotal percentage in subject=%f",perc);
      printf("\ntotal grade in subject=%c\n",grade);
  } 
return 0;
}

For an explanation of this program click here.

OUTPUT


Enter a year:1900 
1900 is not a leap year

If you have any doubts, Please let me know

Previous Post Next Post

Contact Form