Conditionals

Conditionals let a program make decisions. Depending on whether a condition is true or false, the program takes one path or another.

Without conditionals, a program runs the same code every time. With conditionals, it can respond differently based on data — a player’s choice, a score, an input value.

Types of Conditionals#

If/Else Statement#

The most common conditional. Checks a condition and runs one block of code if it’s true, another if it’s false.

See If/Else Statement for a full explanation and examples.

Ternary#

A compact form of if/else that fits on one line. Useful when assigning a value based on a condition.

Resources#