else if statement - Devbhoomi

FREE JOB ALERT & ONLINE TUTORIALS

Hot

Post Top Ad

Sunday 5 November 2017

else if statement

To show a multi-way decision based on several conditions, we use the else if statement.
Syntax :
 if(condition_2)
 {
    block 1 statement;
 }
 else if (condition_2)
 {
    block 2 statement;
 }
 else if(condition_n)
 {
    block n statement;
 }
 else
 {
    block x statement;
 }

here, the conditions are evaluated in order from top to bottom. As soon as any condition evaluates to true, then statement associated with the given condition is executed and if all condition are false, then control is transferred to statement_x skipping the rest of the condition.
Example :

    #include <stdio.h>

    int main ()
    {
    int mark;

    printf("Enter total marks of student : ");
    scanf("%d",&mark);

    if(mark <= 50)
    {
    printf("\nGrade D" );
    }
    else if (mark <= 60)
    {
    printf("\nGrade C");
    }
    else if (mark <= 70)
    {
    printf("\nGrade B");
    }
    else
    {
    printf("\nGrade A");
    }

    return 0;
    }
Output :
 Enter total marks of student : 80
 Grade A

No comments:

Post a Comment

Post Top Ad