Skip to content
ExamSpine

Lesson 2.3

else

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.

if and else

By the end of this you will be able to write a program that does one thing when a condition is true and a different thing when it is false.

Earlier in this part you saw if on its own: the guarded block runs or is skipped. An else adds a second block that runs only when the condition is false. Exactly one of the two blocks runs, never both and never neither.

if and else

age = int(input())
if age >= 18:
    print("Adult")
else:
    print("Minor")
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("Adult");
    } else {
        System.out.println("Minor");
    }
}

If the user types 20, it prints Adult. If the user types 15, it prints Minor. One of the two always runs.

In Python, else: sits at the same indent as if, and the else block is indented under it. In Java, else follows the closing brace of the if block, and the else block has its own braces.

How to read it

  1. The condition is checked.
  2. If true, the if block runs and the else block is skipped.
  3. If false, the if block is skipped and the else block runs.
  4. Either way, the program continues after both blocks.

A block can hold any statements, including another if and else.

A common mistake: putting the same code in both blocks. If a line should run regardless of the condition, put it after the entire if/else, not inside either block.

Now try it yourself on the questions below.

Exercise 2.3.1 · Trace itTrace: if else

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

Line 1: x = 7
Line 2: if x > 10:
Line 3, indented 1 level: y = x * 2
Line 4: else:
Line 5, indented 1 level: y = x + 1
Line 6: print(y)

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

Rowxyoutput
1
2
3

Exercise 2.3.22.3.1 · Write itEven or odd

Write a program that reads a whole number and prints whether it is even or odd.

  1. If the number is even, print the number followed by is even.
  2. If the number is odd, print the number followed by is odd.
  3. Print exactly one of those two lines, never both. Spaces at the end are ignored.
  4. The number can be 0 or negative.

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

In Python, use int to convert. A number is even when its remainder after dividing by 2 is 0. 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 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • an even number (rule 1)

    You type

    1. typed: 4

    It prints

    4 is even
  • an odd number (rule 2)

    You type

    1. typed: 7

    It prints

    7 is odd
  • zero is even (rules 1 and 4)

    You type

    1. typed: 0

    It prints

    0 is even
solution.pySolution.java

Exercise 2.3.32.3.2 · Write itSign check

Write a program that reads a whole number and prints its sign.

  1. If the number is greater than 0, print the number followed by is positive.
  2. If the number is less than 0, print the number followed by is negative.
  3. If the number is exactly 0, print the number followed by is zero.
  4. Print exactly one of those three lines. 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 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • a positive number (rule 1)

    You type

    1. typed: 5

    It prints

    5 is positive
  • a negative number (rule 2)

    You type

    1. typed: -3

    It prints

    -3 is negative
  • zero (rule 3)

    You type

    1. typed: 0

    It prints

    0 is zero
solution.pySolution.java