+ 2
What is the difference between a for loop and a while loop? (Which is the best to use?)
10 Answers
+ 4
basically you can make a for loop using while
loop and vice versa
for loop is build more for running over array
or to do a task n times
while while is batter to run till something happened in the loop
but as I said before you can use them both for every thing
+ 3
there is not much difference in for or while loop,the difference lies in how we wanna use them!!
for loop is great for beginners to get hold of looping and while is great if you are trying to build a complex code that simply LOOKS easy !!
#SiD
+ 2
there is no markable major difference,
+ 1
Generally, I see a 'for' loop used with a counter/index and a 'while' loop when evaluating some arbitrary condition.
Though, as pointed out, either can be used.
+ 1
for is usually used for a sequence or a collection of iterates, a finit amount of elements and
while for satisfying a certain condition.
keep that in mind and you won't go wrong.
0
the same functionalty that repeat piece of code but different at
for:
-----
for(you initialize start point and, condition that end loop when its true,and the step between for each iteration)
while:
-----
you can use it when you don't know the number of ireration
start value if you Have it
while(condition to end loop)
{
your code and you step if you have
}
0
There is a great difference between for loop and while loop.
For loop is used to check certain conditions. Like, maybe, whether the condition is true or false.
Whereas, While loop is used to check certain condition true. But, the loop is executed continuously and automatically until it is true. The instant it becomes false, it gets terminated.
0
for loop is best option
- 2
for loop is better than while to decrease the length of code
- 2
there is no much difference between them but for loop is better as compared to while r do while in the way of length of the code written