Conditional loop: Βρόχος υπό συνθήκη
Είναι ο βρόχος που μπορεί να γίνει infinite loop (ατέρμονος βρόχος).
Άρα, "δυνάμει ατέρμονος βρόχος", θα έλεγα.
Έχω την εντύπωση πως εδώ το "
postconditional" έχει να κάνει πιο πολύ με τον
έλεγχο των συνθηκών (αν είναι αληθείς ή ψευδείς) κατόπιν εκτελέσεως του μπλοκ των εντολών:
Loop - Αποδίδεται στα ελληνικά με τον όρο βρόχος και είναι ένα σύνολο από εντολές που εκτελούνται είτε συνεχώς μέχρι να πάψει να ισχύει κάποια συνθήκη είτε για έναν καθορισμένο αριθμό επαναλήψεων. Οι εντολές για τη δημιουργία βρόχων είναι οι while, do ... while, for και for ... in...
...Ο βρόχος (loop) for ζητάει από τον υπολογιστή να εκτελέσει μια συγκεκριμένη εργασία έναν ορισμένο αριθμό φορών. Ο βρόχος do ... while εκτελεί μια ομάδα εντολών μία φορά τουλάχιστον και μετά ελέγχει μια συνθήκη και αν αυτή είναι αληθής (true), τότε επαναλαμβάνει την ομάδα εντολών...
...if-else Η εντολή if μας βοηθάει στον έλεγχο μίας λογικής έκφρασης (της συνθήκης) και αν είναι αληθής εκτελούνται μία ή περισσότερες εντολές ενώ αν είναι ψευδής δεν εκτελούνται...
Η εντολή ή εντολές που ακολουθούν το while θα εκτελεστούν όσο η συνθήκη είναι αληθής. Ο βρόχος δηλαδή θα τερματιστεί όταν η συνθήκη - που είναι μία boolean έκφραση - γίνει ψευδής.
...Υπάρχουν κάποιες περιπτώσεις στις οποίες θα θέλαμε οι εντολές μέσα στο βρόχο να εκτελεστούν τουλάχιστον μία φορά και στη συνέχεια να ελεγχθεί η συνθήκη εξόδου. Σ’ αυτές τις περιπτώσεις προτιμάται η χρήση της do-while αντί της while...
...Στην
do-while πρώτα εκτελούνται οι εντολές και στη συνέχεια ελέγχεται η συνθήκη. Ο βρόχος τερματίζεται αν η συνθήκη βρεθεί ψευδής...
Η συνθήκη ελέγχεται. Αν είναι αληθής, οι εντολές μέσα στο while εκτελούνται και η
συνθήκη ελέγχεται και πάλι. Οι εντολές μέσα στο while εκτελούνται ενόσω η συνθήκη
είναι αληθής. Όταν η συνθήκη γίνει ψευδής, το while τερματίζει, οι εντολές μέσα σε
αυτό αγνοούνται και το πρόγραμμα συνεχίζει την εκτέλεση του μετά το while
καιThere are two primary types of loops in any programming language, I'll call them "
pre-conditional" and "
post-conditional".
The
pre-conditional loops execute a conditional expression first, and
if and only if that statement evaluates to true, the contents of the loop are executed. After each execution of the loop, the
conditional expression is re-evaluated and until it is false, the contents of the loop will continue to execute.
Always ensure that the conditional will eventually evaluate as false, otherwise you will be stuck within an infinite loop which will never stop executing.
The first type of pre-conditional loop is the "while" loop. The keyword while is used to define the condition, then the loop contents follow, contained within brackets similar to if statements.
The next type of pre-conditional loop in C is the for loop. The for loop is useful when you have a defined number of iterations you want your loop to perform, and you have the counter built into the loop itself.
The C programming language also provides a
post-conditional loop.
The primary difference of the post-conditional loop is that the conditional comparison occurs after the loop executes, so the loop contents will always execute at least once.This type of loop is created in C using the
do keyword, with the
while statement and condition following the
loop body. (βλέπε..Στην
do-while πρώτα εκτελούνται οι εντολές και στη συνέχεια ελέγχεται η συνθήκη)
do
while loop
The do
while loop can be used till a condition returns false. It looks like this:
do{
// do something here
} while (condition)
This loop runs one time in any case, whether the condition is true or false. After that it will check for the condition.καιPre-conditional loopThe statements in the body are executed
as long as the condition presented by the Boolean expression is fulfilled (evaluates to true). The condition is tested before every repeat-cycle is started.
Post-conditional loopThe statements in the loop-body are executed, until the by the boolean expression presented condition is fulfilled.
The condition is tested after every repeat-cycle. If the loop is interrupted processing proceeds with the statement following the until.
The statements in the loop-body are always executed at least once.
Postconditional loop (the actions in the loop body are executed once and the loop condition occurs afterwards)
while() loop begins by testing its condition, and so, the body of a while() loop executes no, one or many times. In comparison, a dowhile() loop automatically executes the body of its loop and then tests its condition. As a result, the body of a dowhile() loop executes one or more times
The difference between the do
while and the while
do loops is that the code statements within the do
while loop always execute at least once; expression isn't evaluated until the loop has completed its first cycle. Therefore, such a loop executes at least once, regardless of the value of expression. The while loop evaluates the expression first; therefore, the statements associated with it may not execute at all.
Μια δομή επανάληψης η οποία εκτελείται έπ’ αόριστον ονομάζεται ατέρμων βρόχος...
Το BLOCK εντολών εκτελείται όσο ισχύει η συνθήκη ελέγχου. Αυτό σημαίνει
ότι μπορεί να μην εκτελεστεί ποτέ, εάν κατά τον πρώτο έλεγχο η συνθήκη δεν
αληθεύει. Μπορεί, όμως, να εκτελείται και συνεχώς (ατέρμων βρόχος) όταν, για
παράδειγμα το σετ εντολών δεν επηρεάζει την τιμή της μεταβλητής που βρίσκεται στη
συνθήκη ελέγχου. ...
Α, κι απ' ό,τι βλέπω, το "loop" στην περίπτωση αυτή αποδίδεται ως "βρόχος επανάληψης" ή "επαναληπτικός βρόχος".δομές ελέγχου επανάληψης: μια τρίτη εντολή επανάληψης που χρησιμοποιεί τις λέξεις κλειδιά do-while
Η διαφορά της από τις προηγούμενες είναι ότι ελέγχει τη συνθήκη στο τέλος και όχι στην αρχή.
Η σύνταξη της εντολής do-while είναι η παρακάτω:
do
εντολή;
while (συνθήκη);
Υπάρχουν κάποιες περιπτώσεις στις οποίες θα θέλαμε οι εντολές μέσα στο
βρόχο να εκτελεστούν τουλάχιστον μία φορά και
στη συνέχεια να ελεγχθεί η
συνθήκη εξόδου. Σ’ αυτές τις περιπτώσεις προτιμάται η χρήση της do- while αντί της while.
Στην do-while πρώτα εκτελούνται οι εντολές και στη συνέχεια ελέγχεται η συνθήκη. Ο
βρόχος τερματίζεται αν η συνθήκη βρεθεί ψευδής.