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

Answered by Sam B. Python tutor

1479 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

[Exam style] Python is an interpreted language. Explain what this means and how interpreted languages differ from compiled languages.


When do we use a for-loop and when a while-loop?


How do you define a class in python


What is the difference between & and && operators?


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo
Cookie Preferences