🇯🇵 日本語 | 🇺🇸 English
Advertisement

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 becomes 20 → the final printf shows the current value of a (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 whose value is 10 (non-zero = true). So the if body executes, and a is now 10, making the final print show a = 10.
C does not make this an error by default (only a warning at best) — a classic bug source.

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 → overall expression is true → "OK" prints.
When in doubt about precedence, add parentheses to make your intent clear.
Advertisement

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 11: if Statement
Next lesson →
Lesson 13: else if & switch