This challenge will introduce you to loops – a way to repeat one or more commands.
- Add the command
Turtle.MoveToRandomPosition();that moves the turtle to a random position on the scene. - Repeat this command forever.
While technically you could just copy and paste the above command, you won’t be able to repeat it forever. A better way to repeat an arbitrary number of commands is to use a
whileloop. The general form of awhileloop is always the same. The following snippet shows it with two placeholders<condition>and<commands>that must be replaced for every loop.while (<condition>) { <commands> }<condition>must be replaced with an expression of typebool. As you probably rememberboolhas only two possible values:trueandfalse. If the condition evaluates totruethe commands inside the curly braces are executed. Then the condition is checked again. If it evaluates totrueagain, all commands inside the curly braces are executed again. So as long as the condition evaluates totruethe commands inside the curly braces are executed. If the condition evaluates tofalsethe commands inside the curly braces are skipped and looping is finished. Program execution continues with the commands after the loop.<commands>must be replaced with an arbitrary number of commands. These commands are repeated as long as the condition of the loop evaluates totrue.To repeat some commands forever we can simply use
trueas our condition. The full loop should then look like this:while (true) { Turtle.MoveToRandomPosition(); }While in C# indentation is not necessary, it helps a lot to see which commands belong to the loop.
- Run the program.
Wow, what a fast turtle. Let’s slow it down a bit.
- Add the command
Turtle.Sleep(500);after the movement.Because sleeping has to occur after every movement, ensure that you put the command before the closing curly brace
}, not after. In this program it wouldn’t make much sense to put any commands after the loop because our loop runs forever. We’ll take a look at finite loops in the next challenge.