Control and simulation 0/2

Lesson 8 of 21

Control

Make choices with if and else, and repeat work with while and for loops.

Part 1 of 9 Choose with if

Choosing with if and else

So far, every program you have written runs each of its lines once, from top to bottom. That is enough for a calculation, but most programs also need to do two other things.

Sometimes a program has to choose. Say a cinema charges 1,800 yen for a ticket and 900 yen for a child under 12, and you want a program that reads someone's age and prints their price. When you write the program, you don't know what age will be typed. So the program has to hold both prices and print the right one when it runs.

Sometimes a program has to repeat. If you want the total of the numbers from 1 to 1,000, you don't want to write out a thousand additions. You want to write one addition and have it run a thousand times.

The statements that decide which lines run next, and how many times, are called control statements. In this lesson we'll learn three of them:

  • if, which chooses which lines to run
  • while, which repeats lines for as long as a condition is true
  • for, which repeats lines once for each number in a range

Here is the cinema program:

Sign up to run
age = int(input())
if age < 12:
    print(900)
else:
    print(1800)

Type 8 and the program prints 900; type 30 and it prints 1800. The expression age < 12 is called the condition. It is either true or false, and you build it with comparisons such as < and ==. An if statement works out its condition and runs the lines under it only when the condition is true. When the condition is false, the lines under else run instead. So whatever age you type, exactly one of the two prices gets printed.

The lines an if controls are called its body, just like a function's body. You write them the same way too: the if line ends with a colon, and the body is indented below it. else works the same way.

You now know enough to solve your first problem with if. Product, the first problem in this lesson's list, is practice for exactly this.

You are given two positive integers aa and bb, and you print whether their product is even or odd. For 3 4 the product is 12, so the answer is Even; for 1 21 it is 21, so the answer is Odd. A number is even when dividing it by 2 leaves remainder 0, so the condition your if checks is a * b % 2 == 0.

More than two choices

Now suppose children under 6 get in free. That makes three prices, but if and else only give you two places to put lines. For the third, you add elif, short for "else if", which checks another condition in between:

Sign up to run
age = int(input())
if age < 6:
    print(0)
elif age < 12:
    print(900)
else:
    print(1800)

The program checks the conditions from the top down and runs the body of the first one that is true. Everything after that is skipped. Take a 4-year-old: age < 6 is true, so the program prints 0 and doesn't even look at age < 12, although that is true too. For an 8-year-old, age < 6 is false, so the program moves on to age < 12, which is true, and prints 900. The else body runs only when every condition above it was false.

This means the order of your checks matters. If you put age < 12 first, a 4-year-old would stop there and pay 900, and the free price could never be printed. When one condition is narrower than another, check the narrow one first.