Skip to content

Loops & Conditions

Control category (orange) blocks steer program flow: repetition, waiting, branching. Add the Operators category (green) comparisons and your program can react to the world.

Waiting

wait [1] seconds
wait until <condition>
  • wait x seconds: pause a fixed time.
  • wait until <condition>: stay here until the condition is true. The core block of sensor programming:
start moving [forward]
wait until <color sensor [C] reflected light < [30]>
stop moving

Loops

repeat [10] times
  ...

forever
  ...

repeat until <condition>
  ...
LoopUse for
repeat x timesKnown counts: blink 3 times, shake the arm 5 times
foreverRun forever: line-follow main loop, status display
repeat untilRun until a condition: follow the line until the junction

Line following is exactly a "repeat until" loop that nudges the steering every iteration (see Line Following).

Branching

if <condition> then
  ...

if <condition> then
  ...
else
  ...

The < > slot takes Sensors category condition blocks or Operators comparisons:

if <color sensor [C] color = [red]> then
  play sound [Bing]
else
  display image [X]

Comparisons and logic (Operators)

< (value) > [50] >
< (value) < [50] >
< (value) = [50] >
< <A> and <B> >
< <A> or <B> >
< not <A> >

Compare ranges, not exact values

Continuous readings (reflected light, distance) almost never equal an exact number. Write < 30 or > 70, never = 30. Equality is only for discrete values like colors.

Combined example: patrol

when program starts
set movement motors [A+E]
repeat [4] times
  start moving [forward]
  wait until <distance sensor [D] distance < [10] cm>
  move [backward] for [1] rotations
  move [steering 100] for [340] degrees      ← U-turn (degrees need measuring)

Exercises

  1. Traffic light: forever loop - stop moving on red, start moving on green.
  2. Use "repeat 5 times" to count down 5 → 1 on the light matrix (cleaner with variables).

Next lesson: Variables.