If else condition in C programming language

If-else statement

In if-else statement first of all ‘If’ condition is checked and if the condition is true then ‘If’ statement gets executed otherwise ‘else’ statements gets executed. An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

An ‘If’-‘else’ statement is used whenever there is need in our program to make decision i.e. when there is a need to move forward in a particular direction. For example a person having age of 18 years or more he is allowed to vote otherwise he is not allowed to enter to the polling booth to cast his vote.

Syntax of if-else statement

If (condition)
{
Statement 1;
Statement 2;
…
}
else
{
Statement 1;
…
}

Flowchart of if else statement

#include <stdio.h>
int main()
{
    int age;
    printf("Enter your age\n");
    scanf("%d", &age);
    printf("You have entered %d as your age\n", age);
    if (age>=18) {
        printf("You can vote!");
    }
    else
    {
        printf("You cannot vote!");
    }
    return 0;
}