Simple If Statement In C



Syntax   
    if (condition )
    {
        True Statement;
    }
    Next Statement;

Simple If statement first check the condition. If condition is true then true statment block is executed and then Next Statement after the simple if ststement is executed. If condition is false then true statement block is not executed and directly next statement after simple if statment is executed. 

     
Example
    int mark = 10;
    if (mark < 35)
    {
        printf("Fail");
    }

If Else Statement In C



Syntax
    
if (condition )
    {
        True Statement;
    }
    else
    {
        False Statement;
    }
    Next Statement;

If...else statement first check the condition. If condition is true then true statment block is executed and then Next Statement after the if...else ststement is executed.
If condition is false then false statement block is executed and then next statement after if...else statment is executed.    

Example:
    int mark=20;
    if (mark < 35)
    {
          printf("Fail");
    }
    else
    {
          printf("Pass");
    }

Nested If Statement in C



Syntax
    if (OuterCondition)
    {
        if (InnerCondition)
        {
            True Statement Block for InnerCondition
        }
        else
        {
            Fasle Statement Block for InnerCondition
        }
    }
    else
    {
        False Statement Block for OuterCondition
    }
    Next Statement;

When one if statement is contained within another if statement then it is known as Nested If statement.
In Nested If Statement first outer condition is checked.
If outer condition is true then inner condiion is checked.
If inner condition is true then true statement block associated with inner condition is executed and then next statement after nested if statment is executed.
If inner condition is false then false statement block associated with inner condition is executed and then next statement after nested if statment is executed.
If outer condition is false then false statement block associated with outer condition is executed and then next statement after nested if statment is executed.

Example
int temp;
temp=38;
if(temp>20)
{
   if(temp>40)
   printf("Very Hot....\n");
   else
   printf("Hot...\n");
}
else
{
printf("Cool....");
}

If ElseIf Ladder In C



Syntax
    if (Condition1)
    {
       True Statement Block1;
    }
    else if (Condition2)
    {
       True Statement Block2;
    }
    .........................
    .........................
    else if (Condition N)
    {
       True Statement BlockN;
    }     
    else
    {
        Default Statement Block;
    }
    Next Statement;

If...else if ladder statement is used when multiple conditions has to be checked in the program.
First Condition1 is checked. If condition1 is true then True Statement Block1 is executed and then Next Statement is executed. If Condition1 is False then Condition2 is checked.
If condition2 is true then True Statement Block2 is executed and then Next Statement is executed. If Condition2 is False then Condition3 is checked.
This process of checking condition is repeated untill any condition evalutes to true.
If all conditions specified with if and else if evalutes to False then Default Statement Block associated with final else is executed.

Example
    float per=45;
    if (per >= 66)
    {
          printf("Distinction\n");
    }
    else if (per <66 && per >=56)
    {
          printf("First Class\n");
    }
      else if (per <56 && per >= 44)
    {
       printf("Second Class\n");
    }     
    else if (per <44 && per >= 35)
    {
       printf("Pass Class\n");
    }     
    else
    {
            printf("Sorry Fail");
    }

Download Projects


Download Programs