Section 5.1

while Loop

Summary

  • What are loops?
  • What is a while loop and how does it work?
  • Do all conditional statement syntax work here?
  • While loop can have multiple conditions.

What are loops?

  • Condition: defines condition(s) that would stop loop
  • Body: what code do you want to repeat?
  • Completed by an end (similar to if-end)
  • How variables work in a loop
    • A loop can reference previously defined variables
    • What happens to variables from inside the loop

What is a while loop and how does it work?

Condition(s):

  • Similar to what we saw with conditional statements
  • Iterates until condition is false
  • Beware of infinite loops!
  • Be careful of infinite loops! “ctrl + c” in the Command Window (MUST be here) will stop the program. However, if you have hit “run” multiple times, you need to repeat the stop command multiple times.

Do all conditional statement syntax work here?

Writing conditions (yes)
Else structures (no)

While loop can have multiple conditions

Resources