Skip to content
ExamSpine

Lesson 2.1

if

Part 2: DecisionsReading, about 4 min3 exercises in this lesson, in Python2 exercises in this lesson, in Java

Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.

The if statement

By the end of this you will be able to write a program that does something only when a condition is true.

So far, every instruction ran every time. An if statement lets a program skip something when a condition is not met, or do something extra when it is.

The if statement

age = int(input())
if age >= 18:
    print("You can vote.")
print("Done.")
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner
        = new Scanner(System.in);
    int age
        = Integer.parseInt(
            scanner.nextLine());
    if (age >= 18) {
        System.out.println(
            "You can vote.");
    }
    System.out.println("Done.");
}

If the user types 20, both lines print. If the user types 15, only Done. prints, because 15 >= 18 is false.

In Python, the condition ends with a colon, and the guarded line is indented. In Java, the condition goes in brackets, and the guarded line sits between braces. The line after the if block runs either way.

How to read it

  1. The condition (age >= 18) is checked.
  2. If it is true, the indented line (or the block in braces) runs.
  3. If it is false, that line (or block) is skipped.
  4. Either way, the program continues with the next line after the block.

The comparison operators you already know from maths work here: >, <, >=, <=, == (equal, two signs), != (not equal). The next lesson covers them in detail.

A common mistake: using = (assignment) instead of == (comparison). In Python, if x = 5: is a syntax error. In Java, if (x = 5) is an error too when x is an int. To compare, write ==.

Now try it yourself on the questions below.

Exercise 2.1.1 · Trace itTrace: if age

Complete the trace table for this Python code. [2]

Line 1: age = 16
Line 2: limit = 18
Line 3: if age >= limit:
Line 4, indented 1 level: print("Yes")
Line 5: age = age + 1
Line 6: print(age)

Fill in one row each time a line changes age or limit, or prints something. The table already has the right number of rows. In each row, write only what that line changes; you can leave the other cells empty or copy their values down. Write values the way Python shows them: 3 and 3.0 are different, and text goes in quotes. In the output column, write exactly what appears on screen, with no quotes around it.

Rowagelimitoutput
1
2
3
4

Exercise 2.1.22.1.1 · Write itOld enough

Write a program that reads an age (a whole number, 0 or more) and prints a check result.

  1. Always print Age: followed by the age on the first line.
  2. If the age is 18 or more, print Allowed on the next line.
  3. Print nothing else. Spaces at the end of a line are ignored.

Your program is run several times. Only what it prints is checked.

In Python, use int to convert the typed line. In Java, use Integer.parseInt.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 5 times: the 2 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • over 18: the age and Allowed (rules 1 and 2)

    You type

    1. typed: 20

    It prints

    Age: 20
    Allowed
  • under 18: only the age (rule 1)

    You type

    1. typed: 15

    It prints

    Age: 15
solution.pySolution.java

Exercise 2.1.32.1.2 · Write itPass mark

Write a program that reads a mark (a whole number, 0 to 100) and prints a result.

  1. Always print Mark: followed by the mark on the first line.
  2. If the mark is 50 or more, print Pass on the next line.
  3. Print nothing else. Spaces at the end of a line are ignored.

Your program is run several times. Only what it prints is checked.

In Python, use int to convert the typed line. In Java, use Integer.parseInt.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 5 times: the 2 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • above 50: the mark then Pass (rules 1 and 2)

    You type

    1. typed: 75

    It prints

    Mark: 75
    Pass
  • below 50: only the mark (rule 1)

    You type

    1. typed: 30

    It prints

    Mark: 30
solution.pySolution.java