which of THE LOOPS IS BETTER?
1> for() loop
2> while() loop
3> do -while loop
and if so than whyyyyy???????????????
which of THE LOOPS IS BETTER?
1> for() loop
2> while() loop
3> do -while loop
and if so than whyyyyy???????????????
It depends on the logic which you are using. It is not correct that one is better than other.
hi,this is swetha,i do think that a while loop is better a for loop because the while loop repeats the condition if the condition satisfies or else it terminates but in for loop is used to test the condition for many times until the condition fails
for() loop is best
because
u do initialization,loop condition,loop modification in a single step
u can have less no. of lines of code
under d cncpt of looping
for loop has strict condition checking
Depends on the program logic. Each has its downsides and upsides though.
For Loop
Advantages include
1. The entire loop can be in one single executable line of code
2. Has the loop conter initialization and post condition action, as well as exiting the loop condition right at the top
3. Enhances readability
One of the major disadvantages is that the counter is defined regardless of the condition is true or false at the start.
while Loop
Quite simple to identify what the condition to come out of the loop is. However, there is no easy way to track if there is any code that ensures that the condition is met at some point. For example, in while (x<10), in the loop body, you need to remember to make sure that x is at some point, 10 or greater. If not, this will be an infinite loop.
do... while
Most useful when you want the body of the loop to execute at least once, regardless of the condition. However, again, if that is not the intent, this is pretty useless. Also, this is not a truly conditional loop, because at least one iteration of the loop executes unconditionally. Again, the disadvantage of remembering to ensure the loop's exit condition is met, is still there.