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 στην C

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

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

Το παρακάτω παράδειγμα βγαίνει από τον for loop όταν η μεταβλητή i είναι ίση με 13:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main() {
int i;
for (i = 0; i <= 15; i++) {
if (i == 13) {
break;
}
printf("%d ", i);
}
return 0;
}
#include <stdio.h> int main() { int i; for (i = 0; i <= 15; i++) { if (i == 13) { break; } printf("%d ", i); } return 0; }
#include <stdio.h>

int main() {
    int i;

    for (i = 0; i <= 15; i++) {
        if (i == 13) {
            break;
        }
        printf("%d ", i);
    }

    return 0;
}

Στο παραπάνω παράδειγμα, η μεταβλητή i αυξάνεται κατά 1 σε κάθε επανάληψη του for loop. Αν η μεταβλητή i είναι ίση με 13, τότε εκτελείται η δήλωση break και το πρόγραμμα βγαίνει από τον for loop. Αν η μεταβλητή i δεν είναι ίση με 13, τότε εκτυπώνεται η τιμή της.

Το αποτέλεσμα του παραπάνω παραδείγματος θα είναι η εκτύπωση των αριθμών 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 και 12 στη σειρά. Η εκτέλεση σταματά όταν η μεταβλητή i γίνει ίση με 13, και ο αριθμός 13 δεν εκτυπώνεται.

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

Το παρακάτω παράδειγμα παραλείπει την τιμή του 13:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main() {
int i;
for (i = 0; i <= 15; i++) {
if (i == 13) {
continue;
}
printf("%d ", i);
}
return 0;
}
#include <stdio.h> int main() { int i; for (i = 0; i <= 15; i++) { if (i == 13) { continue; } printf("%d ", i); } return 0; }
#include <stdio.h>

int main() {
    int i;

    for (i = 0; i <= 15; i++) {
        if (i == 13) {
            continue;
        }
        printf("%d ", i);
    }

    return 0;
}

Στο παραπάνω παράδειγμα, η μεταβλητή i αυξάνεται κατά 1 σε κάθε επανάληψη του for loop. Αν η μεταβλητή i είναι ίση με 13, τότε εκτελείται η δήλωση continue και το πρόγραμμα παραλείπει την τιμή της και συνεχίζει με την επόμενη επανάληψη του for loop. Αν η μεταβλητή i δεν είναι ίση με 13, τότε εκτυπώνεται η τιμή της.

Το αποτέλεσμα του παραπάνω παραδείγματος θα είναι η εκτύπωση των αριθμών 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 και 14 στη σειρά. Η εκτέλεση συνεχίζεται με την επόμενη τιμή της μεταβλητής i όταν η τιμή της είναι ίση με 13.

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

Παράδειγμα με break:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main() {
int i = 0;
while (i <= 10) {
if (i == 5) {
break;
}
printf("%d ", i);
i++;
}
return 0;
}
#include <stdio.h> int main() { int i = 0; while (i <= 10) { if (i == 5) { break; } printf("%d ", i); i++; } return 0; }
#include <stdio.h>

int main() {
    int i = 0;

    while (i <= 10) {
        if (i == 5) {
            break;
        }
        printf("%d ", i);
        i++;
    }

    return 0;
}

Στο παραπάνω παράδειγμα, η μεταβλητή i αυξάνεται κατά 1 σε κάθε επανάληψη του while loop. Αν η μεταβλητή i είναι ίση με 5, τότε εκτελείται η δήλωση break και το πρόγραμμα βγαίνει από το while loop. Αν η μεταβλητή i δεν είναι ίση με 5, τότε εκτυπώνεται η τιμή της.

Το αποτέλεσμα του παραπάνω παραδείγματος θα είναι η εκτύπωση των αριθμών 0, 1, 2, 3 και 4 στη σειρά. Η εκτέλεση σταματά όταν η μεταβλητή i γίνει ίση με 5, και ο αριθμός 5 δεν εκτυπώνεται.

Παράδειγμα με continue:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main() {
int i = 0;
while (i <= 10) {
if (i == 5) {
i++;
continue;
}
printf("%d ", i);
i++;
}
return 0;
}
#include <stdio.h> int main() { int i = 0; while (i <= 10) { if (i == 5) { i++; continue; } printf("%d ", i); i++; } return 0; }
#include <stdio.h>

int main() {
    int i = 0;

    while (i <= 10) {
        if (i == 5) {
            i++;
            continue;
        }
        printf("%d ", i);
        i++;
    }

    return 0;
}

Στο παραπάνω παράδειγμα, η μεταβλητή i αυξάνεται κατά 1 σε κάθε επανάληψη του while loop. Αν η μεταβλητή i είναι ίση με 5, τότε εκτελείται η δήλωση continue και το πρόγραμμα παραλείπει την τιμή και συνεχίζει με την επόμενη επανάληψη του while loop. Αν η μεταβλητή i δεν είναι ίση με 5, τότε εκτυπώνεται η τιμή της.

Το αποτέλεσμα του παραπάνω παραδείγματος θα είναι η εκτύπωση των αριθμών 0, 1, 2, 3, 4, 6, 7, 8, 9, 10 στη σειρά. Η επανάληψη παραλείπει την τιμή της μεταβλητής i όταν η τιμή της είναι ίση με 5, αλλά συνεχίζεται με την επόμενη τιμή της μεταβλητής i στην επόμενη επανάληψη του while loop.

Δωρεα μεσω Paypal

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

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

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

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