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
| Operator | Meaning | 10 and 5 | 5 and 5 |
|---|---|---|---|
> | greater than | true | false |
< | less than | false | false |
>= | greater than or equal to | true | true |
<= | less than or equal to | false | true |
== | equal to | false | true |
!= | not equal to | true | false |
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.
- Always show
Temp:followed by the temperature on the first line. - If the temperature is strictly greater than 30, show
Hoton the next line. - If the temperature is strictly less than 10, show
Coldon the next line. - A temperature between 10 and 30 (inclusive) gets no extra line.
- 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
- typed: 35
It prints
Temp: 35 Hot
below 10: the temp and Cold (rules 1 and 3)
You type
- typed: 5
It prints
Temp: 5 Cold
exactly 30: not strictly greater, no Hot (rule 2)
You type
- typed: 30
It prints
Temp: 30
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.
- Always show
Number:followed by the number on the first line. - If the number is at least 1 and at most 100, show
In rangeon the next line. - 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
- typed: 50
It prints
Number: 50 In range
below the range (rule 1 only)
You type
- typed: 0
It prints
Number: 0
the lower boundary, included (rule 2)
You type
- typed: 1
It prints
Number: 1 In range