Study CommonsRead · reason · practise

Home / Reasoning / And, Or, Not: Combine Conditions in Programs

Reasoning & Coding · Coding

And, Or, Not: Combine Conditions in Programs

Combine conditions with and, or and not, trace which part decides the answer, count items in a loop with two tests and fix a program whose or should be and.

Learning objectiveCombine two or more conditions with and, or and not; decide whether the whole condition is true or false; use a range check such as n >= 10 and n <= 20; count or total items in a loop that use two tests; and fix a program that uses or where it needs and.

The idea

A library rule says: you may borrow a book if you have a card and you have no late books. Both things must be true. A different rule says: free entry if you are under 6 or over 60. Only one needs to be true. Programs make rules like these all the time, and they build them from three small words: and, or and not.

and is true only when both conditions are true. or is true when at least one is true, and that includes the case where both are. not flips an answer: not true is false and not false is true. In this lesson a combined condition is written like this: If n > 5 and n < 10 [say "In"] Else [say "Out"]. Each part, such as n > 5, is checked first, then the words join the answers.

A very common use is a range: a number that is at least 10 and at most 20 is written n >= 10 and n <= 20. Both ends count because of >= and <=. Be careful with the joining word. n >= 10 or n <= 20 is true for every number, because any number is at least 10 or at most 20 (or both). Likewise n < 6 and n > 60 is never true, because no number is below 6 and above 60 at the same time.

not goes in front of a condition, and brackets show what it covers: not (x > 4). The opposite of "greater than 4" is "4 or less", so not (x > 4) means x <= 4. It does not mean x < 4. The number 4 itself shows the difference: 4 > 4 is false, so not (4 > 4) is true.

Inside a loop, the combined condition is checked again on every pass. To count the numbers in a list that are more than 5 and less than 15, put If n > 5 and n < 15 [count = count + 1] inside For each n. A loop can also stop on either of two reasons: Repeat until (wall ahead or on G). Here, or is the safe word, because the robot should stop when either one happens. Wall ahead is true when the square in front is a wall or the edge of the grid, and the robot starts on S facing right.

To test a combined condition, try a value just outside each edge, a value on each edge and a value in the middle. All programs on this page are invented examples, and every answer was checked by running the same program on a computer.

Worked examples

Example 1 · Check each part, then join

n = 7. Is each condition true or false? (a) n > 5 and n < 10 (b) n < 5 or n == 7 (c) not (n == 7)

  1. (a) 7 > 5 is true and 7 < 10 is true. Both are true, so and gives true.
  2. (b) 7 < 5 is false, but 7 == 7 is true. One true part is enough for or, so it is true.
  3. (c) 7 == 7 is true, and not flips it to false.

(a) true, (b) true, (c) false.

Example 2 · A range with both ends

A fun fair ride is for ages 6 to 10, both included: If age >= 6 and age <= 10. Test ages 5, 6, 10 and 11.

  1. Age 5: 5 >= 6 is false, so the and is false. Not allowed.
  2. Age 6: 6 >= 6 is true and 6 <= 10 is true. Allowed (the edge counts).
  3. Age 10: 10 >= 6 is true and 10 <= 10 is true. Allowed (the other edge counts).
  4. Age 11: 11 <= 10 is false. Not allowed.

Ages 6 and 10 are allowed; 5 and 11 are not.

Example 3 · The or that should be and

A museum gives a discount to children under 6 and adults over 60. The program says If age < 6 and age > 60 [say "Discount"]. Does anyone get the discount?

  1. Try age 5: 5 < 6 is true, but 5 > 60 is false. And needs both, so no discount.
  2. Try age 70: 70 > 60 is true, but 70 < 6 is false. No discount.
  3. No age is below 6 and above 60 at once, so nobody ever gets the discount.
  4. Each group needs its own way in. Use or: If age < 6 or age > 60. Now 5 and 61 get it, and 6 and 60 do not.

Nobody gets it. Change and to or.

Example 4 · Count with two tests

count = 0; For each n in [3, 8, 12, 15, 20, 7, 10] [If n > 5 and n < 15 [count = count + 1]]; say count. What does it say?

  1. Check each number against both parts. 3: 3 > 5 is false. 8: true and true, so count is 1.
  2. 12: both true, count is 2. 15: 15 < 15 is false, so skip. 20: 20 < 15 is false, so skip.
  3. 7: both true, count is 3. 10: both true, count is 4.

It says 4 (the numbers 8, 12, 7 and 10).

Example 5 · A robot that stops for either reason

The robot starts on S facing right. Repeat until (wall ahead or on G) [F]. Where does it stop?

A 3-by-6 grid. The robot starts at S at the left of the middle row. G is three squares to the right of S, and a wall is in the top row at the far right.SG
A 3-by-6 grid. The robot starts at S at the left of the middle row. G is three squares to the right of S, and a wall is in the top row at the far right.
  1. The robot checks the condition before every move. On S the square ahead is open and S is not G, so it moves.
  2. After 1 move, the square ahead is open and it is not on G. It moves again. After 2 moves, it is still not on G and still no wall ahead. It moves again.
  3. After 3 moves, it is on G. The condition is true, so it stops, even though the square ahead is open.

It stops on G, after 3 moves.

Common mistakes

Using and when either case is enough.

Why this fails: A number cannot be below 6 and above 60. When a rule has two separate groups, join their conditions with or.

Using or in a range.

Why this fails: n >= 10 or n <= 20 is true for every number. A range has two limits that must both hold, so use and.

Thinking not (x > 4) means x < 4.

Why this fails: The opposite of "greater than 4" is "4 or less", written x <= 4. The boundary number 4 is where the two differ.

Forgetting that or is also true when both parts are true.

Why this fails: or means at least one. If both are true, the condition is still true.

Leaving out the edges of a range.

Why this fails: "From 8 to 12" includes 8 and 12, so use >= and <=. Always test the edge numbers.

Using and to stop a loop on either of two reasons.

Why this fails: Repeat until (A and B) waits for both. If B is never true at the same time as A, the loop never ends. Use or to stop on either.

Foundation

Foundation practice

Decide true or false for and, or and not.

Which is true?

1. n = 6. Which condition is true?

  1. n > 6 and n < 10
  2. n >= 6 and n < 10
  3. n > 6 or n < 3
  4. n < 6 and n > 2
And in real life

2. The rule says: you may go outside if your homework is done and it is not raining. Your homework is done, but it is raining. May you go outside?

  1. Yes, because one part is true and that is enough.
  2. Yes, because homework is the more important part.
  3. No, but only if you wait ten minutes.
  4. No, because with and both parts must be true and "not raining" is false.
Or

3. Program: score = 3; If score < 5 or score > 90 [say "Check"] Else [say "Fine"]. What does it say?

  1. Check
  2. Fine
  3. Check, then Fine
  4. Nothing
Not

4. Program: x = 4; If not (x > 4) [say "A"] Else [say "B"]. What does it say?

  1. B
  2. A, then B
  3. A
  4. Nothing
Range

5. Which list has only ages that make age >= 8 and age <= 12 true?

  1. 8, 10, 12
  2. 7, 9, 11
  3. 9, 12, 13
  4. 8, 12, 14

Practice

Practice practice

Count and total in loops with two tests.

Count with and

6. Program: count = 0; For each n in [4, 9, 14, 15, 6, 20, 11] [If n > 5 and n < 15 [count = count + 1]]; say count. What does it say?

  1. 5
  2. 7
  3. 4
  4. 3
Count with or

7. Program: count = 0; For each n in [2, 5, 16, 15, 20, 9, 4] [If n < 5 or n > 15 [count = count + 1]]; say count. What does it say?

  1. 6
  2. 4
  3. 0
  4. 7
Total with two tests

8. Program: total = 0; For each n in [2, 7, 10, 12] [If n is even and n > 5 [total = total + n]]; say total. What does it say?

  1. 31
  2. 29
  3. 24
  4. 22
Not in a loop

9. Program: count = 0; For each n in [3, 1, 3, 3, 5] [If not (n == 3) [count = count + 1]]; say count. What does it say?

  1. 2
  2. 3
  3. 5
  4. 1
Robot grid

10. The robot starts on S facing right. Repeat until (wall ahead or on G) [F]. Wall ahead is true when the square in front is a wall or the edge. Where does the robot stop?

  1. On G
  2. On the second square of its row, just before the wall
  3. On S, because it never moves
  4. On the wall square
A 3-by-6 grid. The robot starts at S at the left of the middle row. Two squares to its right is a wall, then an open square, then G, then an open square.SG
A 3-by-6 grid. The robot starts at S at the left of the middle row. Two squares to its right is a wall, then an open square, then G, then an open square.

Think

Think practice

Fix conditions and choose good tests.

Find the bug

11. A ride is for heights from 100 cm to 140 cm, with both ends included. The program is If height >= 100 or height <= 140 [say "Ride"] Else [say "No ride"]. It says "Ride" for a height of 90 cm. Which fix works?

  1. Change >= to >.
  2. Change <= to <.
  3. Change or to and.
  4. Add Else [say "Ride"].
Same meaning

12. Which condition always has the same true or false answer as not (n > 5)?

  1. n < 5
  2. n <= 5
  3. n >= 5
  4. n == 5
Never true

13. A museum gives a discount to children under 6 and to adults over 60. A student writes If age < 6 and age > 60 [say "Discount"]. What happens, and how do you fix it?

Choose test values

14. A program checks If n >= 10 and n <= 20. Which test values are best for checking the edges?

  1. 9, 10, 20, 21
  2. 12, 15, 18
  3. 0, 15, 30
  4. 10, 11, 19, 20
Stop on either

15. Program: x = 0; steps = 0; Repeat until (x > 20 or steps == 4) [x = x + 5; steps = steps + 1]; say x. What does it say?

  1. 25
  2. 15
  3. 30
  4. 20

Challenge

Challenge practice

Combine all three words and explain your choice.

Write it

16. Write one condition that is true for the numbers from 10 to 20, both included, but not for 15. Use and and not.

Not around and

17. Which list has only values of x that make not (x > 3 and x < 8) true?

  1. 3, 8, 10
  2. 4, 6, 9
  3. 2, 5, 9
  4. 5, 6, 7
Or becomes and

18. Program: x = 0; steps = 0; Repeat until (x > 20 and steps == 4) [x = x + 5; steps = steps + 1]. This is the Stop on either program with or changed to and. What happens?

  1. It stops with x at 20.
  2. It stops with x at 25.
  3. It never stops.
  4. It stops after 5 passes.
Explain

19. Explain the difference between and and or using the rules "Bring a hat and mittens" and "Bring a hat or mittens".

Answer key

  1. 1. n >= 6 and n < 10
    Both parts must be true. 6 >= 6 is true and 6 < 10 is true. n > 6 and n < 10 fails because 6 is not greater than itself (the boundary trap). n > 6 or n < 3 fails because neither part is true. n < 6 and n > 2 fails because 6 is not less than 6.
  2. 2. No, because with and both parts must be true and "not raining" is false.
    And needs every part to be true. Homework is done (true) but not raining is false, so the whole rule is false. Saying yes treats and like or. Having the homework done does not cancel the rain, and the rule does not say anything about waiting for the rain to stop.
  3. 3. Check
    With or, one true part is enough. 3 < 5 is true, so the whole condition is true and the first branch runs. It does not matter that 3 > 90 is false. Fine would be correct only if both parts were false. An if–else runs exactly one branch, so it never says both and never says nothing.
  4. 4. A
    x > 4 is false because 4 is not greater than 4. not flips false to true, so the condition is true and it says A. Some people read not (x > 4) as x < 4, but the opposite of "greater than 4" is "4 or less", so 4 counts.
  5. 5. 8, 10, 12
    Ages 8 to 12 pass, and both ends count because of >= and <=. 7, 9, 11 fails on 7, which is too young. 9, 12, 13 fails on 13, which is too old. 8, 12, 14 fails on 14. Only 8, 10, 12 passes every test.
  6. 6. 4
    The numbers that are more than 5 and less than 15 are 9, 14, 6 and 11, so count is 4. A count of 5 includes 15, which fails n < 15. A count of 7 comes from using or, since every number is more than 5 or less than 15. A count of 3 forgets the 6.
  7. 7. 4
    Each number needs only one true part. 2 < 5, 16 > 15, 20 > 15 and 4 < 5 pass, so count is 4. The numbers 5 and 15 sit on the boundary and fail both tests. A count of 6 treats them as passing, as if the signs were <= and >=. A count of 0 comes from using and, since no number is below 5 and above 15 at the same time. A count of 7 counts every number in the list.
  8. 8. 22
    Only 10 and 12 are both even and more than 5, so total is 10 + 12 = 22. A total of 31 uses or, so every number is added. 29 adds every number more than 5, including the odd 7. 24 adds every even number, including the 2.
  9. 9. 2
    not (n == 3) is true for every number that is not 3. Those are 1 and 5, so count is 2. A count of 3 counts the 3s, which is the opposite. A count of 5 counts every number, and a count of 1 stops after the first number that passes.
  10. 10. On the second square of its row, just before the wall
    The robot checks the condition before every move. On S, the square ahead is open, so it moves once. On the second square, the square ahead is a wall, so wall ahead is true and it stops. With or, one true part is enough, so it never reaches G. Staying on S would mean the wall was directly ahead at the start. G is behind the wall, and a robot cannot move onto a wall square.
  11. 11. Change or to and.
    With or, almost every number passes: 90 is not at least 100, but it is at most 140, so one part is true. Changing to and requires both, so 90 fails and 100 and 140 still pass. Changing >= to > would wrongly reject exactly 100. Changing <= to < would wrongly reject exactly 140. Adding an Else that says Ride would make everyone ride. None of those fixes the real bug, which is the or.
  12. 12. n <= 5
    The opposite of "greater than 5" is "5 or less". n = 5 shows it: 5 > 5 is false, so not (n > 5) is true, and 5 <= 5 is true. n < 5 is false for n = 5, so it is not the same. n >= 5 is true for 6, where not (n > 5) is false. n == 5 is only one of the many numbers that pass.
  13. 13. It never says "Discount", because no age is both under 6 and over 60. Change and to or: If age < 6 or age > 60.
    And needs both parts true at the same time, and no number is below 6 and above 60. With or, an age of 5 or 61 gets the discount, while 6 and 60 do not, which matches "under 6" and "over 60". A good check is to test an age from each group.
  14. 14. 9, 10, 20, 21
    Test just outside, on, on, and just outside the two edges: 9 should fail, 10 and 20 should pass, and 21 should fail. 12, 15, 18 are all comfortably inside and would pass even if the signs were > and <. 0, 15, 30 are far from the edges, so a wrong sign such as > instead of >= would go unnoticed. 10, 11, 19, 20 never test a value outside the range.
  15. 15. 20
    Trace x and steps: 5, 10, 15, 20, with steps 1, 2, 3, 4. After the 4th pass x is 20, which is not more than 20, but steps == 4 is true, and one true part is enough to stop. So it says 20. It does not say 25, because the loop stopped before another pass. 15 stops one pass early. 30 would need six passes, and the loop stops at 4.
  16. 16. Example: n >= 10 and n <= 20 and not (n == 15)
    Check it with 9 (false), 10 (true), 14 (true), 15 (false), 16 (true), 20 (true) and 21 (false). Each part removes some numbers, and and keeps only the numbers that pass every part.
  17. 17. 3, 8, 10
    x > 3 and x < 8 is true only for numbers between 3 and 8, not including 3 or 8. So not (...) is true for everything else. 3 and 8 are outside, so they make it true, and so does 10. 4, 6, 9 includes 4 and 6, which are inside. 2, 5, 9 includes 5. 5, 6, 7 are all inside, so they make it false.
  18. 18. It never stops.
    Now both parts must be true at once. At steps 4, x is 20, which is not more than 20, so the loop continues. After that steps is 5 or more, so steps == 4 is false for ever, and x > 20 alone is not enough. The loop does not stop at 20 or 25, and it does not stop after 5 passes, because those are only moments when one part is true. This is why changing and to or can fix a loop that never ends.
  19. 19. And needs both: with "hat and mittens" you need both items. Or needs at least one: with "hat or mittens" one item is enough, and bringing both is also allowed.
    In code, and is true only when both conditions are true. or is true when one or both are true. Using and for something that must satisfy either case is a common reason why a program never does anything.

Related practice