150 likes | 261 Vues
This lecture outlines the fundamental concepts of control flow in programming, focusing on how to manipulate the default linear execution of statements using conditional statements and loops. Key topics include the use of `if`, `else`, and `switch` statements for decision-making, as well as loop constructs such as `for`, `while`, and `do...while`. The lecture further explores the `break`, `continue`, and `return` statements to control program execution flow, especially in complex scenarios involving nested loops. Array exercises and practical examples demonstrate these concepts.
E N D
CSE 501NFall ‘0908: 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 • 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
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
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 }
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 }
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);
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);
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);
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);
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);
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 }
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!
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
Conclusion • Questions? • Lab 2 due on Tuesday • I will be in lab now