Skip to the task
Layout
Back to classic view
READTask 1 of 22 · step 1: Conditions: if, else and switchConditions: if, else and switch
Conditions: if, else and switch in practice/02/

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

Details & hint

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.

practice 02 main.c Code Diff
UNCHANGED This step does not change this file.
#include <limits.h>
#include <stdio.h>
#include "input.h"
int main(void) {
const int current_year = 2026;
int study_year = 1;
char group = 'A';
int birth_year = read_int("Year of birth: ");
int g1 = read_int("Grade 1: ");
int g2 = read_int("Grade 2: ");
int g3 = read_int("Grade 3: ");
double average = (g1 + g2 + g3) / 3.0;
int age = current_year - birth_year;
printf("Year of study: %d\n", study_year);
printf("Group: PI25%c\n", group);
printf("Age: %d years\n", age);
printf("Average: %.2f\n", average);
printf("In memory: int %zu B, double %zu B, char %zu B\n", sizeof study_year, sizeof average,
sizeof group);
printf("\n");
printf("Largest int: %d\n", INT_MAX);
long long century = 100LL * 365 * 24 * 60 * 60;
printf("100 years: %lld s\n", century);
printf("0.1 + 0.2 = %.17f\n", 0.1 + 0.2);
return 0;
}