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
Answered by Sam B. Python tutor

1764 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

How do For Loops work?


Generate an array of integers from 0 to 99 and split it into two smaller arrays. Each smaller array will contain half of the original.


Write a program that takes a value x and then outputs x Fibonnaci numbers. E.g. if x=6 output would be 1 1 2 3 5 8


Print "Hello World!" ten times without typing (or pasting) the print function more than once


We're here to help

contact us iconContact ustelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences