What is the difference between while loop , for loop & do while loop?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

What is the difference between while loop , for loop & do while loop??

11th Aug 2018, 7:08 AM
Aditya Upadhyay
Aditya Upadhyay - avatar
11 Answers
+ 10
● The 'for' loop allows Initialization, Evaluation, and "Action" stages all placed within the loop declaration. It also supports locally scoped iterator & variables (memory efficiency, safety), something other types of loops such as the 'while' and 'do..while' loop doesn't. In the case of 'while' and 'do..while' loop, Initialization needs to be placed outside, only Evaluation is placed in loop declaration, and "Action" is placed inside loop body. ● The 'while' loop evaluates loop condition BEFORE it executes the instructions inside loop body, on the other hand ... ● The 'do..while' loop evaluates loop condition AFTER it executes instructions inside loop body (at least once). √ The 'do..while' loop is most suitable for situations where you need the loop to run once, at least, before condition is evaluated. e.g. prompt for verifiable input, ask user login info, showing a menu etc. Hth, cmiiw
11th Aug 2018, 8:36 AM
Ipang
+ 7
Fundamental concept are same in every languages only varies in syntax. They all does the same thing means repeating some statement untill condition is true.
11th Aug 2018, 7:32 AM
Meet Mehta
Meet Mehta - avatar
+ 4
i believe you already know that loop means an iterative process. so *while* means that in as much the condition given is true then you should execute the statement given. its possible the comdition is not true so you dont execute any statement. but *do while* means you will first execute that statement before considering the condition, so a do while statement must occur atleast once but its possible a while statement doesnt occur at all. For loop has a start point and and end point given
11th Aug 2018, 8:15 PM
Iwuanyanwu Fredrick
Iwuanyanwu Fredrick - avatar
+ 3
Thanks for the help Anita
11th Aug 2018, 3:55 PM
Aditya Upadhyay
Aditya Upadhyay - avatar
+ 2
Thanks. but in c programme
11th Aug 2018, 7:18 AM
Aditya Upadhyay
Aditya Upadhyay - avatar
+ 2
in for loop you determine how many times your loop runs but in while loop your code runs until your condition be false and in do while loop your code runs once and then check the condition
11th Aug 2018, 2:18 PM
Anita
Anita - avatar
+ 2
While - your condition is at the begin of the loop block, and makes possible to never enter the loop .In While loop, the condition is first tested and then the block of code is executed if the test result is true. Do While - Your condition is at the end of the loop block, and makes obligatory to enter the loop at least one time . In Do-While, the code is first executed and then the condition is checked. The next iteration is executed if the test result is true. For Loop - For loops are typically used when the number of iterations is known before entering the loop. For-loops can be thought of as shorthands for while-loops which increment and test a loop variable.
13th Aug 2018, 4:32 AM
Manish Kumar
Manish Kumar - avatar
+ 2
while loop it will check the condition before executing the code .so when condition is false it will not executed. do while loop it will first execute the code and check the condition. so when the condition is false it will execute only once for loop is same as while loop
16th Aug 2018, 2:46 AM
Ahrane Mahaganapathy
Ahrane  Mahaganapathy - avatar
+ 1
The between the while loop and the do while is that the do while loop will run a program once even if the condition is false ,is it will still run it at least one time but the while loops does not do this , the for loop usually iterate through the scope of it variable , in just a single scope , with it you don't have to declare a variable outside of it scope and pass in to it method the time to break for loop provide a quick and easy solution to that.. I hope this help you
12th Aug 2018, 8:11 PM
George S Mulbah II
George S Mulbah II - avatar
0
do while - as people said here before me, will be done at least once. while - will be done till the condition is true... same as for loop. for me the main difference is that for loop is more comfortable for counters or some this kind of stuff, while while loop is very comfortable to use for 'flag raised' conditions. example: for( int cntr=0; cntr <max_value; cntr++) while( Flag_PBIT==0);
12th Aug 2018, 6:55 PM
Dan Rabinovich
Dan Rabinovich - avatar