Home

While Loops

So far, your entire code runs only once, from top to bottom. This lesson will be on while loops, one of the most common loops in programming.

In a while loop, there is no condition that needs to be met to run. Instead, the code in the while loop will continuously loop until the condition is no longer met.

More Details

Within a while loop, you can also exit the loop with the break function. A break immediately "breaks" out of the loop, immediately exiting the loop. This is usually used along with an if statement.

Try It Out!

Go to Replit
  • Continuously ask the user to enter a number, printing the sum of that number plus the previous sum (assume 0 for the first sum). Print "Banana" and end the program when "0" is entered.
  • Ask the user for a number, if the number can be divided by 3 (without remainder or decimal), print the number divided by three. Otherwise, exit the program.

Related Problems