🇯🇵 日本語 | 🇺🇸 English

Quiz (if)

Interactive quiz on C if statements.

Question 1 — Behavior of if(a==10)

int a = 10;
if (a == 10) {
  printf("It's A\n");
  a = 20;
}
printf("a = %d\n", a);

What does this print?

It's A
a = 20
It's A
a = 10
a = 20
Explanation: a==10 is true → the if body runs → "It's A" is printed → a is set to 20 → the final printf shows the current value (20).

Question 2 — Pitfall of if(a=10)

int a = 3;
if (a = 10) {    // = is assignment! (meant ==)
  printf("entered\n");
}
printf("a = %d\n", a);

What does this print?

a = 3 (the if body is skipped)
entered
a = 10
Compile error
Explanation: a = 10 is an assignment expression, and its value is 10 (non-zero, so "true"). The if body therefore runs, and a is now 10, so the final print shows a = 10.
C doesn't treat this as an error by default (you'll get a warning at best), which is why it's a classic source of bugs.

Question 3 — Combining AND and OR

int x = 5;
if (x > 0 && x < 10 || x == 100) {
  printf("OK\n");
}

What does this print?

OK
(nothing)
Compile error
Explanation: && has higher precedence than ||, so this is effectively (x>0 && x<10) || x==100.
With x=5: (5>0 && 5<10) is true → the whole expression is true → "OK" is printed.
When you're unsure about precedence, add parentheses to make the intent clear.

Question 4 — The basics of if-else

int n = 7;
if (n > 5) {
  printf("big\n");
} else {
  printf("small\n");
}

What does this print?

big
small
big
small (both)
(nothing)
Explanation: If the condition is true, the if block runs; otherwise the else block runs — exactly one of the two is executed.
n=7 makes n > 5 true → "big" is printed, and the else branch is skipped.

Question 5 — else if ladder

Check several conditions in order. Only the first branch that's true runs.
int score = 75;
if (score >= 90) {
  printf("A\n");
} else if (score >= 70) {
  printf("B\n");
} else if (score >= 50) {
  printf("C\n");
} else {
  printf("D\n");
}

What does this print?

A
B
C
B
C (both match)
Explanation: In an else-if ladder, evaluation stops as soon as a branch is true.
score=75 is not ≥ 90 → the next check >=70 is true → "B" is printed → the remaining conditions are never evaluated.
Conditions can overlap (they're evaluated top-down), so by convention you put the strictest one first.

Question 6 — if without braces (a trap)

Without braces { }, how far does the if's "reach" extend?
int a = 5;
if (a > 10)
  printf("big\n");
  printf("always\n");   // beware the misleading indentation!

What does this print?

always (only one line)
(nothing)
big
always
big
Explanation: Without braces, an if conditionally executes only the single statement that immediately follows it.
With a=5, the condition is false → printf("big"); is skipped.
The second printf("always"); sits outside the if, so it always runs — "always" is printed (don't let the indentation fool you!).
Takeaway: Always use braces, even when the body is a single statement. That way, adding a second line later won't silently introduce bugs.

Question 7 — Comparing floating-point with ==

Mathematically, 0.1 + 0.2 = 0.3. Is that still true in C?
double x = 0.1 + 0.2;
if (x == 0.3) {
  printf("equal\n");
} else {
  printf("not equal\n");
}

What does this print?

equal
not equal
Compile error
Explanation: Floating-point numbers are stored in binary, so 0.1 and 0.2 can't be represented exactly.
The actual value of 0.1 + 0.2 comes out to 0.30000000000000004..., which isn't exactly 0.3. Result: not equal.
Takeaway: Don't compare doubles with ==. Instead, treat two values as equal when the absolute difference is small (e.g. fabs(x - 0.3) < 1e-9).

Question 8 — Short-circuit evaluation (guarding against division by zero)

When the left side of && is false, the right side isn't evaluated. Can we use that to avoid dividing by zero?
int a = 10, b = 0;
if (b != 0 && a / b > 5) {
  printf("OK\n");
} else {
  printf("skip\n");
}

What happens?

"OK" is printed
"skip" is printed (no division by zero occurs)
Crashes with a division-by-zero error
Compile error
Explanation: && uses short-circuit evaluation. As soon as the left side b != 0 is false, the right side a / b isn't evaluated at all.
No division by zero happens, so the else branch prints "skip".
Pattern: Writing "safety check && main condition" creates a guard that prevents the error.
Note: if you flip the order to a / b > 5 && b != 0, the division happens first and you'll hit a division-by-zero error. Always put the safety check first.

Question 9 — What if no branch matches and there's no else?

With only if / else if (no final else), what happens when none of the conditions is true?
int x = 100;
if (x < 0) {
  printf("negative\n");
} else if (x < 10) {
  printf("small\n");
} else if (x < 50) {
  printf("medium\n");
}
printf("done\n");

What does this print?

medium
done
done
(nothing)
Compile error
Explanation: x=100 doesn't satisfy any of the conditions (<0, <10, <50). Without an else, the if block simply does nothing and falls through.
The next printf("done"); sits outside the if, so it always runs — only "done" is printed.
Takeaway: If you need a "none of the above" case, write an else. Leaving it off means "do nothing when nothing matches".

Question 10 — Independent ifs vs else if

These two patterns look similar but behave differently. Compare the two snippets below.
// Pattern A: connected with else if
int n = 50;
if (n > 0) {
  printf("positive\n");
} else if (n > 30) {
  printf("large\n");
}

// Pattern B: two independent ifs
if (n > 0) {
  printf("positive\n");
}
if (n > 30) {
  printf("large\n");
}

What does each print when n=50?

A: positive / B: positive
A: positive / B: positive
large
A: positive
large / B: positive
large
A: positive
large / B: positive
Explanation: This is the key difference between else if and independent ifs.
Pattern A (else if): Only the first true branch runs. n=50 makes n>0 true → prints "positive" and stops (n>30 is never checked).
Pattern B (two independent ifs): Each one is evaluated independently. n>0 is true → "positive"; then n>30 is also true → "large". Both lines are printed.
Which should you use? "Run exactly one" → else if. "Check both independently" → separate ifs.

Question 11 — The final else is a "catch-all"

The final else in an else-if ladder runs whenever every earlier condition was false.
int grade = -5;
if (grade >= 90) {
  printf("Excellent\n");
} else if (grade >= 70) {
  printf("Good\n");
} else if (grade >= 50) {
  printf("Pass\n");
} else {
  printf("Fail\n");
}

What does it print when grade=-5 (a negative value)?

Excellent
Pass
(nothing)
Fail
Explanation: -5 fails >=90, >=70, and >=50, so the final else runs and "Fail" is printed.
The final else catches everything that didn't match any earlier condition — it's the catch-all.
Takeaway: Adding an else gives you peace of mind that unexpected values (like -5 or 999) will still be handled somewhere.

Question 12 — dangling else (which if does the else belong to?)

When braces are omitted, which if does the else actually belong to?
int a = 5, b = 10;
if (a > 0)
  if (b > 20)
    printf("both big\n");
  else
    printf("b is small\n");

What does this print? (indentation is irrelevant)

both big
b is small
(nothing)
Compile error
Explanation: The rule is that else binds to the nearest if (the dangling-else rule).
The indentation makes it look like the else belongs to the outer if, but it's actually the else of the inner if (b > 20).
a=5 (so a > 0 is true) → enter the inner if → b=10 makes b > 20 false → the else branch prints "b is small".
Takeaway: With nested ifs, always use braces. They eliminate the ambiguity entirely.

Question 13 — A grade-classification bug (multiple ifs vs else if)

We want to classify a score as A / B / C. Here's a classic bug.
// Buggy code: three independent ifs
int score = 85;
if (score >= 90) {
  printf("A\n");
}
if (score >= 70) {
  printf("B\n");
}
if (score >= 50) {
  printf("C\n");
}

What does it print when score=85?

A
B
B
C
A
B
C
Explanation: Because the ifs are independent, every block whose condition is true runs.
With score=85: ≥90 is false → skipped / ≥70 is true → "B" / ≥50 is also true → "C". Result: both B and C are printed.

Correct form (use else if):
if (score >= 90) {
  printf("A\n");
} else if (score >= 70) {
  printf("B\n");
} else if (score >= 50) {
  printf("C\n");
}
Now the else-if chain runs only the first matching branch, so score=85 prints just "B".

How to choose:
  • Conditions are mutually exclusive (only one should apply) → use else if.
  • Conditions are independent (both can be true and both should run) → use separate ifs.

Related lessons

Conditionals
if statement
How C if statements work, visualized with flowcharts.
Operators
Comparison & logical operators
Comparison (==, !=, <, >) and logical (&&, ||, !) operators in C.
← Previous lesson
Lesson 13: if Statement
Next lesson →
Lesson 15: switch