Lesson 5 of 23

if, else and else if

8 min 25 XP

if runs code only when its condition is true. Use else if for more choices and else for everything else. In C, 0 means false and any non-zero value means true.

Example: edit me and press Run

Output Loading C (first time takes a few seconds)
Press Run to see the result here.

if (x = 5) stores 5 in x and is always true! Use == to compare. This bug is a classic C trap.

Challenge

+25 XP

Read a number. Print "Positive", "Negative" or "Zero".

Output Loading C (first time takes a few seconds)
Press Run to see the result here.