Day 4 Progress
0%
Day 4  ·  1 Hour

Conditionals — if, else, switch

Make your programs smart — execute different code based on conditions, comparisons, and choices.

0–15 min · Comparison Operators
15–35 min · if / else if / else
35–50 min · switch-case
50–60 min · Quiz
1

Comparison Operators

0 – 15 min

Before writing conditions, you need to know comparison operators — these compare two values and return either true (1) or false (0). Every if statement uses these.

OperatorMeaningExampleResult
==Equal to5 == 5true (1)
!=Not equal to5 != 3true (1)
>Greater than7 > 4true (1)
<Less than3 < 9true (1)
>=Greater than or equal5 >= 5true (1)
<=Less than or equal4 <= 6true (1)
&&AND — both must be true5>2 && 3<8true (1)
||OR — at least one true5>9 || 3<8true (1)
!NOT — reverses result!(5==5)false (0)
⚠️ = vs == — most common C mistake!
if (x = 5) — assigns 5 to x, always evaluates as TRUE — BUG!
if (x == 5) — checks if x equals 5 — correct!
The compiler will not warn you. This bug is very hard to find.
2

if / else if / else

15 – 35 min

The if statement runs a block of code only when a condition is true. Chain multiple conditions using else if. The final else catches everything that did not match.

  • if — checks first condition
  • else if — checks next condition only if previous was false
  • else — runs when nothing above matched
  • Only one block ever runs — as soon as one condition is true, the rest are skipped

if / else if / else — execution flow

marks >= 90?
YES
Grade A
NO →
marks >= 75?
↓ YES
Grade B
NO →
Grade F
grade.c
C
#include <stdio.h>

int main() {
    int marks;
    printf("Enter your marks (0-100): ");
    scanf("%d", &marks);

    if (marks >= 90) {
        printf("Grade A — Excellent!\n");
    } else if (marks >= 75) {
        printf("Grade B — Good!\n");
    } else if (marks >= 50) {
        printf("Grade C — Average\n");
    } else if (marks >= 0) {
        printf("Grade F — Failed\n");
    } else {
        printf("Invalid marks!\n");
    }

    return 0;
}
nested_if.c — even/odd & positive/negative
C
#include <stdio.h>

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);

    // Nested if — if inside an if
    if (n > 0) {
        printf("Positive ");
        if (n % 2 == 0)
            printf("and Even\n");
        else
            printf("and Odd\n");
    } else if (n < 0) {
        printf("Negative number\n");
    } else {
        printf("Zero\n");
    }

    return 0;
}
💡 Curly braces { } are optional when only ONE statement follows if/else — but always use them anyway. Skipping braces is a common source of hard-to-find bugs when you add code later.
switch-case
3

switch-case Statement

35 – 50 min

switch is cleaner than a long chain of else if when you check one variable against many exact values. Each case is one possible value. Always end with break — otherwise execution falls through to the next case!

  • switch(x) — evaluates x once
  • case n: — matches if x equals n
  • break; — exits the switch (required!)
  • default: — runs when no case matches (like else)
switch_day.c
C
#include <stdio.h>

int main() {
    int day;
    printf("Enter day (1-7): ");
    scanf("%d", &day);

    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;
        case 6: printf("Saturday\n");  break;
        case 7: printf("Sunday\n");    break;
        default: printf("Invalid!\n");
    }
    return 0;
}
switch_fallthrough.c — intentional fall-through
C
#include <stdio.h>

int main() {
    int month = 4;  // April

    switch (month) {
        // Multiple cases sharing same action (no break between)
        case 1: case 3: case 5:
        case 7: case 8: case 10: case 12:
            printf("31 days\n");
            break;
        case 4: case 6: case 9: case 11:
            printf("30 days\n");
            break;
        case 2:
            printf("28 or 29 days\n");
            break;
        default:
            printf("Invalid month\n");
    }
    return 0;
}
⚠️ Forgetting break causes fall-through!
Without break, execution continues into the next case automatically — no check needed. This is usually a bug. The only safe intentional use is grouping cases like case 4: case 6: case 9: shown above.
Featureif / else ifswitch
Best forRanges and complex conditionsExact single-value matches
Condition typeAny expressionInteger or char only
Multiple conditionsUse && and ||Multiple case labels
Default caseelsedefault:
ReadabilityGood for 2–3 conditionsBetter for 4+ exact values
practice & quiz
Q

Quick Quiz

55–60 min
Question 1 of 4

What does if (x = 10) do in C?

Question 2 of 4

In an if/else if/else chain, how many blocks can run?

Question 3 of 4

What happens if you forget break in a switch case?

Question 4 of 4

When should you use switch instead of if/else if?

Lesson Checklist

  • I know all 6 comparison operators and 3 logical operators
  • I understand the difference between = (assign) and == (compare)
  • I can write if / else if / else chains correctly
  • I understand nested if — if inside an if
  • I can write a switch-case with break and default
  • I understand fall-through and when it is useful
  • I know when to use switch vs if-else
  • I completed the quiz

Day 5 Preview

Coming up next
  • 🔁 for loop — counted repetition Day 5
  • 🔄 while loop — condition-driven repetition Day 5
  • 🔃 do-while loop — runs at least once Day 5
  • break & continue — control loop flow Day 5