1 / 15

CSE 501N Fall ‘09 08: Control Flow

CSE 501N Fall ‘09 08: Control Flow. 24 September 2009 Nicholas Leidenfrost. Lecture Outline. Lab 2 questions Control Flow and Loops The break Statement The continue Statement The return Statement Array Exercises. Control Flow.

Download Presentation

CSE 501N Fall ‘09 08: Control Flow

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 501NFall ‘0908: Control Flow 24 September 2009 Nicholas Leidenfrost

  2. Lecture Outline • Lab 2 questions • Control Flow and Loops • The break Statement • The continue Statement • The return Statement • Array Exercises

  3. Control Flow • Recall that control flow (a.k.a. flow of control) is the order in which statements in a program are executed • Certain types of statements allow us to alter the default flow of control (which is linear) • The ifstatement allows us to conditionally execute a particular statement • The elseclause of an if statement allows us to specify an “otherwise” condition • The switchstatement allows us to conditionally execute many cases based on equality • Loops (for, while, do … while) allow us to repetitively execute a statement

  4. Control Flow • Sometimes it is necessary to circumvent the execution of statements, and it is undesirable or impossible to do so correctly with an if/elsestatement • Particularly with reference to loops • When desired control flow is logically difficult to achieve due to multiple / nested conditional statements • When we want to exit a loop prematurely

  5. Control Flow: break • We have seen how to use breakin the switchstatement: int myCount; int oneCount, twoCount, threeCount; // ... switch (myCount) { case 1: oneCount++; case 2: twoCount++; case 3: threeCount++; // …more cases }

  6. Control Flow: break • We have seen how to use breakin the switchstatement: int myCount; int oneCount, twoCount; // ... switch (myCount) { case 1: oneCount++; break; case 2: twoCount++; break; // …more cases }

  7. Control Flow: break • What if we want to exit a loop prematurely? public boolean hasValue (int[] searchIn, int searchFor) { boolean found = false; for (int i=0; i<searchIn.length; i++) { if (searchIn[i] == searchFor) found = true; } return found; } Equal to 2 at this point int nums[] = { 1, 2, 3, 4 }; int searchValue; // ... boolean valueFound = hasValue(nums, searchValue);

  8. Control Flow: break • What if we want to exit a loop prematurely? public boolean hasValue (int[] searchIn, int searchFor) { boolean found = false; for (int i=0; i<searchIn.length; i++) { if (searchIn[i] == searchFor) { found = true; break; } } return found; } int nums[] = { 1, 2, 3, 4 }; int searchValue; // ... boolean valueFound = hasValue(nums, searchValue);

  9. break:Nested loops • What if we want to exit multiple loops prematurely? public boolean hasValue (int[][] searchIn, int searchFor) { boolean found = false; for (int i=0; i<searchIn.length; i++) { for (int j=0; j<searchIn[i].length; j++) if (searchIn[i] == searchFor) { found = true; break; } } } return found; } public boolean hasValue (int[][] searchIn, int searchFor) { boolean found = false; for (int i=0; i<searchIn.length; i++) { for (int j=0; j<searchIn[i].length; j++) if (searchIn[i] == searchFor) { found = true; break; } } if (found) break; } return found; } int nums[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; boolean found = hasValue(nums, 3);

  10. Control Flow: return • What if we want to exit a loop prematurely? public boolean hasValue (int[] searchIn, int searchFor) { for (int i=0; i<searchIn.length; i++) { if (searchIn[i] == searchFor) return true; } return false; } int nums[] = { 1, 2, 3, 4 }; int searchValue; // ... boolean valueFound = hasValue(nums, searchValue);

  11. Control Flow: continue • What if we want to skip one or more iterations? • The key word continueis used to jump to the start of the next iteration public voidprintGPAs (Student[] students) { for (Student aStudent : students) { if (aStudent == null) continue; System.out.println(aStudent.getGPA()); } } Student myClass[] = { null, null, new Student() }; printGPAs(myClass);

  12. Control Flow: break • Syntax Implications • What is the problem in this code? int myCount; int oneCount, twoCount; // ... switch (myCount) { case 1: oneCount++; break; System.out.println(“here”); case 2: twoCount++; break; // …more cases }

  13. Control Flow Statements • Syntax Implications public int getIndex (int[] searchIn, int searchFor) { int index = -1; for (int i=0; i<searchIn.length; i++) { if (searchIn[i] != searchFor) { continue; System.out.println(“Not at: “ + i); } else { index = i; break; System.out.println(“Fount at: “ + i); } } return index; } Unreachable! Unreachable!

  14. Array Exercises • Inserting into the middle of an (contiguous) array • Removing from the middle of an (contiguous) array • Reversing an array • Making an array contiguous

  15. Conclusion • Questions? • Lab 2 due on Tuesday • I will be in lab now

More Related