C If else if Ladder statement

if else if Ladder

The general form of else-if ladder is,

Syntax:

if(expression 1)
{
    statement-block1;
}
else if(expression 2)
{
    statement-block2;
}
else if(expression 3)
{
    statement-block3;
}
else
    default-statement;

The expression is tested from the top (of the ladder) to downwards. As soon as the true condition will found, the statement associated with it is executed.

Example:

//Write a C Program which demonstrate use of if-else-if ladder statement.
#include<stdio.h>
#include<conio.h>
void main()
{
    int a;
    printf("Enter a Number: ");
    scanf("%d",&a);
    if(a > 0)
    {
        printf("Given Number is Positive");
    }
    else if(a == 0)
    {
        printf("Given Number is Zero");
    }
    else if(a < 0)
    {
        printf("Given Number is Negative");
    }
    getch();
}

Output:

Command Prompt
Enter a Number: -5
Given Number is Negative


Share This Page to Download:

Share to Download

Share This Page on:


Ask Question