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
wordblocks
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:
wordblocks
start moving [forward]
wait until <color sensor [C] reflected light < [30]>
stop movingLoops
wordblocks
repeat [10] times
...
forever
...
repeat until <condition>
...| Loop | Use for |
|---|---|
| repeat x times | Known counts: blink 3 times, shake the arm 5 times |
| forever | Run forever: line-follow main loop, status display |
| repeat until | Run until a condition: follow the line until the junction |
Line following is typically written as a "repeat until" loop that nudges the steering every iteration (see Line Following).
Branching
wordblocks
if <condition> then
...
if <condition> then
...
else
...The < > slot takes Sensors category condition blocks or Operators comparisons:
wordblocks
if <color sensor [C] color = [red]> then
play sound [Bing]
else
display image [X]Comparisons and logic (Operators)
wordblocks
< (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
wordblocks
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
- Traffic light: forever loop - stop moving on red, start moving on green.
- Use "repeat 5 times" to count down 5 → 1 on the light matrix (cleaner with variables).
Next lesson: Variables.