Home > Resources > Homework > Science

Can you explain the differences between a for loop and a while loop, and when it is appropriate to use each type?

Answer 1

Christina Brady

A for loop is used when the number of iterations is known beforehand, typically iterating over a sequence like a list, range, or string. A while loop is used when the number of iterations is not known and depends on a condition being true or false. Use a for loop for definite iteration and a while loop for indefinite iteration.