Programming patterns involving iteration Overview Loops Well also

  • Slides: 15
Download presentation
Programming patterns involving iteration • Overview Loops • We’ll also see how loops are

Programming patterns involving iteration • Overview Loops • We’ll also see how loops are often combined with arrays (officially your next topic), e. g. , -- Fundamentals of Software Development I Programming patterns involving iteration 1

Programming patterns involving iteration • We’ll review loops – – some of the important

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 Fundamentals of Software Development I Programming patterns involving iteration 2

while (condition) { statement; … statement; } Loops • A loop is: – a

while (condition) { statement; … statement; } Loops • 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 for (start; condition; step) { statement; … statement; } • 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 Fundamentals of Software Development I Programming patterns involving iteration do { statement; … statement; } while (condition); 3

for loops versus while loops • for (int i = 0; i < 7;

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 Fundamentals of Software Development I Programming patterns involving iteration 4

Loop patterns • The next few slides present loop patterns that are often useful,

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 Fundamentals of Software Development I Programming patterns involving iteration 5

Loop pattern: “do N times” for (int k = 0; k < 5; k++)

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 k 5 to the console window a given number of times: String s; int start, stop; s = JOption. Pane. show. Input. Dialog(″Start at? ”) start = Integer. parse. Int(s); s = JOption. Pane. show. Input. Dialog(″Stop after? ”) stop = Integer. parse. Int(s); for (int k = start; k <= stop; k++) { System. out. print(k + ″ ″); System. out. println( Math. pow((double) k, 5. 0)) Fundamentals of Software }; Development I Programming patterns involving iteration 6

Loop pattern: “count” • Counts how many times a given character appears in a

Loop pattern: “count” • Counts how many times a given character appears in a string: public static int char. Count(String s, char c) { int count = 0; for (k = 0; k < s. length(); k++) { if (s. char. At(k) == c) { ++ count; } } return count; } Fundamentals of Software Development I Programming patterns involving iteration 7

Loop pattern: “sum” • Sums the digits in a string of digits: public static

Loop pattern: “sum” • Sums the digits in a string of digits: public static int digit. Sum(String s) { int digit; int sum = 0; for (k = 0; k < s. length(); k++) { digit = Integer. parse. Int(s. substring(k, k+1)); sum = sum + digit; } return sum; } Fundamentals of Software Development I Programming patterns involving iteration 8

Loop pattern: “maximum” • Finds the largest digit in a string of digits: public

Loop pattern: “maximum” • Finds the largest digit in a string of digits: public static int digit. Max(String s) { int digit; int max = Integer. parse. Int(s. substring(0, 1)); for (k = 1; k < s. length(); k++) { digit = Integer. parse. Int(s. substring(k, k+1)); if (digit > max) { max = digit; } } return max; } Fundamentals of Software Development I Programming patterns involving iteration 9

Loop Pattern: “find-first” • Find the first place where a given character appears in

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 find. Char(String s, char c) { int i = 0; while (i < s. length()) { if (s. char. At(i) == c) { return i; for (i=0; i < s. length(); i++) { } if (s. char. At(i) == c) { i++; return i; } } return -1; // Not found } } Fundamentals of Software Development I Programming patterns involving iteration 10

Loop Pattern: “do-forever” • Our instruction-followers often go for (; true; ) { “forever”:

Loop Pattern: “do-forever” • Our instruction-followers often go for (; true; ) { “forever”: System. out. println("hi"); } while (true) { System. out. println("hi"); . . . } Fundamentals of Software Development I Programming patterns involving iteration 11

Loop pattern: “break in middle” while (true) {. . . if (. . .

Loop pattern: “break in middle” while (true) {. . . if (. . . ) { break; }. . . } Fundamentals of Software Development I Programming patterns involving iteration 12

How loops are combined with arrays • This is a preview of what you’ll

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 < max. Depth; k++) { my. Array[k] = 0; // Clear out whole array! } Fundamentals of Software Development I Programming patterns involving iteration 13

How loops are combined with arrays • Preview, cntd: • Searching for things in

How loops are combined with arrays • Preview, cntd: • Searching for things in an array int k; for (k = 0; k < max. Depth; k++) { if (my. Array[k] = 0) { break; // Find an empty slot in array! } } Fundamentals of Software Development I Programming patterns involving iteration 14

How loops are combined with arrays • Preview, cntd: • Loops within loops for

How loops are combined with arrays • Preview, cntd: • Loops within loops for multi-dimensional tables for (int k = 0; k < max. Depth; k++) { for (int m=0; m<max. Width; m++) { my. Table[k][m] = 0; // Clear out 2 -dim table! } } Fundamentals of Software Development I Programming patterns involving iteration 15