This is an old revision of the document!
Table of Contents
Έλεγχος ροής προγράμματος
Συμπληρωματικά, για τον έλεγχο ροής προγράμματος μπορείτε (for,while,do-while,if,switch κλπ) να ανατρέξετε στο documentation της Oracle εδώ.
if - else if - else statement
Ανάλογά με την γλώσσα C. Δείτε το παρακάτω παράδειγμα.
- IfElseDemo.java
class IfElseDemo { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } }
Switch statement
Σε αναλογία με την γλώσσα C, η Java υποστηρίζει την εντολή switch. Η ιδιαιτερότητα της switch στην Java είναι ότι υποστηρίζει ακεραίους, enumerated types ή αλφαριθμητικά. Δείτε τα παρακάτω παραδείγματα με χρήση ακεραίων και αλφαριμθητικών.
- SwitchDemo.java
public class SwitchDemo { public static void main(String[] args) { int month = 8; String monthString; switch (month) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; } System.out.println(monthString); } }
- StringSwitchDemo.java
public class StringSwitchDemo { public static int getMonthNumber(String month) { int monthNumber = 0; if (month == null) { return monthNumber; } switch (month.toLowerCase()) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; case "march": monthNumber = 3; break; case "april": monthNumber = 4; break; case "may": monthNumber = 5; break; case "june": monthNumber = 6; break; case "july": monthNumber = 7; break; case "august": monthNumber = 8; break; case "september": monthNumber = 9; break; case "october": monthNumber = 10; break; case "november": monthNumber = 11; break; case "december": monthNumber = 12; break; default: monthNumber = 0; break; } return monthNumber; } public static void main(String[] args) { String month = "August"; int returnedMonthNumber = StringSwitchDemo.getMonthNumber(month); if (returnedMonthNumber == 0) { System.out.println("Invalid month"); } else { System.out.println(returnedMonthNumber); } } }
While και do-while statements
- WhileDemo.java
class WhileDemo { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } } }
- DoWhileDemo.java
class DoWhileDemo { public static void main(String[] args){ int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11); } }
for statement
Η εντολή for συντάσσεται όπως και στην γλώσσα προγραμματισμού C. Ενδιαφέρον έχει μία διαφοροποιημένη έκδοση της for που υποστηρίζει η Java με σκοπό την ανακύκλωση σε όλα τα μέλη ενός πίνακα (ή ενός Collection όπως θα δούμε αργότερα). Δείτε το παρακάτω παράδειγμα.
- EnhancedForDemo.java
class EnhancedForDemo { public static void main(String[] args){ int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for (int item : numbers) { System.out.println("Count is: " + item); } } }
Branching Statements
Η εντολή break
Η εντoλή break
έχει δύο μορφές την labeled και την unlabeled μορφή. Η unlabeled μορφή είναι η μορφή που γνωρίζετε από την γλώσσα C. Ένα παράδειγμα δίνουμε παρακάτω.
- 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; i++) { if (arrayOfInts[i] == searchfor) { foundIt = true; break; } } if (foundIt) { System.out.println("Found " + searchfor + " at index " + i); } else { System.out.println(searchfor + " not in the array"); } } }
Η labeled μορφή της εντολής break
έχει νόημα όταν έχουμε περισσότερες από μία εμφωλευμένες εντολές ανακύκλωσης while
, do-while
, for
και θέλουμε να τερματίζουμε την εξωτερική ανακύκλωση. Ένα παράδειγμα χρήσης της εντολής labeled break
μπορείτε να δείτε παρακάτω.
- 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; i++) { for (j = 0; j < arrayOfInts[i].length; j++) { if (arrayOfInts[i][j] == searchfor) { foundIt = true; break search; } } } if (foundIt) { System.out.println("Found " + searchfor + " at " + i + ", " + j); } else { System.out.println(searchfor + " not in the array"); } } }
Η ροή του προγράμματος συνεχίζεται μετά τις δύο εντολές ανακύκλωσης for
.
Η εντολή continue
Σε αναλογία με την εντολή for
και η εντολή continue
διαθέτει labeled και unlabeled μορφή. Δείτε τα παρακάτω παραδείγματα
- 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) != 'p') continue; // process p's numPs++; } System.out.println("Found " + numPs + " p's in the string."); } }
- ContinueWithLabelDemo.java
class ContinueWithLabelDemo { public static void main(String[] args) { String searchMe = "Look for a substring in me"; String substring = "sub"; 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" : "Didn't find it"); } }