0

What will be better while loop or if loop..

26th Dec 2017, 8:45 AM
Soumya
Soumya - avatar
8 Answers
+ 11
1)do while () loop runs runs 1 time even if the condition is false , 2) while() & for() loop runs only only when test condition evaluates to true 3)genarally , we use for when we know how many iterations we want 4)& some difference of syntax (obviously ) 4)there is noting like if() loop , if is only a contidion & if it evaluates to true ... the statements will get executed only once //hope it helps â˜ș
26th Dec 2017, 9:49 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 5
It depends what you need it for. A for loop is used when you know for certain how many iterations (how many times) the code in the body of loop is going to run for. Whereas a while loop is used when you don't know how many times it will run and will only stop if the stopping condition is met (when the header becomes false) A do while loop runs at least once before checking the stopping condition. So there is a difference and depending on what you require you can chose the appropriate loop.
26th Dec 2017, 9:00 AM
Tarantino
Tarantino - avatar
+ 1
@MinatoOkazaki In general you are completely right. The only difference when using looks are there background, so their service conditions. But you could choose one type of loops and only write code with that one. You can switch from every loop type to another. You just have to remember the functionality and fit the normal code to the loop. In binary code they all should be equal
26th Dec 2017, 9:23 AM
Andreas K
Andreas K - avatar
+ 1
There is no structure like an if-loop. It's either a loop (while / for/ do-while) or an if-conditional
26th Dec 2017, 9:50 AM
Andreas K
Andreas K - avatar
+ 1
I didn't say that you can't make it like while...I just said if you are good with condition then while loop is a perfect solution. but I do you for loop a lot too. :)
26th Dec 2017, 10:14 AM
ARNAB BISWAS
ARNAB BISWAS - avatar
0
There are no if-loops. And in general there is no difference between all kind of basic loops like while, for, and do-while.
26th Dec 2017, 8:52 AM
Andreas K
Andreas K - avatar
0
test = true for (;test;){} does exactly the thing you prefer on while loops...
26th Dec 2017, 10:12 AM
Andreas K
Andreas K - avatar
- 2
for loop is for kids...while loop is definitely more useful..well it actually depends on the situation but in while loop u can say do it until this condition gets false.
26th Dec 2017, 10:10 AM
ARNAB BISWAS
ARNAB BISWAS - avatar