definite loops instructor rob nash. what is definite looping? a loop: a block of code that is...

14
Definite Loops Instructor Rob Nash

Upload: alicia-price

Post on 13-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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

A Not So Simple Examplefor( int y = 1; y < 10; y++ ) {

for( int x = 1; x < y; x++ ) { System.out.print( “*” );

}System.out.println();

}