⚡0 XP🔥0

Decision Making - if, else and switch

Lesson 5 of 13 ⏱ 4 min read ✎ 3 exercises ❓ 4-question quiz ▶ Runs C

Make decisions with if, if-else, else-if ladders, nested if and the switch statement, with real examples like grades and calculators.

Programs often need to choose: if the marks are above 40, print Pass, otherwise print Fail. C gives you if, else, else if and switch for this.

The if statement#

if (condition) {
    // runs only when the condition is true (non-zero)
}
▶ Try it YourselfC
Ctrl + Enter to run
Output
Heat alert! Drink water.
Have a good day.

if-else#

▶ Try it YourselfC
Ctrl + Enter to run
Output
Result: Fail

Change the input to 72 and run again.

The else-if ladder#

When there are more than two choices, chain them. C checks the conditions top to bottom and runs only the first block whose condition is true.

▶ Try it YourselfC
Ctrl + Enter to run
Output
Units: 250, Bill: Rs 1100.00
हिंदी में समझें

else-if ladder में ऊपर से नीचे conditions check होती हैं। जैसे ही कोई एक condition सच मिलती है, उसका block चलता है और बाकी सब छोड़ दी जाती हैं। इसलिए conditions का क्रम (order) सही रखना ज़रूरी है - बड़ी range पहले या छोटी पहले, सोच कर लिखें।

Nested if#

An if inside another if:

▶ Try it YourselfC
Ctrl + Enter to run
Output
Largest: 61
Common Mistake

Without braces, an else attaches to the nearest if (the "dangling else" problem). Always use { } for nested ifs - it costs nothing and prevents bugs.

The switch statement#

switch compares one integer (or char) value against fixed case labels. It is cleaner than a long else-if ladder when you check for exact values.

switch (expression) {
    case value1:  statements; break;
    case value2:  statements; break;
    default:      statements;     // when no case matches
}
▶ Try it YourselfC
Ctrl + Enter to run
Output
135.00

Fall-through: grouping cases#

If a case has no break, execution continues into the next case. Sometimes that is exactly what you want:

▶ Try it YourselfC
Ctrl + Enter to run
Output
e is a vowel

if-else vs switch#

Use if-else when... Use switch when...
you test ranges (marks >= 60) you test exact values (case 1, case 'a')
conditions involve floats or several variables one integer/char variable is checked
conditions are complex (&&, ||) there are many fixed options (menus)

Summary#

  • if (cond) runs a block when cond is non-zero; else handles the opposite.
  • An else-if ladder runs only the first true branch.
  • switch matches exact integer/char values; remember break, and use default.
  • Always use braces with nested ifs.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Grade calculator

Read marks (0-100) and print the grade using an else-if ladder: 90+ → A+, 75-89 → A, 60-74 → B, 40-59 → C, below 40 → Fail. Print in the form Grade: B. Test input: 68.

Test input:
68
Expected output:
Grade: B
2

Leap year

A year is a leap year if it is divisible by 4 and not by 100, or divisible by 400. Read a year and print 2024 is a leap year or 1900 is not a leap year. Test input: 1900.

Test input:
1900
Expected output:
1900 is not a leap year
3

Day name with switch

Read a number 1-7 and print the day name (1 → Monday ... 7 → Sunday) using switch. Any other number prints Invalid day. Test input 6.

Test input:
6
Expected output:
Saturday

Quick Quiz

Q1.

In C, which values count as "true" in an if condition?

Q2.

What happens if you forget break in a switch case?

Q3.

Which type can you NOT use directly in a switch expression?

Q4.

What is printed? int x = 5; if (x > 2) if (x > 8) printf("A"); else printf("B");