PencilCoder

While Loops!

Goals:

  • Learn about while loops, an alternative to for loops for interation.

Coding Snippets:

  • The following loop continues until the turtle exits the window.
  • coding snippets
  • The following code repeats until the turtle moves a total of 500 units.
  • coding snippets

Math and Computer Concepts:

  • while loops continue to iterate so long as the specified boolean condition evaluates to true. They are particularly useful when the required number of iterations is not known (or even knowable) in advance of running the script. This is true of both examples above, owing to the use of the random function.
  • while loops check the specified condition before each iteration, so it is possible that the code inside the while block won't ever run.
  • while loops are more flexible than for loops: anything that can be done with a for loop can be done with a while loop.

Activities:

Save each of the following in a folder called WhileLoops.