Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is...
-
Upload
alicia-price -
Category
Documents
-
view
215 -
download
2
Transcript of Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is...
![Page 1: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/1.jpg)
Definite LoopsInstructor Rob Nash
![Page 2: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/2.jpg)
What is Definite Looping?A loop: a block of code that is repeated a
number of timesA definite loop: a block of code that is
repeated a finite number of times, known in advance
LoopFor x = 1 to 10 in increments of 1{ … }
![Page 3: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/3.jpg)
The While Loopwhile( condition ) { //increment counter or change condition} //this loop continues while condition is true
int x = 0;while( x < 10 ) {
//do stuffx = x + 1
}
![Page 4: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/4.jpg)
The For LoopFor( expression1 ; expression2; expression
3) exp1: initialization of loop counterexp2: loop continuation testexp3: increment that occurs at the end of a loop,
but before the loop continuation test is evaluated
for( int i = 0; i < 10; i = i + 1)for( int x = 0; x < 10; x++)for( int x = 10; x > 0; x--)
![Page 5: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/5.jpg)
For What?Any time you need to repeat work!Need to read in a directory of users?Or, iterate over websites to assign pagerank?Doing a login screen? What if the username
and password are incorrect? Loop!
![Page 6: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/6.jpg)
Definite Looping Involves…A loop control variableAn initial value for the loop control variableA final or terminal value for the loop control
variableAn increment or decrement that brings us
closer to the terminal value for the control variable.
If your loop isn’t converging on the terminal value, you could be stuck in an infinite loop!
![Page 7: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/7.jpg)
For Examplesfor( int i = 11; i < 77; i = i + 11)
for( int i = 100; i > 0; i = i -1)
for( int i = -200; i < 150; i += 1)
for( int i = 100; i > 0; i = i + 1)Uh, hmmmmm…
for( z = 0; z < 10; z = z + a)But, what if z and a are doubles?
![Page 8: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/8.jpg)
Loops and Loop Variablesfor( int loopCount = 0; loopCount < 3;
loopCount++)In the above example, loopCount is our loop
variableOften called a control variable, as its value
controls or determines when our loop will terminate
![Page 9: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/9.jpg)
Control StructuresDefn: a syntactic structure that controls or alters the flow of
control.
Loops are Repetition Control StructuresThey alter the flow of control so as to iterate over a block of
code many times The exact number of times is known in advance with definite
loopingIfs are Selection Control Structures
These alter the flow of control so as to select one of multiple code paths while executing This is known as conditional branching If some condition is true, go this way, otherwise branch off that
way
![Page 10: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/10.jpg)
Need to Traverse A String?for(int r = 0; r < string.length(); r++) {
System.out.println( string.charAt(r));}
Or, backwards…
for(int r = string.length() – 1; r>=0; r--) {System.out.println( string.charAt(r));
}
![Page 11: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/11.jpg)
A Running Sumfor( int j=0; j <=20; j++ ) {
sum += j; //same as sum = sum + j}
for( int k =0; k <=30; k++ ) {if( k % 2 == 0 ) {
sum = sum + k;}
}
![Page 12: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/12.jpg)
Nested LoopsNeed to loop over a loop? All the time!
Need to clear a monitor of resolution X by Y. Use a loop within a loop to accomplish this!
Or, need to walk over an entire database? One loop would walk over tables
The next (inner) loop would step through each row of the database While the final (innermost) loop would iterate over
each column in the current row Foreach( table ) {
Foreach( row ) { Foreach( column ) {…} } }
![Page 13: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/13.jpg)
Simple Nesting Examplefor( int y = 0; y < 10; y++ ) {
for( int x = 0; x < 5; x++ ) { System.out.print( “*” );
}System.out.println();
}
![Page 14: Definite Loops Instructor Rob Nash. What is Definite Looping? A loop: a block of code that is repeated a number of times A definite loop: a block of code.](https://reader038.fdocuments.in/reader038/viewer/2022110321/56649f545503460f94c78a65/html5/thumbnails/14.jpg)
A Not So Simple Examplefor( int y = 1; y < 10; y++ ) {
for( int x = 1; x < y; x++ ) { System.out.print( “*” );
}System.out.println();
}