← All lessons/Conditionals· Lesson 12 of 32

Combining conditions

Join conditions with && (and), || (or), and flip them with ! (not).

About 7 min

And, or, not

Sometimes one test isn't enough. Logical operators let you combine true/false values:

  • a && band: true only when both sides are true
  • a || bor: true when either side is true
  • !anot: flips true to false and false to true
Both conditions are true, so the circle is drawn green.

Read x > 0 && x < 100 as *“x is greater than 0 and less than 100.”* That's how you check whether a value sits inside a range.

&& vs &

Use two symbols: && for and, || for or. A single & or | is a different (rarely-used) operator.

Your turn

A variable age is set to 25. Use && to check that age >= 18 && age <= 65; when both are true, call fill(0, 200, 0) and draw ellipse(200, 200, 150, 150).

Hints