MakeCode Loops (Part 1)
Over and over again...
Loops stand for repetitive instructions. Here certain statements are executed again and again until a certain condition is no longer fulfilled.
The following example shows a so-called "While-Loop". Here the condition is checked first. In this case this is the check of the left obstacle sensor. As long as the sensor detects something, all instructions in the "do" area are executed.
In summary, as long as the left obstacle sensor detects an obstacle, the light of the JoyCar is set to active.
But it makes little sense to switch the light on every time if it does not go out again at some point. If we set the instruction to switch off the light before our loop, we can directly observe how the light changes between the two states. As long as an obstacle is detected, the instruction block is executed again and again within the loop and not left. Only when the condition no longer applies, the loop is left again and the light is switched off.