What is the logic of programming loops

Counting loop: for

Move square using the for loop (counting loop)

This type of loop is particularly suitable if you know the number of repetitions in advance, such as: "Drive a square!". The robot has to four times cover a distance and always turn 90 degrees in the meantime.

In the flow diagram, an arrow (blue) leads back accordingly (comparable to a feedback). This depends on a condition, the Termination criterion. A counter variable () is used to record and check the number of repetitions required.

The methods and are already known, so we focus here on the actual loop.

The structure of a for loop follows a prescribed syntax. In the body of the loop you will find a number of instructions (here and) to be executed (repeatedly). The general syntax consists of:

forinitializationTermination criterionIncrement{Instructions}
forint counter = 0counter <4counter = counter + 1{...}

Explanation:

for
Keyword to initiate the for loop
initialization
Start value (s) for the counter variable (s)
Termination criterion
Condition (s) for executing the loop body. If the condition is not (no longer) met, the program continues in the next statement below the loop.
Increment
Direction (counting up | counting down) and step size (+1 | +2 | ...) for after each loop pass are specified here.
{Instructions}
this set of statements are executed once per loop pass.