← The C Language Lesson 14 of 77 · 10 XP

Conditions: if / else

Theory and problem statement

Machine translation, not yet reviewed by a native speaker.

if (condition) {
    ...
} else if (another condition) {
    ...
} else {
    ...
}

The condition goes in parentheses. The curly braces can be left out if the block has one statement, but that's a source of bugs:

if (x > 0)
    printf("positive\n");
    printf("this runs ALWAYS\n");   // the indentation lies: the if doesn't apply here

Advice: always use { }. In 2014 a bug like this in Apple's code (the famous "goto fail") broke the checking of SSL certificates.

The conditions are checked from top to bottom, and the first branch that fits runs.

Task. Read a score from 0 to 100 and print the grade: 90 and above — A, 75–89 — B, 60–74 — C, below 60 — F.

Sign up for free to solve this task right in your browser: code editor, running, test checks and hints.