Conditional Statements

Loading concept...

🚦 The Traffic Light of Code: Conditional Statements in C

Imagine you’re at a crossroads. Left leads to the park, right leads to school, straight goes home. How do you decide? You check conditions! “Is it Saturday? Go to the park. Is it a school day? Go to school.”

Your C programs work exactly the same way. They face crossroads too, and conditional statements are their traffic lights — telling the code which path to take.


🎯 What You’ll Learn

  • if statement — The basic yes/no decision
  • if-else statement — Either this OR that
  • Nested if and else-if — Multiple choices
  • switch statement — The menu selector

🟢 The if Statement — “Should I Do This?”

Think of if like asking a simple question before doing something.

Real Life Example:

“If it’s raining, take an umbrella.”

You only take the umbrella if the condition is true. If it’s sunny? You do nothing extra.

How It Looks in C

if (condition) {
    // Do this only if true
}

Example: Is the Number Positive?

int number = 10;

if (number > 0) {
    printf("Positive!\n");
}

What happens:

  1. C checks: Is 10 > 0? YES!
  2. Since it’s true, it prints “Positive!”

Visual Flow

graph TD A[Start] --> B{Is number > 0?} B -->|Yes| C[Print Positive] B -->|No| D[Do nothing] C --> E[End] D --> E

Key Point: If the condition is false, C simply skips the code inside the curly braces {} and moves on.


🔴🟢 The if-else Statement — “This OR That”

Sometimes doing nothing isn’t enough. You need a Plan B!

Real Life Example:

“If it’s raining, take an umbrella. Otherwise, wear sunglasses.”

Now you always do something — either umbrella or sunglasses.

How It Looks in C

if (condition) {
    // Do this if true
} else {
    // Do this if false
}

Example: Pass or Fail?

int marks = 45;

if (marks >= 50) {
    printf("You passed!\n");
} else {
    printf("Try again!\n");
}

What happens:

  1. C checks: Is 45 >= 50? NO!
  2. Since it’s false, C jumps to else
  3. Prints “Try again!”

Visual Flow

graph TD A[Start] --> B{marks >= 50?} B -->|Yes| C[Print: You passed!] B -->|No| D[Print: Try again!] C --> E[End] D --> E

Remember: With if-else, exactly one block always runs. Never both, never neither.


🌳 Nested if and else-if — “Many Roads to Choose”

What if you have MORE than two options? Like a restaurant menu with multiple dishes?

Real Life Example:

“If you want pizza, go to counter 1. Else if you want burger, go to counter 2. Else if you want ice cream, go to counter 3. Otherwise, go home hungry.”

The else-if Ladder

if (condition1) {
    // Path 1
} else if (condition2) {
    // Path 2
} else if (condition3) {
    // Path 3
} else {
    // Default path
}

Example: Grade Calculator

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 if (score >= 60) {
    printf("Grade D\n");
} else {
    printf("Grade F\n");
}

What happens with score = 75:

  1. Is 75 >= 90? No → skip
  2. Is 75 >= 80? No → skip
  3. Is 75 >= 70? YES! → prints “Grade C”
  4. All remaining conditions are skipped

Visual Flow

graph TD A[Start] --> B{score >= 90?} B -->|Yes| C[Grade A] B -->|No| D{score >= 80?} D -->|Yes| E[Grade B] D -->|No| F{score >= 70?} F -->|Yes| G[Grade C] F -->|No| H{score >= 60?} H -->|Yes| I[Grade D] H -->|No| J[Grade F]

Nested if — Decisions Inside Decisions

You can put an if inside another if. Like asking follow-up questions!

Example: Is the Number Positive AND Even?

int num = 8;

if (num > 0) {
    printf("Positive ");
    if (num % 2 == 0) {
        printf("and Even!\n");
    } else {
        printf("and Odd!\n");
    }
} else {
    printf("Not positive\n");
}

Output: Positive and Even!

Think of it like:

  1. First door: “Is it positive?” → Yes, enter
  2. Second door inside: “Is it even?” → Yes!

🎛️ The switch Statement — “Pick a Number, Any Number”

Imagine a vending machine. Press 1 for chips, 2 for candy, 3 for soda. Each button leads to ONE specific item.

switch is perfect when you’re checking one variable against many specific values.

How It Looks in C

switch (expression) {
    case value1:
        // Do something
        break;
    case value2:
        // Do something else
        break;
    default:
        // If nothing matches
}

Example: Day of the Week

int day = 3;

switch (day) {
    case 1:
        printf("Monday\n");
        break;
    case 2:
        printf("Tuesday\n");
        break;
    case 3:
        printf("Wednesday\n");
        break;
    case 4:
        printf("Thursday\n");
        break;
    case 5:
        printf("Friday\n");
        break;
    default:
        printf("Weekend!\n");
}

Output: Wednesday

🚨 The break — Your Emergency Exit

SUPER IMPORTANT: Without break, C doesn’t stop! It “falls through” to the next case.

int num = 1;

switch (num) {
    case 1:
        printf("One\n");
        // No break here!
    case 2:
        printf("Two\n");
        break;
}

Output:

One
Two

Why? No break after case 1, so C keeps going!

Visual Flow

graph TD A[Start] --> B{Check day} B -->|1| C[Monday] B -->|2| D[Tuesday] B -->|3| E[Wednesday] B -->|4| F[Thursday] B -->|5| G[Friday] B -->|other| H[Weekend!] C --> I[break] D --> I E --> I F --> I G --> I H --> I I --> J[End]

When to Use switch vs if-else?

Use switch when… Use if-else when…
Checking ONE variable Checking ranges
Against exact values Complex conditions
Many options (5+) 2-3 options

🎨 Quick Comparison

// Same logic, two styles

// if-else style
if (color == 1) {
    printf("Red");
} else if (color == 2) {
    printf("Green");
} else if (color == 3) {
    printf("Blue");
}

// switch style
switch (color) {
    case 1: printf("Red"); break;
    case 2: printf("Green"); break;
    case 3: printf("Blue"); break;
}

Both work! switch is cleaner for multiple exact matches.


đź§  The Big Picture

Think of your program as a story with choices:

  • if = “Only do this if something special happens”
  • if-else = “Do this or that, but definitely one”
  • else-if = “Choose from a menu of options”
  • switch = “A vending machine with numbered buttons”

⚡ Golden Rules

  1. Always use {} braces — Even for one line. Prevents bugs!
  2. Order matters in else-if — First true condition wins
  3. Don’t forget break in switch — Unless you want fall-through
  4. default is your safety net — Catches unexpected values

🎯 Real World Analogy Recap

C Statement Real Life
if “If door is open, enter”
if-else “If door open, enter. Else, knock”
else-if “Check each room until you find the right one”
switch “Press elevator button for your floor”

You’ve just learned how to make your programs think and decide! 🎉

Every app, game, and website uses these building blocks. When a game checks “Did the player win?”, when Netflix asks “Are you still watching?”, when your phone decides “Should I show a notification?” — it’s all conditional statements!

Now YOUR code can make smart decisions too. 🚀

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.