Download presentation
Presentation is loading. Please wait.
1
Introduction to For Loops
See Beginning JavaScript (Paul Wilton) p. 87 CSD 340 (Blum)
2
For Loop Example CSD 340 (Blum)
3
Result in browser CSD 340 (Blum)
4
SomeHTML+="All work and no play makes Jack a dull boy. "; }
var SomeHTML=""; for(i=1; i<=100;i++) { SomeHTML+="All work and no play makes Jack a dull boy. "; } document.write(SomeHTML); for keyword that indicates a for loop repetitive structure i=1 where we start counting the iterations i<=100 condition under which we will continue to count i++ the incrementing — this does the counting, the changing of the counting variable, in this case by 1. It could also be written i=i+1 or i+=1 The parts above are in parentheses and separated by semicolons. Then comes a set a curly brackets. The line(s) of code in the curly brackets constitute a single “iteration.” CSD 340 (Blum)
5
Variation There are 3 loops this time; the middle one changes the iteration code (inside the curly brackets) somewhat. CSD 340 (Blum)
6
Variation Result CSD 340 (Blum)
7
Second Variation The counting variable is concatenated with the text – so that the counting is seen on the page. CSD 340 (Blum)
8
Second Variation Result
CSD 340 (Blum)
9
Third Variation That’s two minus signs in a row.
A change in the initialization (starting point) to start at 10, a change in the condition appropriate to counting down, a change from incrementing to decrementing. CSD 340 (Blum)
10
Third Variation Result
CSD 340 (Blum)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.