Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

3.5 Οι εντολές Break και Continue στην Java

Έχετε ήδη δει τη δήλωση break που χρησιμοποιήθηκε σε ένα προηγούμενο κεφάλαιο αυτού του εγχειριδίου. Χρησιμοποιήθηκε για να “βγει” από μια δήλωση switch.

Η δήλωση break μπορεί επίσης να χρησιμοποιηθεί για να βγείτε από έναν βρόχο.

Αυτό το παράδειγμα σταματά το βρόχο όταν το i είναι ίσο με 13:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
for (int i = 0; i < 20; i++) {
if (i == 13) {
break;
}
System.out.println(i);
}
for (int i = 0; i < 20; i++) { if (i == 13) { break; } System.out.println(i); }
for (int i = 0; i < 20; i++) {
  if (i == 13) {
    break;
  }
  System.out.println(i);
}

Στο παραπάνω παράδειγμα, ο βρόχος for επαναλαμβάνει την εντολή System.out.println(i) για όλες τις τιμές του i από το 0 έως το 19. Ωστόσο, όταν το i γίνει ίσο με 13, η δήλωση break σταματά τον βρόχο και βγαίνει από αυτόν. Έτσι, η εκτέλεση του προγράμματος θα σταματήσει μετά την εκτύπωση όλων των τιμών i από το 0 έως το 12.

Η δήλωση continue διακόπτει μια επανάληψη (στο βρόχο), αν συμβεί μια συγκεκριμένη συνθήκη και συνεχίζει με την επόμενη επανάληψη στον βρόχο.

Αυτό το παράδειγμα παραλείπει την τιμή 13:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
for (int i = 0; i < 20; i++) {
if (i == 13) {
continue;
}
System.out.println(i);
}
for (int i = 0; i < 20; i++) { if (i == 13) { continue; } System.out.println(i); }
for (int i = 0; i < 20; i++) {
  if (i == 13) {
    continue;
  }
  System.out.println(i);
}

Στο παραπάνω παράδειγμα, ο βρόχος for επαναλαμβάνει την εντολή System.out.println(i) για όλες τις τιμές του i από το 0 έως το 19. Ωστόσο, όταν το i γίνει ίσο με 13, η δήλωση continue παραλείπει την τιμή του 13 και συνεχίζει με την επόμενη τιμή του i. Έτσι, η εκτέλεση του προγράμματος θα εκτυπώσει όλες τις τιμές i από το 0 έως το 19 εκτός από το 13.

Μπορείτε επίσης να χρησιμοποιήσετε τις δηλώσεις break και continue στους while βρόχους:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
int i = 0;
while (i < 10) {
if (i == 4) {
break;
}
i++;
if (i == 2) {
continue;
}
System.out.println(i);
}
int i = 0; while (i < 10) { if (i == 4) { break; } i++; if (i == 2) { continue; } System.out.println(i); }
int i = 0;
while (i < 10) {
  if (i == 4) {
    break;
  }
  i++;
  if (i == 2) {
    continue;
  }
  System.out.println(i);
}

Στο παραπάνω παράδειγμα, ο βρόχος while επαναλαμβάνει την εντολή System.out.println(i) για όλες τις τιμές του i από το 1 έως το 3 και από το 5 έως το 9. Ωστόσο, όταν το i γίνει ίσο με 4, η δήλωση break σταματά τον βρόχο και βγαίνει από αυτόν. Όταν το i γίνει ίσο με 2, η δήλωση continue παραλείπει την τιμή του 2 και συνεχίζει με την επόμενη τιμή του i.

Δωρεα μεσω Paypal

Για την κάλυψη αναγκών φιλοξενίας και δημιουργίας περιεχομένου.

κατηγοριες μαθηματων

Ιστορικο ενοτητων

top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων