STEP 3 Review: if, Comparison, Logic, and switch

Review the key points with a cheat sheet, six code-reading questions, and a checklist of common mistakes.

① Key-point cheat sheet

Review the essentials of STEP 3 in these tables .

🌳 Structure of if / else if / else

if (score >= 80) {
    printf("Excellent\n" );
} else if (score >= 60) {
    printf("Good\n" );
} else {
    printf("Retake\n" );
}
  • Conditions Top to bottom : once a condition is true and its branch executes, the remaining branches are skipped
  • else means when none of the preceding conditions matched

⚖️ Comparison operators

Symbol Meaning Example
==Equal a == 5
!=Not equal a != 5
<Less than a < 5
>Greater than a > 5
<=Less or equal a <= 5
>=Greater or equal a >= 5
  • The result is true (1) or false (0)
  • Confused assignment =with comparison ==are easily confused

🔗 Logical operators: combining conditions

Symbol Meaning Example True when
&&AND a>0 && a<10Both are true
||OR a==0 || a==9At least one is true
!NOT !(a==0)The operand is false
  • At least 0 AND less than 10 is 0 <= a && a < 10
  • the math 0 <= a < 10is cannot be written as a chained comparison

🔀 Structure of switch

switch (n) {
    case 1:
        printf("one\n");
        break; // Required to stop fallthrough! 
    case 2:
        printf("two\n");
        break;
    default:
        printf("other\n");
}
  • Without a break in each case, execution falls through to the next case (fallthrough)
  • When no case matches, use default.
  • For integer types (including int and char), equality matching only is supported

🤔 Choosing if or switch

  • Range conditions ( score >= 80, for example) → if
  • Multiple equality cases (n is 1, 2, or 3) → switch is easier to read
  • switch accepts integers and characters , not floating-point values or strings

② Six code-reading questions

Execute the code mentally before choosing an answer. Read the explanation even if you get it wrong.

Q1. An else-if chain

int score = 85;
if (score >= 90)      printf("S\n");
else if (score >= 80) printf("A\n");
else if (score >= 70) printf("B\n");
else                  printf("C\n");

What is the output?

S
A
A B C
B
Explanation: 85 >= 90 is false; 85 >= 80 is true, so the A branch runs. The remaining branches are skipped, even though later conditions might also be true. Output: A.

Q2. Confusing = with ==

int a = 3;
if (a = 0) printf("A\n");
else        printf("B\n");
printf("a=%d\n", a);

What is the output?

A
a=3
B
a=3
A
a=0
B
a=0
Explanation: a = 0 is assignment: it stores 0 in a, and its value 0 is false. The else branch prints B, then the program prints a=0. For an equality test, write a == 0.

Q3. Combining range conditions

int a = 7;
if (0 <= a && a < 10) printf("in\n");
else                       printf("out\n");

What is the output?

in
out
Nothing
Compilation error
Explanation: With a=7, both 0 <= a and a < 10 are true, so && is true and the output is in. Do not write 0 <= a < 10: C parses that as (0 <= a) < 10.

Q4. Missing break in switch

int n = 2;
switch (n) {
    case 1: printf("A ");
    case 2: printf("B ");
    case 3: printf("C ");
    default: printf("D ");
}

What is the output?

B
B C D
A B C D
B D
Explanation: n=2 starts execution at case 2. Without break, execution falls through case 3 and default, printing B C D. End a case with break; when you want to stop there.

Q5. Which if owns the else?

int a = 0, b = 0;
if (a > 0)
    if (b > 0) printf("both+\n");
    else        printf("a+ b-not+\n" );
else            printf("a not+\n");

What is the output?

both+
a+ b-not+
a not+
Nothing
Explanation: The first condition a>0 is false. Each else belongs to the nearest unmatched if; in this code the final else belongs to the outer if, so the output is a not+. Braces make the intended grouping clear.

Q6. Only default matches

int n = 5;
switch (n) {
    case 1: printf("one"); break;
    case 2: printf("two"); break;
    default: printf("other");
}

What is the output?

one
two
other
Nothing
Explanation: n=5 matches no case, so default executes and prints other. A default case provides a fallback for unmatched values.

③ Common mistakes and tricky points

If an item feels familiar, return to its original lesson and review it.
  1. 1
    Confused assignment =with comparison ==were confused
    if (a = 0)assigns 0 to a, then evaluates as false (0). For comparison, use ==.
  2. 2
    the math 0 ≤ a < 10was written as a chained comparison
    In C, use 0 <= a && a < 10 to express the two comparisons separately. 0 <= a < 10 has a different meaning.
  3. 3
    In a switch case, breakwas omitted
    Execution continues into the next case (fallthrough). This can cause unexpected output. End each case with break;.
  4. 4
    else belonged to an unexpected if
    else means nearest unmatched if owns the else. Omitting braces can create an unintended structure. Always using braces { } makes the structure clear.
  5. 5
    Confused && and || precedence
    && binds more tightly than ||. Thus a==1 || a==2 && b==3 means a==1 || (a==2 && b==3). Use parentheses when unsure.
  6. 6
    Boundary operators <and <=were confused
    Less than differs from less than or equal to. Read the requirement again to choose correctly.
  7. 7
    Passed a floating-point value or string to switch
    The switch expression accepts integer types (including int and char) . Use if for double comparisons or string comparisons.

④ Lessons to revisit when you get stuck

Use these links to return to the relevant explanation.
Lesson 12: if statements Review if / else if / else structure Lesson 13: Comparison and logical operators Review ==, &&, and precedence Lesson 14: if quiz Check understanding with related questions Lesson 15: switch statements Review case and break behavior Cheat sheet Review operator precedence on one page