Why and when to use a for loop versus a while loop in programming?
- Steven Belanger
- Oct 23, 2021
- 1 min read
Typically, you would use a for loop when you know how many times you would like the loop to run. An example would be Topic #1 of this discussion post. You know the loop needs to run to number 100, therefor it is a for loop. A while loop is run until a condition is met and functions a lot like "if" statements, or at least uses the same conditional structure as the "if" statements. It should also be noted that the while loop is typically used when the number of times the loop needs to run is not known.
Example clipped from here (Links to an external site.) www.cs.utah.edu

Comments