Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Definite Loops Instructor Rob Nash

2 What is Definite Looping? A loop: a block of code that is repeated a number of times A 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 { … }

3 The While Loop while( condition ) { //increment counter or change condition } //this loop continues while condition is true int x = 0; while( x < 10 ) { //do stuff x = x + 1 }

4 The For Loop For( expression1 ; expression2; expression 3) exp1: initialization of loop counter exp2: loop continuation test exp3: 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--)

5 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!

6 Definite Looping Involves… A loop control variable An initial value for the loop control variable A final or terminal value for the loop control variable An 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!

7 For Examples for( 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?

8 Loops and Loop Variables for( int loopCount = 0; loopCount < 3; loopCount++) In the above example, loopCount is our loop variable Often called a control variable, as its value controls or determines when our loop will terminate

9 Control Structures Defn: a syntactic structure that controls or alters the flow of control. Loops are Repetition Control Structures They 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 looping Ifs 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

10 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)); }

11 A Running Sum for( 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; }

12 Nested Loops Need 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 ) {…} } }

13 Simple Nesting Example for( int y = 0; y < 10; y++ ) { for( int x = 0; x < 5; x++ ) { System.out.print( “*” ); } System.out.println(); }

14 A Not So Simple Example for( int y = 1; y < 10; y++ ) { for( int x = 1; x < y; x++ ) { System.out.print( “*” ); } System.out.println(); }


Download ppt "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."

Similar presentations


Ads by Google