×

Advanced Coding & Software Engineering Program

Duration: 1 Year (12 Months)

Join our premium 1-year program to master cutting-edge technologies and become an industry-ready Software Engineer!

Course Coverage

  • Languages: C, C++, Java, JavaScript, Python
  • Web Technologies: HTML, CSS, Bootstrap 5, MERN Stack, Full Stack Development
  • Databases: MySQL, MongoDB
  • Data Science Libraries: Pandas, NumPy
  • Development Tools: Visual Studio Code, IntelliJ IDEA, PyCharm, Postman, Git, GitHub
  • Cloud Platforms: Vercel, MongoDB Atlas

Program Highlights

  • Live Classes: Interactive sessions with real-time doubt resolution
  • Hands-On Sessions: Practical coding exercises to build real-world skills
  • Industry Experts: Learn from professionals with years of experience
  • Live Project: Work on real-world projects to apply your skills
  • Get Certificate: Earn a professional certificate upon program completion

Course Fee: Only ₹1020 / month
Limited Period Offer!

Decision-Making Statements in C: From Basics to Advanced



Last Updated on: 24th Jul 2025 18:27:12 PM

Decision-making statements are the heart of any C program, allowing it to make choices based on conditions. Whether you're building a simple calculator or a complex game, these statements guide your code’s flow like a GPS for your program. In this tutorial, we’ll explore Simple if, if-else, Nested if-else, Ladder if (else-if ladder), and switch case, with examples ranging from beginner-friendly to advanced. Each example includes code, output, and a detailed explanation to make learning engaging and clear. Let’s get started!

 

1. Simple if Statement

The if statement evaluates a condition. If the condition is true, the code inside the if block executes. It’s like deciding to bring an umbrella if it’s raining.

Explanation: The condition temp >= 15 && temp <= 30 checks if the temperature is within a comfortable range. The && operator ensures both conditions are true. If false, the else block warns the user.

 

Syntax:

if (condition) {
    // Code executes if condition is true
}

 

Basic Example: Check Voting Eligibility

#include <stdio.h>
int main() {
    int age = 19;
    if (age >= 18) {
        printf("You are eligible to vote!\n");
    }
    return 0;
}

 

Output:

You are eligible to vote!

Explanation: The condition age >= 18 is true for age = 19, so the message is printed. If age were less than 18, nothing would happen.

 

Advanced Example: Password Strength Check

This example checks if a password length is sufficient for basic security.

#include <stdio.h>
#include <string.h>
int main() {
    char password[50];
    printf("Enter your password: ");
    scanf("%s", password);
    if (strlen(password) >= 8) {
        printf("Password is long enough for basic security.\n");
    }
    printf("Program ends.\n");
    return 0;
}

 

Output (if user enters "mysecretpass"):

Enter your password: mysecretpass
Password is long enough for basic security.
Program ends.

 

Output (if user enters "short"):

Enter your password: short
Program ends.

Explanation: The if statement checks if the password length (strlen(password)) is at least 8 characters. If true, it confirms the password’s length is sufficient. The program continues regardless, printing "Program ends."

 

2. if-else Statement

The if-else statement provides an alternative path when the if condition is false. It’s like choosing between coffee or tea based on availability.

 

Syntax:

if (condition) {
    // Code if condition is true
} else {
    // Code if condition is false
}

 

Basic Example: Check if a Number is Positive or Negative

#include <stdio.h>
int main() {
    int num = -5;
    if (num >= 0) {
        printf("%d is positive or zero.\n", num);
    } else {
        printf("%d is negative.\n", num);
    }
    return 0;
}

 

Output:

-5 is negative.

Explanation: Since num is -5, the condition num >= 0 is false, so the else block executes.

 

Advanced Example: Temperature Alert System

This program checks if the temperature is safe for outdoor activities.

#include <stdio.h>
int main() {
    float temp;
    printf("Enter current temperature (Celsius): ");
    scanf("%f", &temp);
    if (temp >= 15 && temp <= 30) {
        printf("Temperature %.1f°C is ideal for outdoor activities.\n", temp);
    } else {
        printf("Warning: Temperature %.1f°C is not ideal. Stay cautious!\n", temp);
    }
    return 0;
}

 

Output (if user enters 25):

Enter current temperature (Celsius): 25
Temperature 25.0°C is ideal for outdoor activities.

 

Output (if user enters 35):

Enter current temperature (Celsius): 35
Warning: Temperature 35.0°C is not ideal. Stay cautious!

 

3. Nested if-else Statement

Nested if-else statements allow you to check multiple conditions hierarchically, like a decision tree.

Syntax:

if (condition1) {
    if (condition2) {
        // Code if both are true
    } else {
        // Code if condition1 is true but condition2 is false
    }
} else {
    // Code if condition1 is false
}

 

Basic Example: Student Grade Check

#include <stdio.h>
int main() {
    int score = 85;
    if (score >= 60) {
        if (score >= 80) {
            printf("Excellent! You got an A.\n");
        } else {
            printf("Good job! You got a B.\n");
        }
    } else {
        printf("You need to improve. Grade below B.\n");
    }
    return 0;
}

Output:

Excellent! You got an A.

Explanation: The outer if checks if score >= 60. Since it’s true, the inner if checks if score >= 80. Since 85 satisfies both, the program prints an A grade.

 

Advanced Example: Loan Eligibility System

This program determines loan eligibility based on credit score and income.

#include <stdio.h>
int main() {
    int credit_score;
    float annual_income;
    printf("Enter your credit score (300-850): ");
    scanf("%d", &credit_score);
    printf("Enter your annual income ($): ");
    scanf("%f", &annual_income);
    
    if (credit_score >= 700) {
        if (annual_income >= 50000) {
            printf("Congratulations! You are eligible for a premium loan.\n");
        } else {
            printf("You qualify for a standard loan due to lower income.\n");
        }
    } else {
        if (credit_score >= 600) {
            printf("You may qualify for a limited loan.\n");
        } else {
            printf("Sorry, you are not eligible for a loan at this time.\n");
        }
    }
    return 0;
}

 

Output (if user enters 750, 60000):

Enter your credit score (300-850): 750
Enter your annual income ($): 60000
Congratulations! You are eligible for a premium loan.

 

Output (if user enters 650, 40000):

Enter your credit score (300-850): 650
Enter your annual income ($): 40000
You may qualify for a limited loan.

 

Explanation: The outer if checks if credit_score >= 700. If true, it checks annual_income >= 50000 to decide between premium or standard loans. If the credit score is below 700, it checks if it’s at least 600 for a limited loan, otherwise denies eligibility.

 

4. Ladder if (else-if Ladder)

The else-if ladder checks multiple conditions sequentially, stopping at the first true condition. It’s like a flowchart with multiple branches.

Syntax:

if (condition1) {
    // Code for condition1
} else if (condition2) {
    // Code for condition2
} else if (condition3) {
    // Code for condition3
} else {
    // Code if no condition is true
}

 

Basic Example: Traffic Light System

#include <stdio.h>
int main() {
    char color = 'R';
    if (color == 'R') {
        printf("Stop! Red light.\n");
    } else if (color == 'Y') {
        printf("Caution! Yellow light.\n");
    } else if (color == 'G') {
        printf("Go! Green light.\n");
    } else {
        printf("Invalid light color.\n");
    }
    return 0;
}

 

Output:

Stop! Red light.

 

Explanation: The program checks the value of color. Since it’s 'R', the first if block executes, and the rest are skipped.

 

Advanced Example: Restaurant Discount System

This program applies discounts based on the bill amount and customer type.

#include <stdio.h>
int main() {
    float bill_amount;
    char customer_type; // R = Regular, V = VIP, S = Student
    printf("Enter bill amount ($): ");
    scanf("%f", &bill_amount);
    printf("Enter customer type (R for Regular, V for VIP, S for Student): ");
    scanf(" %c", &customer_type);
    
    float discount = 0;
    if (bill_amount > 200 && customer_type == 'V') {
        discount = bill_amount * 0.20; // 20% for VIPs over $200
        printf("VIP Discount: $%.2f\n", discount);
    } else if (bill_amount > 100 && customer_type == 'R') {
        discount = bill_amount * 0.10; // 10% for Regulars over $100
        printf("Regular Discount: $%.2f\n", discount);
    } else if (bill_amount > 50 && customer_type == 'S') {
        discount = bill_amount * 0.15; // 15% for Students over $50
        printf("Student Discount: $%.2f\n", discount);
    } else {
        printf("No discount applicable.\n");
    }
    printf("Final bill after discount: $%.2f\n", bill_amount - discount);
    return 0;
}

 

Output (if user enters 250, 'V'):

Enter bill amount ($): 250
Enter customer type (R for Regular, V for VIP, S for Student): V
VIP Discount: $50.00
Final bill after discount: $200.00

 

Output (if user enters 80, 'R'):

Enter bill amount ($): 80
Enter customer type (R for Regular, V for VIP, S for Student): R
No discount applicable.
Final bill after discount: $80.00

 

Explanation: The ladder checks conditions in order:

  • VIPs with bills over $200 get a 20% discount.

  • Regulars with bills over $100 get a 10% discount.

  • Students with bills over $50 get a 15% discount. If none apply, no discount is given. The final bill reflects the discount.

 

5. switch Case

The switch statement is ideal for selecting one option from multiple possible values of a variable, like choosing a menu item.

 

Syntax:

switch (expression) {
    case value1:
        // Code
        break;
    case value2:
        // Code
        break;
    default:
        // Code if no case matches
}

 

Basic Example: Month Name Display

#include <stdio.h>
int main() {
    int month = 4;
    switch (month) {
        case 1:
            printf("January\n");
            break;
        case 2:
            printf("February\n");
            break;
        case 3:
            printf("March\n");
            break;
        case 4:
            printf("April\n");
            break;
        default:
            printf("Invalid month\n");
    }
    return 0;
}

 

Output:

April

 

Explanation: The value of month (4) matches case 4, so "April" is printed. The break ensures the program exits the switch block.

 

Advanced Example: Calculator with Error Handling

This program creates a simple calculator using switch with input validation.

#include <stdio.h>
int main() {
    float num1, num2, result;
    char operator;
    printf("Enter two numbers (e.g., 5 3): ");
    scanf("%f %f", &num1, &num2);
    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &operator);
    
    switch (operator) {
        case '+':
            result = num1 + num2;
            printf("%.2f + %.2f = %.2f\n", num1, num2, result);
            break;
        case '-':
            result = num1 - num2;
            printf("%.2f - %.2f = %.2f\n", num1, num2, result);
            break;
        case '*':
            result = num1 * num2;
            printf("%.2f * %.2f = %.2f\n", num1, num2, result);
            break;
        case '/':
            if (num2 != 0) {
                result = num1 / num2;
                printf("%.2f / %.2f = %.2f\n", num1, num2, result);
            } else {
                printf("Error: Division by zero is not allowed.\n");
            }
            break;
        default:
            printf("Error: Invalid operator.\n");
    }
    return 0;
}

 

Output (if user enters 10, 5, '+'):

Enter two numbers (e.g., 5 3): 10 5
Enter operator (+, -, *, /): +
10.00 + 5.00 = 15.00

 

Output (if user enters 8, 0, '/'):

Enter two numbers (e.g., 5 3): 8 0
Enter operator (+, -, *, /): /
Error: Division by zero is not allowed.

 

Explanation: The switch evaluates the operator. For +, -, *, it performs the calculation. For /, it checks for division by zero to avoid errors. The default case handles invalid operators. The %.2f format ensures two decimal places in output.

 

Why Decision-Making Statements Are Powerful

Decision-making statements let your program:

  • Respond dynamically to user inputs or data.

  • Handle complex logic with nested conditions or ladders.

  • Create user-friendly applications like calculators or eligibility systems.

 

Pro Tip: Combine these statements to build real-world projects, like:

  • A quiz app using switch for question selection.

  • A billing system with discounts using else-if ladders.

  • A game with nested conditions for player decisions.

Try tweaking these examples (e.g., add more cases to the calculator or new discount rules) to deepen your understanding. Happy coding!

 


Online - Chat Now
Let’s Connect

Inquiry Sent!

Your message has been successfully sent. We'll get back to you soon!

iKeySkills Logo