7. Δομές Ελέγχου (Control Statements) Χειμερινό εξάμηνο 2012 Πέτρος Κωμοδρόμος komodromos@ucy.ac.cy http://www.eng.ucy.ac.cy/petros 1
Θέματα Αλγόριθμοι, δομές ελέγχου και ψευδοκώδικας Δομές ελέγχου στη Java Επιλεκτικές δομές ελέγχου: if, if else, switch, (? : ) Επαναληπτικές δομές ελέγχου: while, for, do.while Πολλαπλοί τελεστές ορισμού: +=, -=, /=, *=, %= Αυξητικοί και μειωτικοί τελεστές: ++, -- Εντολές break και continue Λογικοί τελεστές 2
Αλγόριθμοι, Δομές ελέγχου και Ψευδοκώδικας Αλγόριθμος (algorithm) Σειρά ενεργειών που πρέπει να γίνουν με συγκεκριμένη σειρά, ανάλογα και με τα δεδομένα, για να επιλυθεί ένα πρόβλημα Σειριακή εκτέλεση (sequential execution) Δομές ελέγχου (control structures transfer of control) Μπορούν να χρησιμοποιηθούν για να καθοριστούν οι εντολές και η σειρά με την οποία θα πρέπει να εκτελεστούν, δηλαδή χρησιμοποιούνται για τον έλεγχο της ροής ενός προγράμματος Ψευδοκώδικας (pseudo-code) Έκφραση με απλές περιγραφικές λέξεις των εργασιών που πρέπει να γίνουν για να υλοποιηθεί ένας αλγόριθμος 3
Δομές Ελέγχου (Control Structures) στη Java Επιλεκτικές δομές ελέγχου (selection control structures) if if else switch Επαναληπτικές δομές ελέγχου (repetition control structures) while for do.while Δομημένος προγραμματισμός (structured programming) 4
Επιλεκτικές δομές ελέγχου - Εντολές ελέγχου διακλάδωσης if (logical test) statement; if (logical test) { } statements. if (logical test) statement; else if (logical test) statement; else statement; Conditional Operator (? : ) (π.χ. maxab= a>b?a:b ) switch (variable) { case value1: statements break; case value3: statements break; case value4: case value5: statements break; default: statements } 5
Επαναληπτικές δομές ελέγχου Εντολές ελέγχου επαναλήψεων (βρόγχων-loops) for (initialization ; logical test; modification) statement; { } for (initialization ; logical test; modification) statements { } while (logical test) statement; while (logical test) statements do { statements; } while (logical test); 6
Unified Modeling Language (UML): Διαγράμματα Ενεργειών (activity diagrams) Τα διαγράμματα ενεργειών (activity diagrams) χρησιμοποιούνται για να καθορίσουν την σειρά εκτελέσεως εντολών τμήματος ενός προγράμματος, εναλλακτικά/παράλληλα του ψευδοκώδικα Καθορίζουν με συγκεκριμένα σύμβολα τις ενέργειες που πρέπει να γίνουν, τις δομές ελέγχου και που χρησιμοποιούνται σκιαγραφώντας τα κύρια στοιχεία ενός αλγόριθμου www.uml.org 7
UML Διαγράμματα Ενεργειών (activity diagrams) Στρογγυλευμένα ορθογώνια καθορίζουν ενέργειες που πρέπει να γίνουν Ρόμβοι (diamonds) συμβολίζουν: σημεία αποφάσεων (decision symbols) σημεία συγχώνευσης (merge symbols) Μικροί κύκλοι (small circles) Συμπαγής: αρχικές συνθήκες Διπλός κύκλος: τελικές συνθήκες Βέλη μετάβασης (transition arrows) Καθορίζουν τη σειρά εκτέλεσης Ορθογώνια με διπλωμένο πάνω δεξί άκρο Επεξηγηματικές σημειώσεις/σχόλια 8
Παράδειγμα: υπολογισμός μέσου όρου βαθμολογίας (με γνωστό τον αριθμό των βαθμών επαναλήψεων) 1 Set total to zero 2 Set grade counter to one 3 4 While grade counter is less than or equal to ten 5 Prompt the user to enter the next grade 6 Input the next grade 7 Add the grade into the total 8 Add one to the grade counter 9 10 Set the class average to the total divided by ten 11 Print the class average 9
GradeBook.java Assign a value to instance variable coursename Declare method setcoursename Declare method getcoursename 10
Declare method displaymessage Declare method determineclassaverage Declare and initialize Scanner variable input Declare local int variables total, gradecounter, grade and average 11
while loop iterates as long as gradecounter <= 10 Increment the counter variable gradecounter Calculate average grade Display results 12
Create a new GradeBook object Pass the course s name to the GradeBook constructor as a string Call GradeBook s determineclassaverage method 13
Παράδειγμα: υπολογισμός μέσου όρου βαθμολογίας (με άγνωστο αριθμό βαθμών sentinel-controlled repetitions) 1 Initialize total to zero 2 Initialize counter to zero 3 4 Prompt the user to enter the first grade 5 Input the first grade (possibly the sentinel) 6 7 While the user has not yet entered the sentinel 8 Add this grade into the running total 9 Add one to the grade counter 10 Prompt the user to enter the next grade 11 Input the next grade (possibly the sentinel) 12 13 If the counter is not equal to zero 14 Set the average to the total divided by the counter 15 Print the average 16 else 17 Print No grades were entered 14
Assign a value to instance variable coursename Declare method setcoursename Declare method getcoursename 15
Declare method displaymessage Declare method determineclassaverage Declare and initialize Scanner variable input Declare local int variables total, gradecounter and grade and double variable average 16
while loop iterates as long as grade!= the sentinel value, -1 Calculate average grade using (double) to perform explicit conversion Display average grade Display No grades were entered message 17
Create a new GradeBook object Pass the course s name to the GradeBook constructor as a string Call GradeBook s determineclassaverage method 18
Πολλαπλοί (σύνθετοι) τελεστές ορισμού (Compound assignment operators) Μεταβλητή = μεταβλητή τελεστής έκφραση; Μεταβλητή τελεστής= έκφραση; (Τελεστές: + - * / %) variable = variable operator expression; variable operator= expression; π.χ.: x = x + 35; x += 35; y = y / 1.2; y /= 1.2; 19
Assignment operator Sample expression Explanation Assigns Assume: int c = 3, d = 5, e = 4, f = 6, g = 12; += c += 7 C = c + 7 10 to c -= d -= 4 d = d - 4 1 to d *= e *= 5 e = e * 5 20 to e /= f /= 3 f = f / 3 2 to f %= g %= 9 g = g % 9 3 to g 20
Αυξητικοί και μειωτικοί τελεστές (Unary Increment and Decrement Operators) Operator Called ++ ++ -- -- prefix increment postfix increment prefix decrement postfix decrement Sample expression Explanation ++a a++ --b b-- Increment a by 1, then use the new value of a in the expression in which a resides. Use the current value of a in the expression in which a resides, then increment a by 1. Decrement b by 1, then use the new value of b in the expression in which b resides. Use the current value of b in the expression in which b resides, then decrement b by 1. 21
Postincrementing the c variable Preincrementing the c variable 22
Παράδειγμα επαναλήψεων (βρόγχων): while Control-variable name is counter Control-variable initial value is 1 Increment for counter Condition tests for counter s final value 23
Παράδειγμα επαναλήψεων (βρόγχων): for 24
Επιμέρους συστατικά ενός for statement 25
UML activity diagram για το βρόγχο for 26
Αντιστοιχία βρόγχων for και while for ( initialization; loopcontinuationcondition; increment ) { statement ; statement ; } initialization; while ( loopcontinuationcondition ) { } statement ; statement ; increment ; 27
Παραδείγματα χρήσης της δομής ελέγχου: for Vary control variable from 1 to 100 in increments of 1 for ( int i = 1; i <= 100; i++ ) Vary control variable from 100 to 1 in increments of 1 for ( int i = 100; i >= 1; i-- ) Vary control variable from 7 to 77 in increments of 7 for ( int i = 7; i <= 77; i += 7 ) Vary control variable from 20 to 2 in decrements of 2 for ( int i = 20; i >= 2; i -= 2 ) Vary control variable over the sequence: 2, 5, 8, 11, 14, 17, 20 for ( int i = 2; i <= 20; i += 3 ) Vary control variable over the sequence: 99, 88, 77, 66, 55, 44, 33, 22, 11, 0 for ( int i = 99; i >= 0; i -= 11 ) 28
Παράδειγμα επαναλήψεων (βρόγχων): for increment number by 2 each iteration 29
1 // Fig. 5.6: Interest.java 2 // Compound-interest calculations with for. 3 4 public class Interest 5 { 6 public static void main( String args[] ) 7 { 8 double amount; // amount on deposit at end of each year 9 double principal = 1000.0; // initial amount before interest 10 double rate = 0.05; // interest rate 11 12 // display headers 13 System.out.printf( "%s%20s\n", "Year", "Amount on deposit" ); 14 Java treats floating-points as type double 30
15 // calculate amount on deposit for each of ten years 16 for ( int year = 1; year <= 10; year++ ) 17 { 18 // calculate new amount for specified year 19 amount = principal * Math.pow( 1.0 + rate, year ); 20 21 // display the year and the amount 22 System.out.printf( "%4d%,20.2f\n", year, amount ); 23 } // end for 24 } // end main 25 } // end class Interest Year Amount on deposit 1 2 1,050.00 1,102.50 3 1,157.63 4 1,215.51 5 1,276.28 6 1,340.10 7 1,407.10 8 1,477.46 9 1,551.33 10 1,628.89 Calculate amount with for statement Use the comma (,) formatting flag to display the amount with a thousands separator static method pow 31
Παράδειγμα επαναλήψεων (βρόγχων): do while Declares and initializes control variable counter Variable counter s value is displayed before testing counter s final value 32
UML activity diagram για το βρόγχο do while 33
Εντολές break και continue break Προκαλεί άμεση έξοδο από τις εντολές της συγκεκριμένης δομής ελέγχου Χρησιμοποιείται σε περιπτώσεις: while, for, do while και switch continue Προκαλεί μη εκτέλεση των επόμενων εντολών του βρόγχου συνεχίζοντας όμως στην επόμενη επανάληψη Χρησιμοποιείται σε περιπτώσεις: while, for, και do while switch 34
Παράδειγμα χρήσης: break Exit for statement (break) when count equals 5 35
Παράδειγμα χρήσης: continue Skip line 12 and proceed to line 7 when count equals 5 36
Παράδειγμα πολλαπλής επιλογής: switch και break 37
38
Loop condition uses method hasnext to determine whether there is more data to input (grade / 10 ) is controlling expression switch statement determines which case label to execute, depending on controlling expression 39
default case for grade less than 60 40
41
Call GradeBook public methods to count grades 42
43
UML activity diagram για την πολλαπλή επιλογή switch 44
Λογικοί Τελεστές (Logical Operators) της Java && (conditional AND) (conditional OR) & (boolean logical AND) (boolean logical inclusive OR) ^ (boolean logical exclusive OR)! (logical NOT) 45
Λογικό ΚΑΙ (conditional AND): && expression1 expression2 expression1 && expression2 false false False false true False true false False true true True Λογικό Η (conditional OR): expression1 expression2 expression1 expression2 false false false false true true true false true true true true Short-Circuit Evaluation of Complex Conditions: Εκτελείται μόνο ότι είναι απαραίτητο για να καθοριστεί αν είναι true ή false 46
Boolean Logical Operators Boolean Logical AND (&) Operator Ίδιο με το && αλλά υπολογίζει ΠΑΝΤΑ και τους δύο τελεστέους (operands) και όχι μόνο όταν απαιτείται Boolean Logical OR ( ) Operator Ίδιο με το αλλά υπολογίζει ΠΑΝΤΑ και τους δύο τελεστέους (operands) και όχι μόνο όταν απαιτείται 47
Λογικό αποκλειστικό Ή - Boolean Logical Exclusive OR: ^ Είναι αληθές (true) μόνο αν o ένας από τους τελεστέους είναι αληθές, ενώ είναι ψευδής αν και οι δύο είναι είτε αληθής ή ψευδής expression1 expression2 expression1 ^ expression2 false false false false true true true false true true true false Λογική άρνηση - Logical Negation Operator:! expression false true!expression true false 48
Conditional AND truth table Conditional OR truth table Boolean logical AND truth table 49
Boolean logical inclusive OR truth table Boolean logical exclusive OR truth table Logical negation truth table 50
Αποτελέσματα προγράμματος: 51
Operators Προτεραιότητα Τελεστών Associativity Type ++ -- right to left unary postfix ++ - (type) + -! right to left unary prefix * / % left to right multiplicative + - left to right additive < <= > >= left to right relational ==!= left to right equality & left to right boolean logical AND ^ left to right boolean logical exclusive OR left to right boolean logical inclusive OR && left to right conditional AND left to right conditional OR?: right to left conditional = += -= *= /= %= right to left assignment 52
Ανακεφαλαίωση Δομημένου Προγραμματισμού χρησιμοποιώντας UML Structured Programming Summary using UML 53