This shows you the differences between two versions of the page.
|
java:break_loop_statements [2017/02/16 08:09] gthanos |
java:break_loop_statements [2017/02/16 08:09] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Εντολές τερματισμού και ελέγχου της επανάληψης ====== | ||
| - | ===== Η εντολή break ===== | ||
| - | |||
| - | Η εντoλή **break** τερματίζει την υφιστάμενη επανάληψη και θέτει την ροή του προγράμματος αμέσως μετά το τέλος της εντολής επανάληψης. Η εντολή **break** έχει δύο μορφές, | ||
| - | |||
| - | <code java BreakDemo.java> | ||
| - | class BreakDemo { | ||
| - | public static void main(String[] args) { | ||
| - | |||
| - | int[] arrayOfInts = | ||
| - | { 32, 87, 3, 589, | ||
| - | 12, 1076, 2000, | ||
| - | 8, 622, 127 }; | ||
| - | int searchfor = 12; | ||
| - | |||
| - | int i; | ||
| - | boolean foundIt = false; | ||
| - | |||
| - | for (i = 0; i < arrayOfInts.length; | ||
| - | if (arrayOfInts[i] == searchfor) { | ||
| - | foundIt = true; | ||
| - | break; | ||
| - | } | ||
| - | } | ||
| - | |||
| - | if (foundIt) { | ||
| - | System.out.println(" | ||
| - | } else { | ||
| - | System.out.println(searchfor + " not in the array" | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Η //labeled// μορφή της εντολής **break** έχει νόημα όταν έχουμε περισσότερες από μία εμφωλευμένες εντολές επανάληψης **while**, **do-while**, | ||
| - | |||
| - | <code java BreakWithLabelDemo.java> | ||
| - | class BreakWithLabelDemo { | ||
| - | public static void main(String[] args) { | ||
| - | |||
| - | int[][] arrayOfInts = { | ||
| - | { 32, 87, 3, 589 }, | ||
| - | { 12, 1076, 2000, 8 }, | ||
| - | { 622, 127, 77, 955 } | ||
| - | }; | ||
| - | int searchfor = 12; | ||
| - | |||
| - | int i; | ||
| - | int j = 0; | ||
| - | boolean foundIt = false; | ||
| - | |||
| - | search: | ||
| - | for (i = 0; i < arrayOfInts.length; | ||
| - | for (j = 0; j < arrayOfInts[i].length; | ||
| - | if (arrayOfInts[i][j] == searchfor) { | ||
| - | foundIt = true; | ||
| - | break search; | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | |||
| - | if (foundIt) { | ||
| - | System.out.println(" | ||
| - | } else { | ||
| - | System.out.println(searchfor + " not in the array" | ||
| - | } | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Εκτελέστε τα προγράμματα για να διαπιστώσετε την λειτουργία της κανονικής και της //labeled// μορφής της εντολής **break**. Η ροή του προγράμματος συνεχίζεται μετά τις δύο εντολές επανάληψης **for**. | ||
| - | |||
| - | ===== Η εντολή continue ===== | ||
| - | |||
| - | Σε αναλογία με την εντολή '' | ||
| - | |||
| - | <code java ContinueDemo.java> | ||
| - | class ContinueDemo { | ||
| - | public static void main(String[] args) { | ||
| - | |||
| - | String searchMe = "peter piper picked a " + "peck of pickled peppers"; | ||
| - | int max = searchMe.length(); | ||
| - | int numPs = 0; | ||
| - | |||
| - | for (int i = 0; i < max; i++) { | ||
| - | // interested only in p's | ||
| - | if (searchMe.charAt(i) != ' | ||
| - | continue; | ||
| - | |||
| - | // process p's | ||
| - | numPs++; | ||
| - | } | ||
| - | System.out.println(" | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <code java ContinueWithLabelDemo.java> | ||
| - | class ContinueWithLabelDemo { | ||
| - | public static void main(String[] args) { | ||
| - | |||
| - | String searchMe = "Look for a substring in me"; | ||
| - | String substring = " | ||
| - | boolean foundIt = false; | ||
| - | |||
| - | int max = searchMe.length() - | ||
| - | substring.length(); | ||
| - | |||
| - | test: | ||
| - | for (int i = 0; i <= max; i++) { | ||
| - | int n = substring.length(); | ||
| - | int j = i; | ||
| - | int k = 0; | ||
| - | while (n-- != 0) { | ||
| - | if (searchMe.charAt(j++) != substring.charAt(k++)) { | ||
| - | continue test; | ||
| - | } | ||
| - | } | ||
| - | foundIt = true; | ||
| - | break test; | ||
| - | } | ||
| - | System.out.println(foundIt ? "Found it" : " | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Εκτελέστε τα προγράμματα για να διαπιστώσετε την λειτουργία της κανονικής και της //labeled// μορφής της εντολής **continue**. | ||
| - | |||
| - | | Προηγούμενο: | ||