Link Search Menu Expand Document

While Loops

Table of contents

  1. Description
  2. Examples
  3. Infinite Loops

Description

While loops are used to execute a block of code until a condition fails.

Examples


Infinite Loops

An infinite loop is a common error with that occurs when a condition is never given the opportunity to fail. The loop below is an infinite loop, because the cash variable is never decremented. Therefore, the condition never changes and will remain true.

Run the program below, and observe the infinite loop. Then, paste cash -= taco_price on line 6 and run the program again.