This is an old revision of the document!
class WhileDemo { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } } }
class DoWhileDemo { public static void main(String[] args){ int count = 1; do { System.out.println("Count is: " + count); count++; } while (count < 11); } }
Η εντολή for συντάσσεται όπως και στην γλώσσα προγραμματισμού C.
class ForDemo { public static void main(String[] args){ int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for (int i=0; i<numbers.length;i++) { System.out.println("Count is: " + numbers[i]); } } }
Ενδιαφέρον έχει μία διαφοροποιημένη έκδοση της for που υποστηρίζει η Java με σκοπό την ανακύκλωση σε όλα τα μέλη ενός πίνακα (ή ενός Collection όπως θα δούμε αργότερα). Δείτε το παρακάτω παράδειγμα, χρήσης του συγκεκριμένου τελεστή για την ανακύκλωση στα μέλη ενός πίνακα.
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); } } }