Conditional controlling and if else statement

We can control the flow of our program using if else statement. If else statement let your program to take the decision that which code to should be executed. Without if else condition our program will be work in same way every time. Generally, we use if else statement in almost every program to take a decision.

If else statement works by the method of two binary digits 0 and 1. If a condition returns 1 then this is considered as true (actually all non-zero integer is considered as true) and the program will execute the code inside that block. On the other hand, if a condition returns zero then it is considered as false to the compiler and it will not execute the code inside that block.

Let’s try to understand if else statement through a C program bellow;

#include <stdio.h>
int main(){
    int age;
    printf("Enter your age : \n");
    scanf("%d", &age);

    if (age < 18){
        printf("You are not eligible for voting.\n");
        printf("You are eligible for voting.\n");

return 0;

Write this code and run it. It will want an input from you. Give any integer of less than 18 as the input here. Then press the enter and you will see the following output.

if else condition in c programming, if else statement in c

Again run the code and give any integer of greater than 18 as the input. Now you will see the following output.

if else statement in c programming, if conditions in c

For first case you have given the input 16 which fulfill the first condition of age < 18. For this it gives the output of the code inside the first block.

For second case you have given the input 21 which does not fulfill the first condition of age < 18 rather it fills the condition that age >= 18. So the program has executed the second condition which was inside the else block here.

Now, lets see another example of if else statement here to take a good knowledge about if else statement in C programming.

#include <stdio.h>
int main(){
    int yourAge, yourFriendsAge;
    printf("Enter your age : \n");
    scanf("%d", &yourAge);

    printf("Enter your friend's age : \n");
    scanf("%d", &yourFriendsAge);

    if (yourAge > yourFriendsAge){    // checking if you are older
        printf("You are older than your friend.\n");
    }else if(yourFriendsAge > yourAge){    // checking if your friend is older
        printf("Your friend is older than you.\n");
    }else{      // default that you are both at the same age
        printf("Wow! you have the same age as your friend.\n");

return 0;

Function of "if statement" in a program

If statement checks the condition inside if that is it true or not. If the condition is true then if will go to the conde inside this block and execute them. But if the condition is not true. Then it will go the the next block to check other code and instruction.

Lets try to understand through a program.

#include <stdio.h>
int main(){
    int i;
    printf("Enter an integer to check : \n");
    scanf("%d", &i);

    if (i > 0){
        printf("%d is positive.\n", i);

    if (i < 0){
        printf("%d is negative.\n", i);

    if (i == 0){
        printf("You have entered zero. Ok, it is also positive.\n");

return 0;

Function of "else statement" in program

When the condition inside the if  statement becomes false then the compiler automatically goes to the else block and execute the code inside else block without checking any condition. Because compiler knows the code inside else as a default to execute. But the compiler ignore the else block when the if block will be true.

Let’s understand by a C program.

#include <stdio.h>
int main(){
    int a;
    printf("Enter any integer : \n");
    scanf("%d", &a);

    if (a >= 0){        // checking condition
        printf("The number is positive.\n");

    }else{          // no need to check any condition
        printf("The number is  negative.\n");

return 0;

Function of "else if statement" in a program

When we need to check more than one condition then we have to use else if statement. A compiler will first check the condition of “if statement”. If that condition is false, then the compiler will check the “else if statement”. We can use any number of else if block under a if block. When the compiler will not get any true condition by checking every else if block, then it will perform the default instruction given inside the else block.

Try to understand through the given C program.

#include <stdio.h>
int main(){
    int number;
    printf("Enter your obtained number in the exam : \n");
    scanf("%d", &number);

    if (number < 33){
        printf("OOPS! You are fail in the exam.\n");

    }else if(number < 40){
        printf("You have got D grade.\n");

    }else if(number < 50){
        printf("You have got C grade.\n");

    }else if(number < 60){
        printf("You have got B grade.\n");

    }else if(number < 70){
        printf("You have got A- grade.\n");

    }else if(number < 80){
        printf("You have got A grade.\n");

        printf("Wow! You have got A+");

return 0;

Operators to check multiple conditions

We can also check multiple condition inside any if or if else block. Here we have to know the function of the following operators.

  • && (AND operator) will return true when the condition of both side will be true.
  • || (OR operator) will return true if the condition of any side returns true. Otherwise it will return false.
  • != will return true if the value of both side is not equal. Otherwise it will return false.
  • == will return true if the value of both side is equal. Otherwise it will return false.
  • Other operators will do their own functions which we will understand at the time of our coding.

See the following example to understand the use of multiple conditions.

#include <stdio.h>
int main(){
    int x, y, z;
    printf("Enter three integer to check the maximum of them : \n");
    scanf("%d%d%d", &x, &y, &z);

    if (x > y && x > z){
        printf("%d is the biggest number.\n", x);

    }else if(y > x && y > z){
        printf("%d is the biggest number.\n", y);

        printf("%d is the biggest number.\n", z);

return 0;

Let’s see another example. We can write the program to determine letter grade as the following.

#include <stdio.h>
int main(){
    int yourNumber;
    printf("Enter your number in the examination.\n");
    scanf("%d", &yourNumber);

    if (yourNumber > 100 || yourNumber < 0){
        printf("Invalid number.\n");

    }else if(yourNumber >= 0 && yourNumber < 33){
        printf("OH! You are fail in the exam.\n");

    }else if(yourNumber >= 33 && yourNumber < 40){
        printf("You have got GPA 1.00\n");

    }else if(yourNumber >= 40 && yourNumber < 50){
        printf("You have got GPA 2.00\n");

    }else if(yourNumber >= 50 && yourNumber < 60){
        printf("You have got GPA 3.00\n");

    }else if(yourNumber >= 60 && yourNumber < 70){
        printf("You have got GPA 3.50\n");

    }else if(yourNumber >= 70 && yourNumber < 80){
        printf("You have got GPA 4.00\n");

        printf("Wow! You have got GPA 5.00\n");

return 0;

Go to the next page to learn about nested if else condition.