Control Flow

Lesson 4 of 20

Teach programs to decide: if, elsif, unless and case turn true/false questions into different paths.

The big picture

Code that chooses

So far every line ran. Control flow lets your program pick a path: run this, skip that, depending on the answers to true/false questions.

if / elsif / else

Ruby checks each condition top to bottom and runs the first true branch — exactly one.

unless

Reads like English: run this unless the condition is true.

case / when

Compare one value against a list of options and run the matching branch.

Conditions are questions

Every condition boils down to true or false — comparisons and && || ! build them.