definite loops instructor rob nash. what is definite looping? a loop: a block of code that is...
TRANSCRIPT
Definite LoopsInstructor Rob Nash
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{ … }
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
}
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--)
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!
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!
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?
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
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
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));
}
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;}
}
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 ) {…} } }
Simple Nesting Examplefor( int y = 0; y < 10; y++ ) {
for( int x = 0; x < 5; x++ ) { System.out.print( “*” );
}System.out.println();
}
A Not So Simple Examplefor( int y = 1; y < 10; y++ ) {
for( int x = 1; x < y; x++ ) { System.out.print( “*” );
}System.out.println();
}