Skip to content
ExamSpine

Lesson 2.4

else-if chains; the first true branch wins

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.

else-if chains and the first true branch

By the end of this you will be able to write a chain of conditions where the first true one wins and the rest are skipped.

In the last lesson you saw if/else for two outcomes. When there are three or more, an else-if chain checks one condition after another and runs the first block whose condition is true.

An else-if chain

mark = int(input())
if mark >= 90:
    print("A")
elif mark >= 70:
    print("B")
elif mark >= 50:
    print("C")
else:
    print("F")
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner
        = new Scanner(System.in);
    int mark
        = Integer.parseInt(
            scanner.nextLine());
    if (mark >= 90) {
        System.out.println("A");
    } else if (mark >= 70) {
        System.out.println("B");
    } else if (mark >= 50) {
        System.out.println("C");
    } else {
        System.out.println("F");
    }
}

A mark of 85 prints B. A mark of 95 prints A. Only the first true branch runs.

In Python, the keyword is elif (one word). In Java, it is else if (two words). The else at the end catches everything that did not match any condition above it.

The first true branch wins

The conditions are checked from the top. The moment one is true, its block runs and the rest of the chain is skipped. Order matters: if you check >= 50 before >= 70, a mark of 85 matches >= 50 first and gets C.

A common mistake: checking the ranges in the wrong order. When every test is a >= test, as here, start with the highest threshold and work down.

Now try it yourself on the questions below.

Exercise 2.4.1 · Trace itTrace: elif

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

Line 1: n = 75
Line 2: if n >= 90:
Line 3, indented 1 level: grade = "A"
Line 4: elif n >= 70:
Line 5, indented 1 level: grade = "B"
Line 6: elif n >= 50:
Line 7, indented 1 level: grade = "C"
Line 8: else:
Line 9, indented 1 level: grade = "F"
Line 10: print(grade)

Fill in one row each time a line changes n or grade, 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.

Rowngradeoutput
1
2
3

Exercise 2.4.22.4.1 · Write itGrade letter

Write a program that reads a mark (a whole number, 0 to 100) and prints the mark, then the grade on the next line.

  1. Always print Mark: followed by the mark on the first line.
  2. On the next line, print the grade letter:
  • 90 or more: A
  • 70 to 89: B
  • 50 to 69: C
  • below 50: F
  1. Spaces at the end of a line are ignored.

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

In Python, use elif. In Java, use else if.

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 7 times: the 3 examples below, and 4 hidden ones that check the rules above. Only what it prints is checked.

  • an A (rule 2)

    You type

    1. typed: 95

    It prints

    Mark: 95
    A
  • a B (rule 2)

    You type

    1. typed: 75

    It prints

    Mark: 75
    B
  • exactly 90: the A boundary (rule 2)

    You type

    1. typed: 90

    It prints

    Mark: 90
    A
solution.pySolution.java

Exercise 2.4.32.4.2 · Write itTicket price

Write a program that reads an age (a whole number, 0 or more) and prints the age and the ticket price on one line.

  1. Under 12: price is 5 (child).
  2. 65 or over: price is 7 (senior).
  3. Everyone else: price is 10 (adult).
  4. The format is Age: N Price: P on one line, where N is the age and P is the price.
  5. Spaces at the end are ignored.

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

In Python, use int to convert. 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 7 times: the 4 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • a child under 12 (rule 1)

    You type

    1. typed: 8

    It prints

    Age: 8 Price: 5
  • an adult (rule 3)

    You type

    1. typed: 30

    It prints

    Age: 30 Price: 10
  • exactly 12: not a child (rule 1 boundary)

    You type

    1. typed: 12

    It prints

    Age: 12 Price: 10
  • exactly 65: a senior (rule 2 boundary)

    You type

    1. typed: 65

    It prints

    Age: 65 Price: 7
solution.pySolution.java