Skip to content
ExamSpine

Lesson 2.7

and, or, not

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.

and, or, not

By the end of this you will be able to combine conditions with and, or and not.

So far each if has checked one condition. When a decision depends on two things, you combine conditions with boolean operators.

and: both must be true

A number is in the range 1 to 100 when it is at least 1 and at most 100.

n = 50
if n >= 1 and n <= 100:
    print("In range")
public static void main(String[] args) {
    int n = 50;
    if (n >= 1 && n <= 100) {
        System.out.println("In range");
    }
}

This prints In range. In Python the operator is the word and. In Java it is written &&. Both sides must be true. If either is false, the whole condition is false.

or: at least one must be true

A day is on the weekend when it is Saturday (6) or Sunday (7).

day = 6
if day == 6 or day == 7:
    print("Weekend")
public static void main(String[] args) {
    int day = 6;
    if (day == 6 || day == 7) {
        System.out.println("Weekend");
    }
}

This prints Weekend. In Python the operator is the word or. In Java it is written ||. One true side is enough. The condition is false only when both sides are false.

not: flips true to false

The not operator reverses a condition: true becomes false, and false becomes true.

found = False
if not found:
    print("Not found")
public static void main(String[] args) {
    boolean found = false;
    if (!found) {
        System.out.println("Not found");
    }
}

This prints Not found. In Python the operator is the word not. In Java it is written !, straight before the condition.

A common mistake: and versus or

"Between 1 and 100" needs and (and in Python, && in Java), not or (or in Python, || in Java). With or, every number passes (any number is either at least 1 or at most 100). Read it aloud: "the number is at least 1 and the number is at most 100."

Now try it yourself on the questions below.

Exercise 2.7.1 · Trace itTrace: and or

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

Line 1: a = 5
Line 2: b = 12
Line 3: if a > 3 and b < 10:
Line 4, indented 1 level: c = 1
Line 5: else:
Line 6, indented 1 level: c = 0
Line 7: if a > 3 or b < 10:
Line 8, indented 1 level: d = 1
Line 9: else:
Line 10, indented 1 level: d = 0
Line 11: print(c)
Line 12: print(d)

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

Rowabcdoutput
1
2
3
4
5
6

Exercise 2.7.22.7.1 · Write itRange with and

Write a program that reads a whole number and checks whether it is in the range 1 to 100 (inclusive), using one condition with and (Python) or && (Java).

  1. Always print Number: followed by the number on the first line.
  2. If the number is at least 1 and at most 100, print In range. Otherwise, print Out of range.
  3. Spaces at the end of a line are ignored.

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

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 range (rule 2)

    You type

    1. typed: 50

    It prints

    Number: 50
    In range
  • below range (rule 2)

    You type

    1. typed: 0

    It prints

    Number: 0
    Out of range
  • lower boundary, included (rule 2)

    You type

    1. typed: 1

    It prints

    Number: 1
    In range
solution.pySolution.java

Exercise 2.7.32.7.2 · Write itWeekend check

Write a program that reads a day number (1 for Monday through 7 for Sunday) and prints whether it is a weekday or the weekend.

  1. Always print Day: followed by the day number on the first line.
  2. If the day is 6 (Saturday) or 7 (Sunday), print Weekend. Otherwise, print Weekday.
  3. Spaces at the end of a line are ignored.
  4. The day number is always between 1 and 7.

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

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.

  • Saturday: Weekend (rule 2)

    You type

    1. typed: 6

    It prints

    Day: 6
    Weekend
  • a weekday (rule 2)

    You type

    1. typed: 3

    It prints

    Day: 3
    Weekday
  • Sunday: Weekend (rule 2)

    You type

    1. typed: 7

    It prints

    Day: 7
    Weekend
solution.pySolution.java