C if-else-if ladder

Syntax for if-else-if ladder

The general format for the if-else if statement is:


if (condition 1){
   simple or compound statement  //from   w  ww.  ja  v a  2s .  c o m
}else if (condition 2){
   simple or compound statement  
}else if ( condition 3){
   simple or compound statement  
   .....
}else if ( conditon n ){
   simple or compound statement  
}

Example for if-else-if ladder

The follow code uses if else if ladder to check int value.


#include <stdio.h>
/*  ww w . j  a v a  2 s . c  o m*/
main(){
  int i = 2;

  if(i == 0){
     printf(" i == 0. \n");
  }else if(i == 1){
     printf(" i == 1. \n");
  }else if(i == 2){
     printf(" i == 2. \n");
  }
}

The code above generates the following result.





















Home »
  C Language »
    Language Basics »




C Language Basics
Data Types
Operator
Statement
Array