if else in c

In c, if else is a decision making statement. if condition or expression of if returns true, then the statements of if block executes, if it returns false, then statement of else block will execute.

Note: else only executes, when if returns false.

Syntax

if(expression)
{
  //set of statements
}
else
{
 //set of statements
}

Example

#include<stdio.h>

void main()
{
    int weight = 60;
    if(weight <= 65)
    {
       printf("Weight is normal");
    }
    else
    {
       printf("Over weight");
    }
}
Output:
Weight is normal

Explanation

  1. In above example, variable weight is declared and initialized with value 60.
  2. With if else, if checks the given condition if(weight <= 65) is true or not.
  3. If it returns true, then the if block statements, gets executed.
  4. If it returns false, then else block statements, gets executed.