What is the difference between a FOR loop and WHILE loop and how do I write them?

A FOR loop runs for a certain amount of iterations before ending; the amount of loops is specified in the condition. A WHILE loop runs until a condition is met, so if the condition is never met then the WHILE loop would theoretically run forever. In Python a FOR loop can be written like this: (// means new line)
i = 1 // for i in range (1,5): // print("Hello World number:", i) // i = i + 1 // A WHILE loop is written like this: // x = 10 // while x > 0: // print("Hello World") // x = x-1

SB

Related Python Mentoring answers

All answers ▸

Create an algorithm that can be used as a guessing game. Make sure to import random at the start.


Write a program that can convert between celcius and farenheit temperature scales


Create a program that checks for syntactical errors in an email address.


Write a short program to print all the even numbers 1 to 100