Lesson 1 of 4 · 8 minutes

If this, then that

So far, every line ran every time. An if statement runs a block only when a condition is true.

The condition goes inside round brackets. The code inside curly brackets is the block. If the condition is false, the block is skipped. Nothing prints. The program continues.

Think of a fork in a path. Take one branch if the sign's condition is true. Otherwise, walk past it.

A path branches to a message only when age is at least 18

A true condition takes the branch. A false one walks past.

Loading the editor…

Change age to 15. Nothing prints, because the block is skipped.

Quick check

When does code inside an if block run?

Choose one answer.

The condition is false and there is no else. What prints?

Choose one answer.

You can now make code run only when a condition is true.