While loops

In addition to conditionals, another handy construction is the ability to repeat an action while a specific condition does not appear. A while loop is used for that, with the following syntax.

[!java|scala|c]while (condition) {
    action();
}[/!][!python]while condition:
    action()[/!]

The inner bloc is then executed again and again, as long as the condition remains true. More specifically, the buggle tests the value of the condition. If it's false, it ignores the bloc and continue below. If it's true, it executes the bloc. After that, it tests the condition. If it's now false (for example because the moves of the block made us facing the wall), it now ignores the bloc and continue. If it's still true, it execute the bloc and reevaluate the condition. It does so as long as the condition remains true.

Naturally, if the chosen action does not modify the value of the condition, the buggle will do the action endlessly. The stop button of the interface becomes then handy. To test this, you can try to type the following code in the editor:

[!java|scala]while (true) {
    left();
}[/!][!c]while (1) {
    left();
}[/!][!python]while True:
    left()[/!]

This will let the buggle turn left as long as [!c]1[/!][!java|scala]true[/!][!python]True[/!] remains true (ie, endlessly), or until you stop it manually using the stop button.

Exercise goal

You now have to write some code so that your buggles move forward until they encounter a wall. The idea is thus to do something like:
while we are not facing a wall, do:
  moveForward()

Look at the documentation (in "Help/About this world") for the full list of buggles' methods.