C++. Robot. Loop "repeat N times"


Loop with a known number of repetitions "repeat (N)"
If you need to execute some sequence of the same commands N times in the program, then to shorten the code, use the construction:
 
repeat (N)
{
   commands
}


For example, program:
forward;
forward;
forward;
forward;


you can write shorter:

repeat (4)
{
    forward;
}


Such a construction is called a loop with a known number of repetitions. The use of loops in the program significantly reduces the size of the program, and hence the memory occupied by the program.

Loops is a key concept in computer science. If you want to be a good programmer, knowing how to handle loops is the most important skill to learn.

In order to show the robot that it is necessary to repeat several commands within the same loop, enclose these commands in curly braces!  
To repeat one command, curly braces can be omitted.

With curly braces we define some block of commands that must be executed in a loop, or after checking the condition.

The number of opening brackets should always equal the number of closing brackets. Always check this!