A while loop is one type of loop that will go on forever while some condition is true (or false).

You can add the word “not” DIRECTLY AFTER “while” if you want to say while something is not true.

Syntax-

while # condition X:

code that loops

code that doesn’t loop

while not #condition X:

code that loops

code that doesn’t loop

  1. First the condition is checked (either true or false)
  2. If true, the code inside the loop will run and you go back to step 1 (checking the condition)
  3. If false, the code that doesn’t loop runs and the looping process is over

Key idea: the condition in the while loop has to eventually be made false by the body of the while loop. If it never becomes false, the loop will go on forever, resulting in a runtime error.