0

Difference between for loop and while loop any one can explain

4th Jan 2020, 9:42 AM
Prakash
Prakash - avatar
7 Respuestas
+ 13
Prakash Check this out: Loops: while and for • https://javascript.info/while-for for (let i = 0; i < 10; i++) {     // do something } for loop consists of three statements, one that is executed before our loop starts (i = 0), one that defines how long our loop should run (i < 10), and one that is executed after each loop (i++). In this example, we are setting i = 0 before our loop starts. We will continue to loop as long as i < 10, and each iteration of the loop will increase i by one. Finally, within our brackets is the code that will be run on each iteration of the loop. Any part of 'for' can be skipped. For example, we can omit 'begin' if we don’t need to do anything at the loop start. let i = 0; // we have i already declared and assigned for ( ; i < 3; i++) { // no need for "begin"     console.log(i); // 0, 1, 2 } We can also remove the 'step' part: let i = 0; for ( ; i < 3; ) {     console.log(i++); } The loop became identical to while (i < 3).
4th Jan 2020, 11:57 AM
Danijel Ivanović
Danijel Ivanović - avatar
4th Jan 2020, 1:00 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 4
for-loop allows locally scoped variables, while-loop doesn't, Python's for-loop is somewhat like for-each loop in the 3 other tagged languages.
4th Jan 2020, 11:29 AM
Ipang
+ 3
#The for statement iterates through a collection or iterable object or generator function. In for loop we can define the how many time will the for loop run. For loop is easy to write than while loop because in for loop initialization,condition check,increment/decrement define in one line. #The while statement simply loops until a condition is False.in while loop we check only condition if condition become false then loop break otherwise it will run.variable is define before outside from the loop and it increment/decrement inside the loop. Also check this. https://www.quora.com/What-is-the-difference-between-while-loop-and-for-loop-What-is-an-example-of-this
4th Jan 2020, 9:59 AM
Maninder $ingh
Maninder $ingh - avatar
+ 1
FOR LOOP checks the conditions before iteration. WHILE LOOP iterates some part of codes before checking the conditions.
6th Jan 2020, 5:51 AM
Stonny
Stonny - avatar
0
Simple: "A condition-controlled loop causes a statement or set of statements to repeat as long as a condition is true. IN PYTHON, you use the WHILE statement to write a condition-controlled loop." "A count-controlled loop iterates a specific number of times. IN PYTHON, you use the FOR statement to write a count-controlled loop."
5th Jan 2020, 8:51 PM
Daniel Ukoha
Daniel Ukoha - avatar