+ 1

I still don't get for loop

Although this was explained by my teacher in python, I kind of forgot so..... for (i=1, text=""; i<=5; i++) { text = i; document.write(i + "<br />"); }

25th Sep 2020, 7:21 AM
ToxicDMG
ToxicDMG - avatar
2 Answers
+ 6
‱ The For Loop — https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for for (i = 0; i < 10; i++) {     // do something } Our 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.
25th Sep 2020, 8:28 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 5
I will try to explain the 'for loop' using python concept, which is similar to others but using different syntax. Lets create a list lst = ['bob', 'zac', 'toxic'] Now lets iterate through the list. We use a 'for loop' because we have a known number of iterations. for each_item in lst: print(each_item) This means the code will iterate through the list and print each item within the list as a seperate action. bob zac toxic This technique is useful when you wish to examine a number of items and return those that fit your defining parameters
25th Sep 2020, 7:56 AM
Rik Wittkopp
Rik Wittkopp - avatar