150 likes | 256 Vues
Learn about different loop patterns involving iteration, including loops with arrays and their variations. Understand how to combine loops with arrays and different loop patterns to achieve specific results.
E N D
Programming patterns involving iteration • Overview Loops • We’ll also see how loops are often combined with arrays (officially your next topic), e.g., -- Programming patterns involving iteration
Programming patterns involving iteration • We’ll review loops – • some of the important variations, and • ways you can use them toward certain ends. • We’ll also see how loops are often combined with arrays (officially your next topic), e.g., -- • Processing every member of an array • Searching for things in an array • Loops within loops for multi-dimensional tables Programming patterns involving iteration
Loops while (condition) { statement; … statement; } • A loop is: • a block of code that executes repeatedly while some condition holds true. • Java provides three forms for explicit loops: • while • for • do..while • The conditions in loops are written as in if statements • The break statement breaks out of the loop that it is within • Some loop examples follow for (start; condition; step) { statement; … statement; } do { statement; … statement; } while (condition); Programming patterns involving iteration
for loops versus while loops • for (int i = 0; i < 7; i++) { System.out.println (i + " " + i*i);} is equivalent to • int i = 0;while (i < 7) { System.out.println (i + " " + i*i);i++;} • Typically we use: • for when we know in advance how many times the loop will execute • while when something that happens in the loop determines when the loop exits • do..while when we want a while loop that always does at least one iteration Programming patterns involving iteration
Loop patterns • The next few slides present loop patterns that are often useful, including: • Do N times • Do N times, changing per loop variable • Count • Sum, minimum, maximum • Find-first • Do-forever • Wait-until Programming patterns involving iteration
Loop pattern: “do N times” for (int k = 0; k < 5; k++) { System.out.print(k + ″ ″); System.out.println( Math.pow((double) k, 5.0) ); Example: print k and k5 to the console window a given number of times: String s; int start, stop; s = JOptionPane.showInputDialog(″Start at?”) start = Integer.parseInt(s); s = JOptionPane.showInputDialog(″Stop after?”) stop = Integer.parseInt(s); for (int k = start; k <= stop; k++) { System.out.print(k + ″ ″); System.out.println( Math.pow((double) k, 5.0)) }; Programming patterns involving iteration
Loop pattern: “count” • Counts how many times a given character appears in a string: public static int charCount(String s, char c) { int count = 0; for (k = 0; k < s.length(); k++) { if (s.charAt(k) == c) { ++ count; } } return count; } Programming patterns involving iteration
Loop pattern: “sum” • Sums the digits in a string of digits: public static int digitSum(String s) { int digit; int sum = 0; for (k = 0; k < s.length(); k++) { digit = Integer.parseInt(s.substring(k, k+1)); sum = sum + digit; } return sum; } Programming patterns involving iteration
Loop pattern: “maximum” • Finds the largest digit in a string of digits: public static int digitMax(String s) { int digit; int max = Integer.parseInt(s.substring(0, 1)); for (k = 1; k < s.length(); k++) { digit = Integer.parseInt(s.substring(k, k+1)); if (digit > max) { max = digit; } } return max; } Programming patterns involving iteration
Loop Pattern: “find-first” • Find the first place where a given character appears in a string.Return -1 if the character does not appear. • public int findChar(String s, char c) { int i = 0; while (i < s.length()) { if (s.charAt(i) == c) { return i; } i++; } return -1; // Not found } for (i=0; i < s.length(); i++) { if (s.charAt(i) == c) { return i; } } Programming patterns involving iteration
Loop Pattern: “do-forever” • Our instruction-followers often go “forever”: while (true) { System.out.println("hi"); ... } for (; true;) { System.out.println("hi"); } Programming patterns involving iteration
Loop pattern: “break in middle” while (true) { ... if (...) { break; } ... } Programming patterns involving iteration
How loops are combined with arrays • This is a preview of what you’ll really be studying next! • Example: Processing every member of an array: for (int k = 0; k < maxDepth; k++) { myArray[k] = 0; // Clear out whole array! } Programming patterns involving iteration
How loops are combined with arrays • Preview, cntd: • Searching for things in an array int k; for (k = 0; k < maxDepth; k++) { if (myArray[k] = 0) { break; // Find an empty slot in array! } } Programming patterns involving iteration
How loops are combined with arrays • Preview, cntd: • Loops within loops for multi-dimensional tables for (int k = 0; k < maxDepth; k++) { for (int m=0; m<maxWidth; m++) { myTable[k][m] = 0; // Clear out 2-dim table! } } Programming patterns involving iteration