Lesson 4: Conditionals — if & else
Until now our programs ran line after line, never stopping and never choosing. In this lesson we learn to make decisions: comparison operators like == and >= return 1 (true) or 0 (false), and if runs a code block only when the condition is true. else provides an alternative path that runs when the c
if is like a doorman: if you meet the condition — you enter through the main door; otherwise (else) — you're sent to the side door. Only one door opens each time.
- comparison operator
- ==, !=, <, >, <=, >= — compare two values and return 1 (true) or 0 (false).
- if (condition)
- Runs the block in curly braces only if the condition is true (nonzero).
- else
- The alternative branch: runs exactly when the if condition is false.
- truth = nonzero
- In C conditions there is no built-in boolean: 0 is false, any other value is true.
- block { }
- A group of lines between curly braces that behaves as one unit.