java:control_flow
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java:control_flow [2015/02/09 05:10] – gthanos | java:control_flow [2020/02/21 10:41] (current) – gthanos | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
- | Αναφορικά | + | Συμπληρωματικά, για |
- | ===== if - else if - else statement ===== | + | * [[java: |
+ | * [[java: | ||
+ | * [[java: | ||
- | Ανάλογά με την γλώσσα C. Δείτε το παρακάτω παράδειγμα. | ||
- | <code java IfElseDemo.java> | ||
- | class IfElseDemo { | ||
- | public static void main(String[] args) { | ||
- | int testscore = 76; | + | | Προηγούμενο: [[:java:operators | Τελεστές |
- | char grade; | + | |
- | + | ||
- | if (testscore >= 90) { | + | |
- | grade = ' | + | |
- | } else if (testscore >= 80) { | + | |
- | grade = ' | + | |
- | } else if (testscore >= 70) { | + | |
- | grade = ' | + | |
- | } else if (testscore >= 60) { | + | |
- | grade = ' | + | |
- | } else { | + | |
- | grade = ' | + | |
- | } | + | |
- | System.out.println(" | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | ===== Switch statement ===== | + | |
- | + | ||
- | Σε αναλογία με την γλώσσα C, η Java υποστηρίζει την εντολή switch. Η ιδιαιτερότητα της switch στην Java είναι ότι υποστηρίζει ακεραίους, | + | |
- | + | ||
- | <code java SwitchDemo.java> | + | |
- | public class SwitchDemo { | + | |
- | public static void main(String[] args) { | + | |
- | + | ||
- | int month = 8; | + | |
- | String monthString; | + | |
- | switch (month) { | + | |
- | case 1: | + | |
- | | + | |
- | case 2: monthString = " | + | |
- | | + | |
- | case 3: monthString = " | + | |
- | | + | |
- | case 4: monthString = " | + | |
- | | + | |
- | case 5: monthString = " | + | |
- | | + | |
- | case 6: monthString = " | + | |
- | | + | |
- | case 7: monthString = " | + | |
- | | + | |
- | case 8: monthString = " | + | |
- | | + | |
- | case 9: monthString = " | + | |
- | | + | |
- | case 10: monthString = " | + | |
- | | + | |
- | case 11: monthString = " | + | |
- | | + | |
- | case 12: monthString = " | + | |
- | | + | |
- | default: monthString = " | + | |
- | | + | |
- | } | + | |
- | System.out.println(monthString); | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | <code java StringSwitchDemo.java> | + | |
- | public class StringSwitchDemo { | + | |
- | + | ||
- | public static int getMonthNumber(String month) { | + | |
- | + | ||
- | int monthNumber = 0; | + | |
- | + | ||
- | if (month == null) { | + | |
- | return monthNumber; | + | |
- | } | + | |
- | + | ||
- | switch (month.toLowerCase()) { | + | |
- | case " | + | |
- | monthNumber = 1; | + | |
- | break; | + | |
- | case " | + | |
- | monthNumber = 2; | + | |
- | break; | + | |
- | case " | + | |
- | monthNumber = 3; | + | |
- | break; | + | |
- | case " | + | |
- | monthNumber = 4; | + | |
- | break; | + | |
- | case " | + | |
- | monthNumber = 5; | + | |
- | break; | + | |
- | case " | + | |
- | monthNumber = 6; | + | |
- | break; | + | |
- | case " | + | |
- | monthNumber = 7; | + | |
- | break; | + | |
- | case " | + | |
- | monthNumber = 8; | + | |
- | break; | + | |
- | case " | + | |
- | monthNumber = 9; | + | |
- | break; | + | |
- | case " | + | |
- | monthNumber = 10; | + | |
- | break; | + | |
- | case " | + | |
- | monthNumber = 11; | + | |
- | break; | + | |
- | case " | + | |
- | monthNumber = 12; | + | |
- | break; | + | |
- | default: | + | |
- | monthNumber = 0; | + | |
- | break; | + | |
- | } | + | |
- | + | ||
- | return monthNumber; | + | |
- | } | + | |
- | + | ||
- | public static void main(String[] args) { | + | |
- | + | ||
- | String month = " | + | |
- | + | ||
- | int returnedMonthNumber = | + | |
- | StringSwitchDemo.getMonthNumber(month); | + | |
- | + | ||
- | if (returnedMonthNumber == 0) { | + | |
- | System.out.println(" | + | |
- | } else { | + | |
- | System.out.println(returnedMonthNumber); | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | ===== While και do-while statements ===== | + | |
- | + | ||
- | + | ||
- | <code java WhileDemo.java> | + | |
- | class WhileDemo { | + | |
- | public static void main(String[] args){ | + | |
- | int count = 1; | + | |
- | while (count < 11) { | + | |
- | System.out.println(" | + | |
- | count++; | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | < | + | |
- | class DoWhileDemo { | + | |
- | public static void main(String[] args){ | + | |
- | int count = 1; | + | |
- | do { | + | |
- | System.out.println(" | + | |
- | count++; | + | |
- | } while (count < 11); | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | ===== for statement ===== | + | |
- | + | ||
- | Η εντολή '' | + | |
- | + | ||
- | <code java EnhancedForDemo.java> | + | |
- | class EnhancedForDemo { | + | |
- | public static void main(String[] args){ | + | |
- | int[] numbers = | + | |
- | | + | |
- | for (int item : numbers) { | + | |
- | | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | ===== Branching Statements ===== | + | |
- | ==== break ==== | + | |
- | + | ||
- | Η εντoλή '' | + | |
- | + | ||
- | <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// μορφή της εντολής '' | + | |
- | + | ||
- | <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; | + | |
- | j++) { | + | |
- | if (arrayOfInts[i][j] == searchfor) { | + | |
- | foundIt = true; | + | |
- | break search; | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | if (foundIt) { | + | |
- | System.out.println(" | + | |
- | } else { | + | |
- | System.out.println(searchfor + " not in the array" | + | |
- | } | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | Η ροή του | + | |
- | + | ||
- | ==== 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" : " | + | |
- | } | + | |
- | } | + | |
- | </ | + | |
- | + | ||
- | |[[: | + | |
java/control_flow.1423458642.txt.gz · Last modified: 2015/02/09 05:10 by gthanos