for loop vs while loop

it depends on the problem: a for loop is comfier when you know the number of iterations you need, and you're better off with a while loop when the iterations can't be really defined as an exact value
 
For loops performe a specific number of iterations where as a while loop will continue to iterate whilst a condition is true.
 
Does for loop has any advantage or disadvantage over while loop?
I think I saw somewhere that the for loop is faster in terms of execution.

My advice: is to use for loop when the number of iterations is known. Otherwise use while
 
For loop is super convenient when you're dealing with collections (groups of stuff, like lists). If you need to do something once for each element of the collection regardless of the size of the collection, for loop is the way.
 
For loop:
Best for iterating over iterables.

While loop:
Keep looping till a condition is met
 
Also with some programming languages the last loop of while(actually do while) loop don't get executed so you gotta know whatcha doin.
 
While loops are landmines, the advantage of not using them professionally is job security.

Not only can the code fail, the condition source can fail. More importantly in a maintenance perspective you need to be able to know how a program is going to behave in runtime from the source code. While loops create unknowns.

You never want to be entertaining collections that can be changed when a thread is looping over them.

The only acceptable usage of while is when the loop never ends.
 
Back
Top