Decision Making in C (if, else, switch)

Decision making allows a C program to choose different paths based on conditions. C provides several control statements like if, else-if, nested if, and switch to make decisions and control the flow of execution.

Why Decision Making?

Programs often need to make logical choices. Example: checking age for voting, validating passwords, menu-driven programs, etc.

1. if Statement

The if statement runs a block only if a condition is true.

if (condition) {
    // code runs when condition is true
}

Example

#include <stdio.h>

int main() {

    int age = 20;

    if(age >= 18) {
        printf("Eligible to vote");
    }

    return 0;
}

2. if-else Statement

Used when both true and false conditions need handling.

if (condition) {
    // true block
} else {
    // false block
}

Example

#include <stdio.h>

int main() {

    int marks = 55;

    if(marks >= 40)
        printf("Pass");
    else
        printf("Fail");

    return 0;
}

3. else-if Ladder

Useful when multiple conditions need checking sequentially.

if (condition1) {

} else if (condition2) {

} else if (condition3) {

} else {

}

Example

#include <stdio.h>

int main() {

    int marks = 78;

    if(marks >= 90)
        printf("Grade A+");
    else if(marks >= 75)
        printf("Grade A");
    else if(marks >= 60)
        printf("Grade B");
    else
        printf("Grade C");

    return 0;
}

4. Nested if

An if statement inside another if.

Example

#include <stdio.h>

int main() {

    int age = 22;
    char citizen = 'Y';

    if(age >= 18) {
        if(citizen == 'Y')
            printf("Eligible for voting");
        else
            printf("Not a citizen");
    } else {
        printf("Underage");
    }

    return 0;
}

5. switch Statement

The switch statement selects a block of code to execute based on a matching value.

switch(expression) {
    case value1: statements; break;
    case value2: statements; break;
    ...
    default: statements;
}

Example

#include <stdio.h>

int main() {

    int day = 3;

    switch(day) {
        case 1: printf("Monday"); break;
        case 2: printf("Tuesday"); break;
        case 3: printf("Wednesday"); break;
        case 4: printf("Thursday"); break;
        case 5: printf("Friday"); break;
        case 6: printf("Saturday"); break;
        case 7: printf("Sunday"); break;
        default: printf("Invalid day");
    }

    return 0;
}

Example – Character Menu

#include <stdio.h>

int main() {

    char ch = 'B';

    switch(ch) {
        case 'A': printf("Apple"); break;
        case 'B': printf("Banana"); break;
        case 'C': printf("Cherry"); break;
        default: printf("Unknown");
    }

    return 0;
}

Example – Simple Calculator

#include <stdio.h>

int main() {

    int a = 12, b = 4;
    char op = '*';

    switch(op) {
        case '+': printf("Sum = %d", a + b); break;
        case '-': printf("Difference = %d", a - b); break;
        case '*': printf("Product = %d", a * b); break;
        case '/': printf("Division = %d", a / b); break;
        default: printf("Invalid operator");
    }

    return 0;
}

Example 1 – Check Positive, Negative, or Zero

#include <stdio.h>

int main() {

    int n = -5;

    if(n > 0)
        printf("Positive");
    else if(n < 0)
        printf("Negative");
    else
        printf("Zero");

    return 0;
}

Example 2 – Largest of Two Numbers

#include <stdio.h>

int main() {

    int a = 12, b = 9;

    if(a > b)
        printf("A is larger");
    else
        printf("B is larger");

    return 0;
}

Example 3 – Leap Year Check

#include <stdio.h>

int main() {

    int year = 2024;

    if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        printf("Leap Year");
    else
        printf("Not a Leap Year");

    return 0;
}

Example 4 – Character Check (Alphabet, Digit, Symbol)

#include <stdio.h>

int main() {

    char ch = '9';

    if(ch >= '0' && ch <= '9')
        printf("Digit");
    else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
        printf("Alphabet");
    else
        printf("Symbol");

    return 0;
}

Example 5 – Electricity Bill Calculation

#include <stdio.h>

int main() {

    int units = 240;
    float bill;

    if(units <= 100)
        bill = units * 1.50;
    else if(units <= 200)
        bill = 150 + (units - 100) * 2.00;
    else
        bill = 350 + (units - 200) * 3.00;

    printf("Total Bill = %.2f", bill);

    return 0;
}

Example 6 – Switch Case for Months

#include <stdio.h>

int main() {

    int month = 4;

    switch(month) {
        case 1: printf("January"); break;
        case 2: printf("February"); break;
        case 3: printf("March"); break;
        case 4: printf("April"); break;
        case 5: printf("May"); break;
        case 6: printf("June"); break;
        case 7: printf("July"); break;
        case 8: printf("August"); break;
        case 9: printf("September"); break;
        case 10: printf("October"); break;
        case 11: printf("November"); break;
        case 12: printf("December"); break;
        default: printf("Invalid Month");
    }

    return 0;
}

Example 7 – Voting Eligibility (Nested If)

#include <stdio.h>

int main() {

    int age = 25;
    char id = 'Y';

    if(age >= 18) {
        if(id == 'Y')
            printf("Eligible for Voting");
        else
            printf("ID Proof Not Available");
    } else {
        printf("Underage");
    }

    return 0;
}

Example 8 – Pass/Fail with Grace Marks

#include <stdio.h>

int main() {

    int marks = 38;

    if(marks >= 40)
        printf("Pass");
    else if(marks >= 35)
        printf("Pass with grace marks");
    else
        printf("Fail");

    return 0;
}

Example 9 – Simple ATM Menu

#include <stdio.h>

int main() {

    int choice = 2;
    int balance = 5000;

    switch(choice) {
        case 1: printf("Balance: %d", balance); break;
        case 2: printf("Withdraw Money"); break;
        case 3: printf("Deposit Money"); break;
        case 4: printf("Exit"); break;
        default: printf("Invalid Option");
    }

    return 0;
}

Example 10 – Advanced Grading System

#include <stdio.h>

int main() {

    int marks = 87;

    if(marks >= 90)
        printf("A+");
    else if(marks >= 85)
        printf("A");
    else if(marks >= 75)
        printf("B+");
    else if(marks >= 60)
        printf("B");
    else if(marks >= 40)
        printf("C");
    else
        printf("Fail");

    return 0;
}

Difference Between if-else & switch

if-else:
- Works with ranges
- Works with conditions
- Slower for many comparisons

switch:
- Only works with exact values
- Cleaner for menu systems
- Often faster

Practice Questions

  1. What is an if-else ladder?
  2. Write a program to find the largest of 3 numbers.
  3. Create a switch menu for basic arithmetic operations.
  4. Explain nested if with an example.
  5. Write a program to check vowel or consonant using switch.

Practice Task

Create a menu program using switch: 1. Check Even/Odd 2. Find Square 3. Find Cube 4. Exit Take user input and perform the selected operation.