Skip to the task
Layout
Back to classic view

You are looking at lesson 3. Your place is still lesson 2, task 1: Types.

READTask 1 of 22 · step 1: Conditions: if, else and switch

Conditions: if, else and switch

Comparisons, &&, || and !, if / else if / else, = or ==, switch with break, and why doubles are not compared with ==.

In topic 1 you saw the three blocks of structured programming. You already write sequences. This topic covers the other two: selection (this step) and repetition (the next one).

Comparison

Operator Means 7 ? 5
== equal 0
!= not equal 1
< <= less, not more 0, 0
> >= more, not less 1, 1

The result of a comparison is an int: 1 is true, 0 is false. C treats any non-zero value as true.

Logical operators

  • && is and: both conditions are true. grade >= 1 && grade <= 10
  • || is or: at least one is true. grade < 1 || grade > 10
  • ! is not: it flips the result. !(grade >= 5)

In maths you write 1 ≤ x ≤ 10, but not in C. 1 <= x <= 10 first works out 1 <= x (0 or 1), and then compares that 0 or 1 with 10. GCC catches it:

main.c:5:20: error: comparison of constant '10' with boolean expression is always true [-Werror=bool-compare]
    5 |     if (5 <= grade <= 10) {
      |                    ^~
main.c:5:11: error: comparisons like 'X<=Y<=Z' do not have their mathematical meaning [-Werror=parentheses]
    5 |     if (5 <= grade <= 10) {
      |         ~~^~~~~~~~

Write 5 <= grade && grade <= 10.

if, else if, else

if (average >= 9) {
    printf("Excellent\n");
} else if (average >= 5) {
    printf("Passed\n");
} else {
    printf("Failed\n");
}

The conditions are checked from the top. The first branch that fits runs and the rest are skipped, so 9.5 prints only "Excellent". else is optional.

Always write the braces { }, even with one statement inside. Without them, a second statement you add one day won't be part of the branch.

= or ==

if (grade = 10) stores 10 and then checks whether 10 is not 0, so the condition is always true. GCC warns:

main.c:5:9: error: suggest parentheses around assignment used as truth value [-Werror=parentheses]
    5 |     if (grade = 10) {
      |         ^~~~~

Comparison is ==.

switch

When you compare one whole number with several exact values, switch is clearer:

switch (grade) {
case 10:
    printf("Excellent\n");
    break;
case 9:
    printf("Very good\n");
    break;
default:
    printf("Other\n");
    break;
}
  • case 10: runs from here when grade equals 10.
  • break leaves the switch. Without it, execution "falls through" into the next case. The course flags treat that as an error: this statement may fall through.
  • default: takes all other values.
  • Several values can share the same code: case 1: case 2: case 3: one after another, with no statements between them.

switch works only with whole numbers and char, and only for exact equality. For ranges (>= 9) use if.

Two doubles are not compared with ==

From topic 2: 0.1 + 0.2 is not exactly 0.3, so if (0.1 + 0.2 == 0.3) is false. Compare real numbers with an allowed error: a - b < 0.000001 && b - a < 0.000001.