Download presentation
Presentation is loading. Please wait.
1
AdvWeb-1/14 DePaul University SNL 262 Advanced Web Page Design Chapt 8 - Looping Instructor: David A. Lash
2
AdvWeb-2/14 Chapt 8 - Looping u Loops are another basic construct in almost every programming language. u The purpose of loops is to make code more compact (at the cost of increase complexity a little) u You often create/use counting variables to keep track of the number of iterations u Will look at: – The for loop – while loop – do while loop – combining loops with arrays
3
AdvWeb-3/14 The For Loop u For example, these 2 segments are equal: document.write("Hello Number 1"); document.write("Hello Number 2"); document.write("Hello Number 3"); document.write("Hello Number 4"); document.write("Hello Number 5"); for (i=1; i<5; i++ ) { document.write("Hello Number", i, ” " ); } This is often called a for loop
4
AdvWeb-4/14 For Loop General Format for (i=1; i<5; i++ ) { document.write("Hello Number", i, "\n" ); } Initial value of i increment i by 1 each iteration Repeat until conditi on is false
5
AdvWeb-5/14 For Loop Example User Response Example User Response Example A Looping we will go.... for (i=1; i<5; i++ ) { document.write(" Hello Number ", i, " " ); } See: http://www.depaul.edu/~dlash/extra/Advwebpage/examples/6_ loops8 -1.html http://www.depaul.edu/~dlash/extra/Advwebpage/examples/6_ loops8 -1.html
6
AdvWeb-6/14 Yet another example.... For example, what would the following do? i =19; for (i=1; i<5; i++ ) { document.write("Hello Number", i, "\n" ); }
7
AdvWeb-7/14 Still yet another example.... For example, what would the following do? for (i=1; i<5; i++ ) { for (j=1; j<10; j++ ) { document.write("Hello Number", i, j,, " ); }
8
AdvWeb-8/14 Don’t Do This.... For example, what would the following do? for (i=6; i>5; i++ ) { document.write("Hello Number", i, "\n" ); }
9
AdvWeb-9/14 The While Loop i=0; while ( i < 10 ) { document.write("Hello Number", i++, "\n" ); } Initial value of i Increment i by 1 each iteration Repeat until condition is false for (i=1; i<10; i++ ) { document.write("Hello Number", i, "\n" ); }
10
AdvWeb-10/14 Using break... Causes innermost loop or switch statement to exit immediately while ( true ) { if ( values[i] > max ) { max=values[n]; break; } n++; }
11
AdvWeb-11/14 Using Continue... Causes innermost loop or switch statement to exit immediately for ( i =0; i < values.length; i++ ) { if ( values[i] > null ) { continue; // skip bad data } total_values=total+values[i]; }
12
AdvWeb-12/14 For/in... A way to loop through the properties of an object: General Format: for ( variable in object ) statement Any JavaScript variable Any JavaScript object Some sample objects are windows, browser, history that have properties you can look at. (more information in Chapt 10)
13
AdvWeb-13/14 For/in Example... Looping Exercise Looping Exercise A Looping we will go.... for (i in window) { document.write(" property: " + i + " "); document.write(" value: " + window[i] + " "); } See: http://www.depaul.edu/~dlash/extra/Advwebpage/examples/6_ loops8 -3b.html http://www.depaul.edu/~dlash/extra/Advwebpage/examples/6_ loops8 -3b.html
14
AdvWeb-14/14 For/in Example... numbs = new Array(); for (i=0; i<5; i++ ) numbs[i] = i*5; guess=window.prompt("gimme a number to look for..."); FOUND=0; for (i=0; i<5; i++) { if ( numbs[i] == guess ) { document.write(" FOUND IT NUMBER=" + numbs[i] + "Index = " + i + " " ); FOUND=1; break; } if ( FOUND == 0 ) document.write(" Could not find it..." + guess + " " ); See: http://www.depaul.edu/~dlash/extra/Advwebpage/examples/6_ loops8 -4.html http://www.depaul.edu/~dlash/extra/Advwebpage/examples/6_ loops8 -4.html A “flag” Each iteration looks sets next array element
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.