Path tracer Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

The ideas behind the lab

Exactly what you watched happen, in plain steps.

The shape of an if statement

if is followed by a condition in parentheses and then ONE statement — or a block of statements in { }. The condition is worked out; if it is true (non-zero) the statement runs, otherwise it is skipped. With else, the second statement runs when the condition is false. Afterwards the program continues with the next line.

if (condition)
    statement;              // runs only if true

if (condition) {
    statement1;             // a block: several statements
    statement2;
} else {
    statement3;             // runs only if false
}
Forms of the if statement
Form Example What runs
if if (marks >= 40) printf("Pass"); the statement, only when the condition is true
if with a block if (x < 0) { x = -x; count++; } all statements in { }, only when true
if-else if (n % 2 == 0) puts("even"); else puts("odd"); exactly one of the two
if-else with blocks if (a > b) { … } else { … } exactly one of the two blocks
conditional operator max = a > b ? a : b; a short if-else that gives a value

if

  1. The condition goes in parentheses: if (marks >= 40).
  2. Any expression can be a condition: 0 is false, anything else is true.
  3. Without braces, the if controls only the ONE next statement.
  4. With braces { }, it controls the whole block.
  5. After the if, the program continues with the next statement either way.

if-else

  1. else means "otherwise": it runs when the condition is false.
  2. Exactly one of the two branches runs — never both, never neither.
  3. else belongs to the if just before it; it cannot stand alone.
  4. Both branches can be single statements or blocks.
  5. For "choose a value" the conditional operator ?: is a short alternative.

Good habits

  1. Always use braces, even for one statement — it prevents the "missing braces" bug.
  2. Never put a ; right after if ( … ).
  3. Compare with ==, not =. Compile with -Wall -Wextra.
  4. Indent the branches so the structure is visible (C itself ignores indentation).
  5. Write ranges with &&: if (age >= 13 && age <= 19).
Walkthrough

Three everyday decisions

Every line, explained.

1 #include <stdio.h>
2
3 int main(void)
4 {
5 int temp = 31;
6 if (temp > 30) { 31 > 30 is true, so the whole block runs.
7 printf("Hot day\n"); Prints Hot day
8 printf("Drink water!\n"); Prints Drink water! — also inside the block.
9 }
10 int n = -8;
11 if (n < 0) A negative number…
12 n = -n; …is made positive: the absolute value, 8.
13 int a = 12, b = 19, max;
14 if (a > b) 12 > 19 is false…
15 max = a; …so this is skipped…
16 else
17 max = b; …and the else runs: max is 19.
18 printf("%d %d\n", n, max); Prints 8 19
19 return 0;
20 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

A semicolon after the condition

✗ Wrong
if (x > 5);
    printf("big");
✓ Right
if (x > 5)
    printf("big");

The ; is an empty statement that becomes the body of the if. printf then always runs.

Missing braces

✗ Wrong
if (x > 5)
    printf("big");
    x = 0;
✓ Right
if (x > 5) {
    printf("big");
    x = 0;
}

Without braces only the first statement belongs to the if; x = 0 always runs, whatever the indentation says.

= instead of ==

✗ Wrong
if (x = 0)
✓ Right
if (x == 0)

x = 0 sets x to 0 and is always false. gcc -Wall warns: suggest parentheses around assignment used as truth value.

A semicolon before else

✗ Wrong
if (x > 0) {
    puts("positive");
};
else {
    puts("not positive");
}
✓ Right
if (x > 0) {
    puts("positive");
} else {
    puts("not positive");
}

The ; after } ends the if statement, so the else has no if: "error: 'else' without a previous 'if'".

A range with || instead of &&

✗ Wrong
if (age >= 13 || age <= 19)   // always true
✓ Right
if (age >= 13 && age <= 19)

Every age is either ≥ 13 or ≤ 19, so || is always true. Both conditions must hold: &&.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

int x = 4; if (x > 5) printf("A"); printf("B"); — what is printed?

Question 2

Which runs when the condition of an if-else is false?

Question 3

Is if (5) true or false?

Question 4

Without braces, how many statements does an if control?

Question 5

What does if (n = 5) printf("five"); do when n is 2?

Assessment

if and if-else quiz

Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.