Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Software Development IProgramming patterns involving iteration1 Overview LoopsOverview Loops We’ll also see how loops are often combined.

Similar presentations


Presentation on theme: "Fundamentals of Software Development IProgramming patterns involving iteration1 Overview LoopsOverview Loops We’ll also see how loops are often combined."— Presentation transcript:

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

2 Fundamentals of Software Development IProgramming patterns involving iteration2 We’ll review loops –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., --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

3 Fundamentals of Software Development IProgramming patterns involving iteration3 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java provides three forms for explicit loops:Java provides three forms for explicit loops: –while –for –do..while The conditions in loops are written as in if statementsThe conditions in loops are written as in if statements The break statement breaks out of the loop that it is withinThe break statement breaks out of the loop that it is within Some loop examples followSome loop examples follow while (condition) { statement; … statement; statement;} for (start; condition; step) { statement; … statement; statement;} do { statement; … statement; … statement; statement; } while (condition);

4 Fundamentals of Software Development IProgramming patterns involving iteration4 for loops versus while loops for (int i = 0; i < 7; i++) { System.out.println (i + " " + i*i); }for (int i = 0; i < 7; i++) { System.out.println (i + " " + i*i); } is equivalent to is equivalent to int i = 0; while (i < 7) { System.out.println (i + " " + i*i); i++; }int i = 0; while (i < 7) { System.out.println (i + " " + i*i); i++; } Typically we use: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

5 Fundamentals of Software Development IProgramming patterns involving iteration5 Loop patterns The next few slides present loop patterns that are often useful, including:The next few slides present loop patterns that are often useful, including: –Do N times Do N times, changing per loop variableDo N times, changing per loop variable –Count –Sum, minimum, maximum –Find-first –Do-forever –Wait-until

6 Fundamentals of Software Development IProgramming patterns involving iteration6 Loop pattern: “do N times” Example: print k and k 5 to the console window a given number of times: for (int k = 0; k < 5; k++) { System.out.print(k + ″ ″); System.out.print(k + ″ ″); System.out.println( Math.pow((double) k, 5.0) ); System.out.println( Math.pow((double) k, 5.0) ); 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.print(k + ″ ″); System.out.println( Math.pow((double) k, 5.0)) System.out.println( Math.pow((double) k, 5.0))};

7 Fundamentals of Software Development IProgramming patterns involving iteration7 Loop pattern: “count” Counts how many times a given character appears in a string: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; }

8 Fundamentals of Software Development IProgramming patterns involving iteration8 Loop pattern: “sum” Sums the digits in a string of digits:Sums the digits in a string of digits: public static int digitSum(String s) { int digit; int digit; int sum = 0; int sum = 0; for (k = 0; k < s.length(); k++) { for (k = 0; k < s.length(); k++) { digit = Integer.parseInt(s.substring(k, k+1)); digit = Integer.parseInt(s.substring(k, k+1)); sum = sum + digit; sum = sum + digit; } return sum; return sum;}

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

10 Fundamentals of Software Development IProgramming patterns involving iteration10 Loop Pattern: “find-first” Find the first place where a given character appears in a string. Return -1 if the character does not appear.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) {public int findChar(String s, char c) { int i = 0; int i = 0; while (i < s.length()) { while (i < s.length()) { if (s.charAt(i) == c) { if (s.charAt(i) == c) { return i; return i; } i++; i++; } return -1; // Not found return -1; // Not found} for (i=0; i < s.length(); i++) { if (s.charAt(i) == c) { return i; }}

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

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

13 Fundamentals of Software Development IProgramming patterns involving iteration13 How loops are combined with arrays This is a preview of what you’ll really be studying next!This is a preview of what you’ll really be studying next! Example: Processing every member of an array:Example: Processing every member of an array: for (int k = 0; k < maxDepth; k++) { for (int k = 0; k < maxDepth; k++) { myArray[k] = 0; // Clear out whole array! myArray[k] = 0; // Clear out whole array! }

14 Fundamentals of Software Development IProgramming patterns involving iteration14 How loops are combined with arrays Preview, cntd:Preview, cntd: Searching for things in an arraySearching for things in an array int k; for (k = 0; k < maxDepth; k++) { if (myArray[k] = 0) { if (myArray[k] = 0) { break; // Find an empty slot in array! break; // Find an empty slot in array! } }

15 Fundamentals of Software Development IProgramming patterns involving iteration15 How loops are combined with arrays Preview, cntd:Preview, cntd: Loops within loops for multi-dimensional tablesLoops 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! myTable[k][m] = 0; // Clear out 2-dim table! }}


Download ppt "Fundamentals of Software Development IProgramming patterns involving iteration1 Overview LoopsOverview Loops We’ll also see how loops are often combined."

Similar presentations


Ads by Google