Header Ads Widget

if - else - if Ladder

if( condition 1)
  statement A;
else if( condition 2)
  statement B;
else if( condition 3)
  statement C;
else if( condition 4)
  statement D;
else
  statement X;


The conditions are evaluated from top to downward. When a true condition is found , the statement associated  with it is executed and the rest of ladder is bypassed. If none of conditions are true then the final else is executed. If there is no true condition and final else is not present then no action takes place.

Example :  Whether  a number given by user is zero, positive or negative.

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

Output :
Enter number :-7
Number is negative.




Post a Comment

0 Comments