Skip to main content
C Programming Basics
CHAPTER 07 Beginner

Conditional Statements

Updated: May 17, 2026
5 min read

# CHAPTER 7

Conditional Statements

1. Introduction

Programs need to make decisions. Should a user be granted access? Is the player's health zero? Conditional statements allow a C program to choose different paths of execution based on specific conditions, creating intelligent software.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Use the if statement to run code conditionally.
  • Use if-else for two-way branching.
  • Chain conditions using else if ladders.
  • Use switch-case for multi-way branching.
  • Write concise conditions using the Ternary Operator.

3. The if Statement

Executes a block of code ONLY if the condition evaluates to true (non-zero).
c
12345
int age = 20;

if (age >= 18) {
    printf("You are an adult.\n");
}

4. The if-else Statement

Provides an alternative block of code to run if the condition is false (zero).
c
1234567
int temperature = 35;

if (temperature > 30) {
    printf("It's hot outside!\n");
} else {
    printf("The weather is pleasant.\n");
}

5. The else if Ladder

Used to check multiple conditions in sequence. As soon as one condition is true, its block executes, and the rest are skipped.
c
123456789101112
int score = 75;

if (score >= 90) {
    printf("Grade: A\n");
} else if (score >= 80) {
    printf("Grade: B\n");
} else if (score >= 70) {
    printf("Grade: C\n");
} else {
    printf("Grade: F\n");
}
// Output: Grade: C

6. Nested if Statements

Placing an if statement inside another if statement.
c
123456789101112
int age = 25;
int has_license = 1; // 1 means true

if (age >= 18) {
    if (has_license == 1) {
        printf("You can drive.\n");
    } else {
        printf("You need a license first.\n");
    }
} else {
    printf("You are too young to drive.\n");
}

7. The switch-case Statement

The switch statement is a cleaner alternative to a long else if ladder when comparing a single variable against exact values.
c
123456789101112131415
int day = 3;

switch (day) {
    case 1:
        printf("Monday\n");
        break; // Exits the switch block
    case 2:
        printf("Tuesday\n");
        break;
    case 3:
        printf("Wednesday\n");
        break;
    default: // Runs if no cases match
        printf("Weekend!\n");
}

Important: Without the break keyword, execution "falls through" and runs all subsequent cases!

8. The Ternary Operator (?:)

A shorthand for a simple if-else statement. Syntax: condition ? valueiftrue : valueiffalse;
c
123
int age = 20;
// If age >= 18, assign 1 to is_adult, else assign 0.
int is_adult = (age >= 18) ? 1 : 0; 

9. Mini Project: Grade Calculator

c
12345678910111213141516171819202122232425
#include <stdio.h>

int main() {
    int marks;

    printf("=== GRADE CALCULATOR ===\n");
    printf("Enter your marks (0-100): ");
    scanf("%d", &marks);

    if (marks < 0 || marks > 100) {
        printf("Error: Invalid marks entered.\n");
    } else if (marks >= 90) {
        printf("Grade: A+ (Outstanding)\n");
    } else if (marks >= 80) {
        printf("Grade: A (Excellent)\n");
    } else if (marks >= 70) {
        printf("Grade: B (Good)\n");
    } else if (marks >= 60) {
        printf("Grade: C (Pass)\n");
    } else {
        printf("Grade: F (Fail)\n");
    }

    return 0;
}

10. Memory-Level Explanation

At the assembly level, an if statement compiles into a "Compare" (CMP) instruction followed by a "Jump" (JMP, JE, JNE) instruction. If the comparison fails, the CPU alters the Instruction Pointer (IP) register to skip over the code block entirely.

11. Common Mistakes

  • Using = instead of ==:
if (x = 5) assigns 5 to x, which evaluates to true (non-zero). This is a massive logic bug. It must be if (x == 5).
  • Missing break in switch: This causes fall-through, executing unintended code.
  • Floating point in switch: switch only works with integers and characters. You cannot switch on a float.

12. Exercises

  1. 1. Write a program to check if a given year is a leap year.
  1. 2. Build a simple calculator using switch-case that asks the user for an operator (+, -, *, /) and two numbers.
  1. 3. Use the ternary operator to find the maximum of two numbers.

13. MCQ Quiz with Answers

Question 1

What happens if an if condition evaluates to 0?

Question 2

Which keyword handles the "catch-all" scenario in a switch statement?

Question 3

What is the output of if (5 = 5) { printf("Yes"); }?

Q4. Can you use a float variable in a switch expression? a) Yes b) No Answer: b) No
Question 5

What prevents "fall-through" in a switch case?

Question 6

What does (x > y) ? x : y; do?

Q7. Is else mandatory after an if? a) Yes b) No Answer: b) No
Question 8

Which operator is used to compare two values for equality?

Question 9

Can if statements be nested inside other if statements indefinitely?

Question 10

In C, what value represents 'true'?

14. Interview Questions

  • Q: Explain the difference between if-else if ladders and switch statements. Which is faster?
  • Q: What is the dangling else problem?
  • Q: Why is it considered dangerous to use if (a = b)?

15. Summary

Conditional statements give your program decision-making power. if, else if, and else provide flexible logic evaluation. switch-case offers a cleaner syntax for matching exact integers or characters. The ternary operator is a concise shortcut for simple binary decisions.

16. Next Chapter Recommendation

In Chapter 8: Loops in C, we will learn how to repeat actions automatically using for, while, and do-while loops.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·