Skip to content
ExamSpine

Lesson 2.2

Comparison operators

Part 2: DecisionsReading, about 4 min2 exercises in this lesson

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

Comparison operators

By the end of this you will know the six comparison operators and be able to use them in conditions, including comparing text.

The six operators

OperatorMeaning10 and 55 and 5
>greater thantruefalse
<less thanfalsefalse
>=greater than or equal totruetrue
<=less than or equal tofalsetrue
==equal tofalsetrue
!=not equal totruefalse

Each gives true or false. Use them in an if condition:

temperature = int(input())
if temperature > 30:
    print("Hot")
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner
        = new Scanner(System.in);
    int temperature
        = Integer.parseInt(
            scanner.nextLine());
    if (temperature > 30) {
        System.out.println("Hot");
    }
}

If the user types 35, Hot prints. If the user types 30, nothing prints, because 30 is not greater than 30.

Equal to: two signs, not one

== asks whether two values are the same. = sets a variable. In Python, if x = 5: is a syntax error. In Java, if (x = 5) is an error too when x is an int. Always use == in a condition.

Comparing text

This program prints Stopping. when the user types stop:

name = input()
if name == "stop":
    print("Stopping.")
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner
        = new Scanner(System.in);
    String name = scanner.nextLine();
    if (name.equals("stop")) {
        System.out.println("Stopping.");
    }
}

In Python, == works on text: name == "stop" is true when the user typed stop. In Java, == on text checks whether two text values are the same object in memory, which is almost never what you want, so Java uses name.equals("stop") instead. If you write Java, always compare text with .equals, never ==.

A common mistake: using > when you mean >=. A threshold of 50 with > misses exactly 50. Read the condition aloud: "Is 50 greater than 50?" is false.

Now try it yourself on the questions below.

Exercise 2.2.12.2.1 · Write itCheck temperature

Write a program that reads a temperature (a whole number) and prints a report.

  1. Always show Temp: followed by the temperature on the first line.
  2. If the temperature is strictly greater than 30, show Hot on the next line.
  3. If the temperature is strictly less than 10, show Cold on the next line.
  4. A temperature between 10 and 30 (inclusive) gets no extra line.
  5. 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 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • above 30: the temp and Hot (rules 1 and 2)

    You type

    1. typed: 35

    It prints

    Temp: 35
    Hot
  • below 10: the temp and Cold (rules 1 and 3)

    You type

    1. typed: 5

    It prints

    Temp: 5
    Cold
  • exactly 30: not strictly greater, no Hot (rule 2)

    You type

    1. typed: 30

    It prints

    Temp: 30
solution.pySolution.java

Exercise 2.2.22.2.2 · Write itRange check

Write a program that reads a whole number and checks whether it is in the range 1 to 100, inclusive.

  1. Always show Number: followed by the number on the first line.
  2. If the number is at least 1 and at most 100, show In range on the next line.
  3. 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. You need two conditions: one for the lower bound and one for the upper bound.

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.

  • in the middle of the range (rules 1 and 2)

    You type

    1. typed: 50

    It prints

    Number: 50
    In range
  • below the range (rule 1 only)

    You type

    1. typed: 0

    It prints

    Number: 0
  • the lower boundary, included (rule 2)

    You type

    1. typed: 1

    It prints

    Number: 1
    In range
solution.pySolution.java