06-loops - Iteration Statements (Loops) CSE 114: Computer...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Iteration Statements (Loops) CSE 114: Computer Science I SUNY at Stony Brook 1 What We’ve Covered ๏ Simple programs ๏ Variables and data types ๏ Expressions ๏ Conditional (selection) statements 2 Iterative Programming ๏ Many programs perform the same task many times ๏ Operations are repeated on different data ๏ Ex. Adding a list of numbers ๏ Ex. Displaying frames of a movie file ๏ Repetitive tasks are specified using loops 3 Loop Elements All loop constructs share four basic elements: 1. Initialization 2. Testing the loop condition 3. Loop body (the task to be repeated) 4. Loop update ๏The relative order of these elements may vary 4 Initialization ๏ This section of code is used to set starting values ๏ For example, setting a total to 0 initially ๏ This can be done as part of the loop, or separately before the loop code begins 5 Loop Tests ๏ Test expressions are used to determine whether the loop should execute (again) ๏ Tests compare one value/variable with another ๏ If the test evaluates to TRUE, then the loop will execute another time 6 Loop Update ๏ This step changes the value(s) of the loop variable(s) before the loop repeats ๏ Ex. moving to the next item to process ๏ This can be done explicitly as part of the loop, or it can be done inside the loop body 7 while Loops ๏ while loops execute as long as the test condition is true ๏ Order of execution: 1. Initialization 2. Loop condition test 3. Loop body 4. Loop update 8 General Form initialization while ( loop condition test ) { loop body loop update } 9 while Loop Example int countDown = 5; while (countDown >= 0) { System.out.print(countDown); System.out.println(“...”); countDown--; } 10 Loop Breakdown int countDown = 5; while (countDown >= 0) { System.out.print(countDown); System.out.println(“...”); countDown--; } 11 Loop initialization Loop test Loop body Loop update Loop Output 5... 4... 3... 2... 1... 0... 12 Another Example int root = 0; while (root < 10) { root += 1; System.out.print(root + “ * ” + root); System.out.print(“ = ”); System.out.println(root * root); } 13 Loop Output 1*1=1 2*2=4 3*3=9 4 * 4 = 16 ... 8 * 8 = 64 9 * 9 = 81 10 * 10 = 100 14 do...while Loops ๏ Like while loops, but always execute at least once ๏ Order of execution: 1. Initialization 2. Loop body 3. Loop update 4. Loop condition test 15 General Form initialization do { loop body loop update } while ( loop condition test ) ; 16 do...while Example // print numbers from 1-15 int counter = 1; do { System.out.println(counter + “”); counter++; } while (counter < 16); 17 Loop Breakdown // print numbers from 1-15 int counter = 1; do { Loop initialization System.out.println(counter + “”); Loop body Loop update counter++; Loop test } while (counter < 16); 18 Loop Output 1 2 3 4 5 ... 13 14 15 19 do...while Example int sum = 0, value = 0; Scanner in = new Scanner(System.in); do { sum += value; System.out.print(”Enter a # (-1 to quit): “); value = in.nextInt(); } while (value != -1); 20 for Loops ๏ for loops execute a fixed number of times ๏ Order of execution: 1. Initialization 2. Loop condition test 3. Loop body 4. Loop update 21 General Form for ( initialization ; loop condition test ; loop update ) { loop body } 22 for Loop Example for (int i = 0; i < 10; i++) { System.out.println(i); } 23 Loop Breakdown Loop Loop Loop update initialization test for (int i = 0; i < 10; i++) { System.out.println(i); } Loop body 24 Loop Output 0 1 2 3 4 5 6 7 8 9 25 Another Example int nextNumber, sum = 0; Scanner in = new Scanner(System.in); for (int i = 0; i < 5; i++) { System.out.print(“Enter a number: ”); nextNumber = in.nextInt(); in.nextLine(); // consume extra newline sum += nextNumber; } 26 i 0 1 2 3 4 5 nextNumber 2 15 5 7 3 - sum 0 2 17 22 29 32 32 27 Fancier for Loop Headers ๏ We can include multiple initialization or update statements in a loop header ๏ Separate each statement with a comma ๏ Ex. for (i = 0, j = 1; i < 5; i++, j--) { } ๏ Loop update statements don’t have to increase by 1 ๏ Ex. for (i = 1; i < 101; i = i + 5) { } 28 Advanced for Loop Headers ๏ Loop headers can also include calls to methods ๏ Ex. for (int i = 0; i < numPlayers(); i++) ๏ A for loop can also omit part of the header ๏ Ex. for ( ; i < 5; i++) { } ๏ Pathological example: for ( ; ; ) { } 29 Choosing a Loop Type ๏ For a fixed number of iterations: ๏ for loops are generally considered the way to go ๏ For a variable number of iterations: ๏ while loops can execute 0 or more times ๏ do...while loops execute at least once ๏ However, any loop can be rewritten as any other type 30 Nested Loops ๏ The body of a loop can contain any other type of statement(s) ๏ This includes other loops ๏ If the outer loop executes n times, and the inner loop executes m times, the body of the inner loop will execute (n × m) times 31 Nested Loop Example for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { System.out.print(“*”); } System.out.print(“\n”); } 32 Example Output **** **** **** **** 33 Another Example int numStars = 1; do { for (int i = 0; i < numStars; i++) System.out.print(“*”); System.out.print(“\n”); numStars++; } while (numStars < 11); 34 Loop Output * ** *** **** ***** ****** ******* ******** ********* ********** 35 Next Time ๏ Methods and Objects ๏ Testing and Debugging 36 ...
View Full Document

This note was uploaded on 04/03/2009 for the course CSE 114 taught by Professor Tashbook during the Spring '08 term at SUNY Stony Brook.

Ask a homework question - tutors are online