if, else if, else

We can execute statements depends on condition using if, else if or else keyword.
Syntax:

if(condition)
{
   //statements
}
else if(condition)
{
    //statements
}
else
{
     //statements
}

Here the first condition inside if will be checked. If this condition is true then statements inside if will be executed and else ifelse will be skipped otherwise condition inside else if() will be checked and if this condition is true then statements inside else if will be executed and else will be skipped otherwise statements inside else will be executed.
Any non-zero value will be treated as true and zero is treated as false. i.e:

if(10>8)

here 10>8 is true that means condition inside if is true. In the same manner for

if(10)

or

if(-12)

condition inside if is true.

if(0)  condition inside if is false.

Example:

#include<stdio.h>
int main()
{
     int n;
     printf("Enter a number :");
     scanf("%d",&amp;n);
     if(n&gt;0 &amp;&amp; n&lt;100)
     {
       printf("Your entered number positive is less than 100");
     }
     else if(n&gt;100)
     {
        printf("Your entered positive number is greater than 100");
     }
     else
     {
        printf("Your entered  number negative");
     }
     return 0;
}

OUTPUT

Enter a number : 102
Your entered positive number is greater than 100


Next Previous