Decision Making in C (if , if..else, Nested if, if-else-if )

Last Updated : 29 Jun, 2026

Conditional statements are used to execute different blocks of code based on whether a specified condition is true or false.

  • They evaluate one or more conditions before making a decision.
  • They help control the program flow by executing code only when required.
C
#include <stdio.h>

int main() {

    int num = 100;
    
    if (num > 50) {
        printf("Start the show");
    }

    return 0;
}

Output
Start the show

Explanation

In the above example, the if statement executes the code only when the specified condition is true.

  • If num > 50 is true, the message is displayed.
  • If num <= 50, the if block is skipped and no output is produced.

Types of Conditional Statements in C

In the above program, we have used if statement, but there are many different types of conditional statements available in C language:

conditional statements in c

if statement

The if statement is used to execute a block of code only when a specified condition is true.

  • It is the simplest decision-making statement in C.
  • The condition must evaluate to true or false to determine whether the block is executed.
C
#include <stdio.h>

int main()
{

    int age = 20;

    // If statement
    if (age >= 18)
    {
        printf("Eligible for vote");
    }
    return 0;
}

Output
Eligible for vote

The expression inside () parenthesis is the condition and set of statements inside {} braces is its body. If the condition is true, only then the body will be executed.

If there is only a single statement in the body, {} braces can be omitted.

if-else statement

The if-else statement is used to execute one block of code if a condition is true and another block if it is false.

  • The if block executes when the condition is true.
  • The else block executes when the condition is false.
C
#include <stdio.h>

int main() {
    int age = 10;

    if (age >= 18) {
        printf("Eligible for vote");
    }
    else {
        printf("Not Eligible for vote");
    }
    return 0;
}

Output
Not Eligible for vote

The block of code following the else statement is executed as the condition present in the if statement is false.

Nested if-else statement

A nested if-else is an if or if-else statement placed inside another if or else block.

  • It is used to check multiple conditions in a step-by-step manner.
  • The inner if statement is evaluated only if the outer condition allows it.
C
#include <stdio.h>

int main(){
    int age = 11;

    if (age >= 18) {
        if (age >= 60)
            printf("Eligible to vote (Senior Citizen)\n");
        else
            printf("Eligible for vote\n");
    }
    else {
        printf("Not eligible to vote (Under 18)\n");
        if (age >= 13) 
                printf("teenager\n");
            else
                printf("not a teenager\n");
        }

    return 0;
}

Output
Not eligible to vote (Under 18)
not a teenager

if-else-if Ladder statement

The if-else-if ladder is used to test multiple conditions and execute the block corresponding to the first true condition.

  • Conditions are checked from top to bottom until one evaluates to true.
  • If no condition is true, the else block is executed (if present).
C
#include <stdio.h>

int main() {
    int i = 20;

    // If else ladder with three conditions
    if (i == 10)
        printf("Not Eligible");
    else if (i == 15)
        printf("wait for three years");
    else if (i == 20)
        printf("You can vote");
    else
        printf("Not a valid age");
        
    return 0;
}

Output
You can vote

Switch Statement

The switch case statement is used to execute different blocks of code based on the value of an expression or variable.

  • It provides an alternative to the if-else-if ladder for multiple value-based conditions.
  • The matching case is executed based on the value of the switch expression.
C
#include <stdio.h>

int main() {
    
    // variable to be used in switch statement
    int var = 18;

    // declaring switch cases
    switch (var) {
    case 15:
        printf("You are a kid");
        break;
    case 18:
        printf("Eligible for vote");
        break;
    default:
        printf("Default Case is executed");
        break;
    }

    return 0;
}

Output
Eligible for vote

Note: The switch expression should evaluate to either integer or character. It cannot evaluate any other data type.

Conditional Operator

The conditional operator (?:) is used to choose between two expressions based on a condition.

  • It is a shorthand alternative to the if-else statement.
  • It works with three operands: a condition, a true expression, and a false expression.
C
#include <stdio.h>

int main() {
    int var;
    int flag = 0;

    // using conditional operator to assign the value to var
    // according to the value of flag
    var = flag == 0 ? 25 : -25;
    printf("Value of var when flag is 0: %d\n", var);

    return 0;
}

Output
Value of var when flag is 0: 25

Related Article

Comment