User Tools

Site Tools


java:break_loop_statements

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
java:break_loop_statements [2015/09/18 10:47] – created gthanosjava:break_loop_statements [2017/02/16 08:09] (current) – external edit 127.0.0.1
Line 1: Line 1:
-====== Εντολές τερματισμού ή ελέγχου της ανακύκλωσης ======+====== Εντολές τερματισμού και ελέγχου της επανάληψης ======
  
-==== Η εντολή break ====+===== Η εντολή break =====
  
-Η εντoλή **break** τερματίζει την υφιστάμενη ανακύκλωση και θέτει την ροή του προγράμματος αμέσως μετά το τέλος της εντολής ανακύκλωσης. Η εντολή **break** έχει δύο μορφές την //labeled// και την //unlabeled// μορφή. Η unlabeled μορφή είναι η μορφή που γνωρίζετε από την γλώσσα **C**. Ένα της παράδειγμα της unlabeled μορφής δίνουμε παρακάτω.+Η εντoλή **break** τερματίζει την υφιστάμενη επανάληψη και θέτει την ροή του προγράμματος αμέσως μετά το τέλος της εντολής επανάληψης. Η εντολή **break** έχει δύο μορφέςτην //labeled// και την //unlabeled// μορφή. Η unlabeled μορφή είναι η μορφή που γνωρίζετε από την γλώσσα **C**. Ένα της παράδειγμα της unlabeled μορφής δίνουμε παρακάτω.
  
 <code java BreakDemo.java> <code java BreakDemo.java>
 class BreakDemo { class BreakDemo {
-    public static void main(String[] args) {+  public static void main(String[] args) {
  
-        int[] arrayOfInts =  +    int[] arrayOfInts =  
-            { 32, 87, 3, 589, +      { 32, 87, 3, 589, 
-              12, 1076, 2000, +        12, 1076, 2000, 
-              8, 622, 127 }; +        8, 622, 127 }; 
-        int searchfor = 12;+    int searchfor = 12;
  
-        int i; +    int i; 
-        boolean foundIt = false;+    boolean foundIt = false;
  
-        for (i = 0; i < arrayOfInts.length; i++) { +    for (i = 0; i < arrayOfInts.length; i++) { 
-            if (arrayOfInts[i] == searchfor) { +      if (arrayOfInts[i] == searchfor) { 
-                foundIt = true; +        foundIt = true; 
-                break; +        break; 
-            +      
-        }+    }
  
-        if (foundIt) { +    if (foundIt) { 
-            System.out.println("Found " + searchfor + " at index " + i); +      System.out.println("Found " + searchfor + " at index " + i); 
-        } else { +    } else { 
-            System.out.println(searchfor + " not in the array"); +      System.out.println(searchfor + " not in the array");
-        }+
     }     }
 +  }
 } }
 </code> </code>
  
-Η //labeled// μορφή της εντολής **break** έχει νόημα όταν έχουμε περισσότερες από μία εμφωλευμένες εντολές ανακύκλωσης **while**, **do-while**, **for** και θέλουμε να τερματίζουμε την εξωτερική ανακύκλωση. Ένα παράδειγμα χρήσης της εντολής //labeled// **break** μπορείτε να δείτε παρακάτω.+Η //labeled// μορφή της εντολής **break** έχει νόημα όταν έχουμε περισσότερες από μία εμφωλευμένες εντολές επανάληψης **while**, **do-while**, **for** και θέλουμε να τερματίσουμε την εξωτερική επανάληψη. Ένα παράδειγμα χρήσης της εντολής //labeled// **break** μπορείτε να δείτε παρακάτω.
  
 <code java BreakWithLabelDemo.java> <code java BreakWithLabelDemo.java>
 class BreakWithLabelDemo { class BreakWithLabelDemo {
-    public static void main(String[] args) {+  public static void main(String[] args) {
  
-        int[][] arrayOfInts = {  +    int[][] arrayOfInts = {  
-            { 32, 87, 3, 589 }, +      { 32, 87, 3, 589 }, 
-            { 12, 1076, 2000, 8 }, +      { 12, 1076, 2000, 8 }, 
-            { 622, 127, 77, 955 } +      { 622, 127, 77, 955 } 
-        }; +    }; 
-        int searchfor = 12;+    int searchfor = 12;
  
-        int i; +    int i; 
-        int j = 0; +    int j = 0; 
-        boolean foundIt = false;+    boolean foundIt = false;
  
-    search: +  search: 
-        for (i = 0; i < arrayOfInts.length; i++) { +    for (i = 0; i < arrayOfInts.length; i++) { 
-            for (j = 0; j < arrayOfInts[i].length; +      for (j = 0; j < arrayOfInts[i].length; j++) { 
-                 j++) { +        if (arrayOfInts[i][j] == searchfor) { 
-                if (arrayOfInts[i][j] == searchfor) { +          foundIt = true; 
-                    foundIt = true; +          break search;
-                    break search; +
-                } +
-            }+
         }         }
 +      }
 +    }
  
-        if (foundIt) { +    if (foundIt) { 
-            System.out.println("Found " + searchfor + " at " + i + ", " + j); +      System.out.println("Found " + searchfor + " at " + i + ", " + j); 
-        } else { +    } else { 
-            System.out.println(searchfor + " not in the array"); +      System.out.println(searchfor + " not in the array");
-        }+
     }     }
 +  }
 } }
 </code> </code>
  
-Η ροή του προγράμματος συνεχίζεται μετά τις δύο εντολές ανακύκλωσης **for**. +Εκτελέστε τα προγράμματα για να διαπιστώσετε την λειτουργία της κανονικής και της //labeled// μορφής της εντολής **break**. Η ροή του προγράμματος συνεχίζεται μετά τις δύο εντολές επανάληψης **for**.
  
-==== Η εντολή continue ====+===== Η εντολή continue =====
  
-Σε αναλογία με την εντολή ''for'' και η εντολή ''continue'' διαθέτει //labeled// και //unlabeled// μορφή. Δείτε τα παρακάτω παραδείγματα+Σε αναλογία με την εντολή ''for'' και η εντολή ''continue'' διαθέτει //labeled// και //unlabeled// μορφή. Δείτε τα παρακάτω παραδείγματα:
  
 <code java ContinueDemo.java> <code java ContinueDemo.java>
 class ContinueDemo { class ContinueDemo {
-    public static void main(String[] args) {+  public static void main(String[] args) {
  
-        String searchMe = "peter piper picked a " + "peck of pickled peppers"; +    String searchMe = "peter piper picked a " + "peck of pickled peppers"; 
-        int max = searchMe.length(); +    int max = searchMe.length(); 
-        int numPs = 0;+    int numPs = 0;
  
-        for (int i = 0; i < max; i++) { +    for (int i = 0; i < max; i++) { 
-            // interested only in p's +      // interested only in p's 
-            if (searchMe.charAt(i) != 'p'+      if (searchMe.charAt(i) != 'p'
-                continue;+        continue;
  
-            // process p's +      // process p's 
-            numPs+++      numPs++;
-        } +
-        System.out.println("Found " + numPs + " p's in the string.");+
     }     }
 +    System.out.println("Found " + numPs + " p's in the string.");
 +  }
 } }
 </code> </code>
Line 100: Line 99:
 <code java ContinueWithLabelDemo.java> <code java ContinueWithLabelDemo.java>
 class ContinueWithLabelDemo { class ContinueWithLabelDemo {
-    public static void main(String[] args) {+  public static void main(String[] args) {
  
-        String searchMe = "Look for a substring in me"; +    String searchMe = "Look for a substring in me"; 
-        String substring = "sub"; +    String substring = "sub"; 
-        boolean foundIt = false;+    boolean foundIt = false;
  
-        int max = searchMe.length() -  +    int max = searchMe.length() -  
-                  substring.length();+          substring.length();
  
-    test: +  test: 
-        for (int i = 0; i <= max; i++) { +    for (int i = 0; i <= max; i++) { 
-            int n = substring.length(); +      int n = substring.length(); 
-            int j = i; +      int j = i; 
-            int k = 0; +      int k = 0; 
-            while (n-- != 0) { +      while (n-- != 0) { 
-                if (searchMe.charAt(j++) != substring.charAt(k++)) { +        if (searchMe.charAt(j++) != substring.charAt(k++)) { 
-                    continue test; +          continue test;
-                } +
-            } +
-            foundIt = true; +
-                break test;+
         }         }
-        System.out.println(foundIt ? "Found it" : "Didn't find it");+      } 
 +      foundIt = true; 
 +        break test;
     }     }
 +    System.out.println(foundIt ? "Found it" : "Didn't find it");
 +  }
 } }
 </code> </code>
  
 +Εκτελέστε τα προγράμματα για να διαπιστώσετε την λειτουργία της κανονικής και της //labeled// μορφής της εντολής **continue**.
  
 +| Προηγούμενο: [[:java:loop_statements | Εντολές επανάληψης ]] | [[:toc | Περιεχόμενα ]] | Επόμενο: [[oop:introduction| Εισαγωγή στον Αντικειμενοστραφή Προγραμματισμό ]] |
java/break_loop_statements.1442573246.txt.gz · Last modified: 2015/09/18 09:47 (external edit)