1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
2 Fall 2009ACS-1903 Incrementing and Decrementing Variables by 1 number = number + 1 ; number += 1; number = number - 1 ; number -= 1 ; number++; ++number; number --; --number;
3 Fall 2009ACS-1903 while loops while Pre-test (expression tested before the iteration) Loop could be executed zero times do-while Post-test (expression tested after the iteration) Loop executed at least once
4 Fall 2009ACS-1903 Flowchart for while Boolean Expression Loop statement(s) false true The statements comprising the loop body are executed while the expression is true. pre-test
5 Fall 2009ACS-1903 Reading keyboard input until a sentinel is reached studentNumber=keyboard.nextInt(); countStudents = 0; while (studentNumber != 0) { countStudents++; studentNumber=keyboard.nextInt(); } Sometimes systems are designed to process some data until a “special” value is encountered. ReadUntilSentinel.java
6 Fall 2009ACS-1903 Example studentNumber=keyboard.nextInt(); // get student numbers until the sentinel of 0 is encountered while (studentNumber != 0) { mark = keyboard.nextDouble(); if (mark < 50) grade = "F"; else if (mark < 60) grade = "D"; else if (mark < 65) grade = "C"; else if (mark < 70) grade = "C+"; else grade = "B"; // display the grade System.out.println(studentNumber+" "+mark+" "+grade); studentNumber=keyboard.nextInt(); } Read student numbers and marks -until sentinel of 0 for student number -if there is a student number, there will be a mark too -assign grades based on marks What is the flowchart for this? marksAndGrades.java
7 Fall 2009ACS-1903 Flowchart for do-while Boolean Expression Loop statement(s) false true The statements comprising the loop body are executed repeatedly until the expression becomes false. post-test
8 Fall 2009ACS-1903 Summing the digits of a number int number = 373 ; do { sum +=(number % 10); number=number/10; } while (number!=0); Sometimes a process can be used that can or must execute at least once and so it can be structured with a loop with a post-test. SumDigitsOfInteger.java
9 Fall 2009ACS-1903 for Loop The initialization section … initialize its control variable. The test section of the for statement is similar to the test in a while loop. The update section of the for loop is the last thing to execute in the loop. Example: UserSquares.javaUserSquares.java
10 Fall 2009ACS-1903 The for Loop Initialization Typically, for loops initialize a counting variable Can initialize multiple variables. Variables declared in this section have scope only for the for loop.
11 Fall 2009ACS-1903 The Update Expression usually used to increment or decrement the counting variable(s) last section to execute in the loop. may update multiple variables. Each variable updated is executed as if it were on a line by itself.
12 Fall 2009ACS-1903 Modifying The Control Variable bad programming style to update the control variable of a for loop within the body of the loop leads to hard to maintain code and difficult debugging. update section should be used to update the control variable.
13 Fall 2009ACS-1903 Multiple Initializations and Updates The for loop may initialize and update multiple variables. for(int i = 5, int j = 0; i < 10 || j < 20; i++, j+=2){ … loop statements } only the semicolons are mandatory. for(;;){ … loop statements }//infinite loop. We will not consider using for’s like the above We do not cover the use of the break or continue statements If left out, the test section defaults to true.
14 Fall 2009ACS-1903 Nested Loops Like if statements, loops can be nested. If a loop is nested, the inner loop will execute all of its iterations each time the outer loop executes once. for(int i = 0; i < 10; i++) for(int j = 0; j < 10; j++) { … loop statements } The loop statements in this example execute 100 times. Example: Clock.javaClock.java