Close
Close

go to C>Decide if/else>else if statements actually see the syntax and example programe where is else if in syntax program in chapter???

   jenny_uppal_1722

#include <stdio.h>
int main()
{
    if(condition)
    {
        statement
        statement
        ...
    }
    else if(condition)
    {
        statement
        statement
        ...
    }
    else if(condition)
    {
        statement
        statement
        ...
    }
    else
    {
        statement
        statement
        ....
    }
    return 0;
}

  • Sorry but I didn't get the question. Please make it more clear
    - Amit Kumar
  • i think the #include <stdio.h> int main() { if(condition) { statement statement ... } else if(condition) { statement statement ... } else if(condition) { statement statement ... } else { statement statement .... } return 0; } is not right!! i think there should be (else if) insted of (else) in the last..?
    - jenny_uppal_1722
  • there should be ( else if) not (else) in the last.....right????..thisis my ques
    - jenny_uppal_1722
  • No, there will be else if you want one. If neither the condition of if nor any of the conditions of else if are true the else gets executed.
    - Amit Kumar
  • For example, look at the answer below.
    - Amit Kumar

Answers

  •   

    Look at the code:

    #include<stdio.h>
    main(){
      int  i = 10;
      if (1==5){
        printf("if\n");
      }
      else if(i==7){
        printf("else if\n");
      }
      else{
        printf("else\n");
      }
    }
    

    In this code else will be executed and else will be printed.



  •   

    jenny its easy,,try to understand...consider the below program

    #include<stdio.h> 
    main()
    {  int  i = 10,j=2;
      if (i==j){   
     printf("equal"); 
     } 
     else if(i==7)
    {    
    printf("i value 7"); 
     }  
    else{    
    printf("not equal");  
    }
    }

    in first if condition, i=j is tested since it is false it goes to else if condition since it is also false it goes to last statement where it acts a default statement if above condtions are false..so no need to write the condition again...hope u understand..thanks



Ask Yours
Post Yours
Write your answer