Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.

Similar presentations


Presentation on theme: "ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."— Presentation transcript:

1 ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University (ICU)

2 Spring 2005 2 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Chapter 7 – Expressions and Assignment Statements Chapter 7 – Expressions and Assignment Statements Side Effects Side Effects Overloaded Operators Overloaded Operators Type Conversions Type Conversions Short-circuit Expressions Short-circuit Expressions Mixed-mode Assigments Mixed-mode Assigments Java allows widening type coercions Java allows widening type coercions Last Lecture

3 Spring 2005 3 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University This Lecture Chapter 8 – Statement-Level Control Structures Chapter 8 – Statement-Level Control Structures Selection Statements Selection Statements Iterative Statements Iterative Statements Unconditional Branching Unconditional Branching Guarded Commands Guarded Commands

4 Spring 2005 4 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Chapter 8 Statement-Level Control Structures

5 Spring 2005 5 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Control Structures Control Structure: a control statement and the statements whose execution it controls Control Structure: a control statement and the statements whose execution it controls Selection Statements – if, switch Selection Statements – if, switch Iterative Statements – do, for, while, … Iterative Statements – do, for, while, … Unconditional Branching – goto Unconditional Branching – goto Guarded Commands – nondeterministic if Guarded Commands – nondeterministic if Levels of Control Flow Levels of Control Flow Within expressions Within expressions Among program statements Among program statements Among program units Among program units

6 Spring 2005 6 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Nested Two-Way Selectors ALGOL 60 – disallow direct nesting ALGOL 60 – disallow direct nesting if... then if... then begin begin if...then... if...then... end end else... else... Java – else goes with the nearest if Java – else goes with the nearest if if... if... if... else... else... FORTRAN 90 and Ada – closing special words FORTRAN 90 and Ada – closing special words if... then if... then if... then... if... then... end if end if else... else... end if end if

7 Spring 2005 7 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Testing Nested Ifs public class TestNestedIfs { public static void main(String args[]) { public static void main(String args[]) { int n = 3; if (n < 5) if (n > 3) if (n > 3) System.out.println("n is greater than 3."); else else System.out.println("n is not greater than 3."); System.out.println("n is not greater than 3."); }} d:\ICE1341\Samples>java TestNestedIfs N is not greater than 3.

8 Spring 2005 8 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Multiple Selection Statements (1) Design Issues: 1. What is the form and type of the control expression? 2. How are the selectable segments specified? 3. Is execution flow through the structure restricted to include just a single selectable segment? 4. What is done about unrepresented expression values? switch (month) { case 3: case 3: case 4: case 4: case 5: season = "Spring"; case 5: season = "Spring"; break; break; case 6: case 6: case 7: case 7: case 8: season = "Summer"; case 8: season = "Summer"; break; break; case 9: case 9: case 10: case 10: case 11: season = "Fall"; case 11: season = "Fall"; break; break; default: season = "Winter"; default: season = "Winter"; break; break;} * AW Lecture Notes

9 Spring 2005 9 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Multiple Selection Statements (2) Design Choices (C Switch): 1. Control expression can be only an integer type 2. Selectable segments can be statement sequences or blocks 3. No implicit branch at the end of selectable segments (reliability vs. flexibility) 4. default clause is for unrepresented values (it’s optional) switch (month) { case 3: case 3: case 4: case 4: case 5: season = "Spring"; case 5: season = "Spring"; break; break; case 6: case 6: case 7: case 7: case 8: season = "Summer"; case 8: season = "Summer"; break; break; case 9: case 9: case 10: case 10: case 11: season = "Fall"; case 11: season = "Fall"; break; break; default: season = "Winter"; default: season = "Winter"; break; break;} * AW Lecture Notes

10 Spring 2005 10 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Other Multiple Selectors FORTRAN Arithmetic IF FORTRAN Arithmetic IF IF (arithmetic expression) N1, N2, N3 No encapsulation of selectable segments (they could be anywhere) No encapsulation of selectable segments (they could be anywhere) Ada's case statement Ada's case statement Constant lists can include: Constant lists can include: Subranges e.g., 10..15 Subranges e.g., 10..15 Boolean OR operators – e.g., 1..5 | 7 | 15..20 Boolean OR operators – e.g., 1..5 | 7 | 15..20 Lists of constants must be exhaustive Lists of constants must be exhaustive Often accomplished with others clause Often accomplished with others clause This makes it more reliable This makes it more reliable * AW Lecture Notes

11 Spring 2005 11 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Iterative Statements Iterative Statement: causes a collection of statements to be executed multiple times Iterative Statement: causes a collection of statements to be executed multiple times cf. Recursion: unit-level control cf. Recursion: unit-level control Design issues: Design issues: 1. How is iteration controlled? 2. Where is the control mechanism in the loop? SUM = 0 SUM = 0 DO 20 N = 1, 100, 3 DO 20 N = 1, 100, 3 20 SUM = SUM + N SUM = 0 SUM = 0 N = 1 N = 1 20 SUM = SUM + N N = N + 3; N = N + 3; IF (N.LE. 100) THEN IF (N.LE. 100) THEN GOTO 20 GOTO 20

12 Spring 2005 12 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Counter-Controlled Loops Design Issues: Design Issues: 1. What are the type and scope of the loop variable? 2. What is the value of the loop variable at loop termination? 3. Should it be legal for the loop variable or loop parameters to be changed in the loop body, and if so, does the change affect loop control? 4. Should the loop parameters be evaluated only once, or once for every iteration? DO 20 N = 1, 100, 3 DO 20 N = 1, 100, 3 20 SUM = SUM + N Loop Variable Initial Value Terminal Value Stepsize Loop Parameters

13 Spring 2005 13 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University FORTRAN ‘DO’ Loop Syntax: DO label var = start, finish [, stepsize] Syntax: DO label var = start, finish [, stepsize] Stepsize can be any value but zero Stepsize can be any value but zero Parameters can be expressions Parameters can be expressions Design Choices: Design Choices: 1. Loop variable must be integer 2. Loop variable always has its last value 3. The loop variable cannot be changed in the loop, but the parameters can 4. Loop parameters are evaluated only once * AW Lecture Notes

14 Spring 2005 14 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University ALGOL 60 ‘For’ Loop Syntax: for var := do statement where can have: Syntax: for var := do statement where can have: list of expressions list of expressions expression step expression until expression expression step expression until expression expression while boolean_expression expression while boolean_expression e.g., for index := 1 step 2 until 50, e.g., for index := 1 step 2 until 50, 60, 70, 80, 60, 70, 80, index + 1 until 100 do index + 1 until 100 do (index = 1, 3, 5, 7,..., 49, 60, 70, 80, 81, 82, 83,..., 100) Parameters are evaluated with every iteration, making it very complex and difficult to read Parameters are evaluated with every iteration, making it very complex and difficult to read * AW Lecture Notes

15 Spring 2005 15 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University ‘For’ Loops in C-based Languages Syntax: for ([expr_1] ; [expr_2] ; [expr_3]) statement Syntax: for ([expr_1] ; [expr_2] ; [expr_3]) statement The expressions can be statement sequences, with the statements separated by commas or null The expressions can be statement sequences, with the statements separated by commas or null e.g., for (int i=0, j=10; j==i; i++, j--) e.g., for (int i=0, j=10; j==i; i++, j--) printf(“%d, %d”, i, j); printf(“%d, %d”, i, j); for (;;) … for (;;) … In Java, the control expression must be Boolean In Java, the control expression must be Boolean Design Choices: Design Choices: 1, 2. There is no explicit loop variable 3. Everything can be changed in the loop 4. expr_1 is evaluated once, others are evaluated with each iteration others are evaluated with each iteration Flexible!

16 Spring 2005 16 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Other Counter-Controlled Loops Pascal Pascal for variable := initial (to | downto) final do Ada Ada for var in [reverse] discrete_range loop...... end loop The loop variable is implicitly declared and undeclared as the loop begins and terminates The loop variable is implicitly declared and undeclared as the loop begins and terminates e.g., Count : Float := 1.35; for Count in 1..10 loop for Count in 1..10 loop Sum := Sum + Count; end loop end loop Count is an integer loop variable Count is a float variable

17 Spring 2005 17 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Logically Controlled Loops Posttest version executes the loop body at least once Posttest version executes the loop body at least once e.g., At the above examples, what happens if n is already greater than 100 before reaching to the loop? Pascal – while … do, repeat … until Pascal – while … do, repeat … until Ada and Perl support only pretest versions Ada and Perl support only pretest versions FORTRAN 77 and 90 support neither version FORTRAN 77 and 90 support neither version while (n <= 100) { sum += n; n += 3; } do { sum += n; n += 3; } while (n <= 100) PretestPosttest

18 Spring 2005 18 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University User-Located Loop Control Design Issues: 1. Should the conditional be part of the exit? C-based – unconditional Ada – conditional (exit when …) 2. Can control be transferable out of more than one loop? Java, C#, Perl – Yes while (n <= 100) { sum += n; if (sum == m) continue; n += 3; } while (n <= 100) { sum += n; if (sum == m) break; n += 3; } out: for (int i=0; i<k; i++) { while (n <= 100) { sum += n; sum += n; if (sum == m) if (sum == m) break out; break out; n += 3; n += 3; }} Java

19 Spring 2005 19 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Iteration Based on Data Structures & Unconditional Branching IBDS: Use order and number of elements of some data structures to control iteration IBDS: Use order and number of elements of some data structures to control iteration Unconditional Branching (Goto) Unconditional Branching (Goto) Problem: readability – Spaghetti Logic Problem: readability – Spaghetti Logic Some languages do not have them: e.g., Java, Modular-2 Some languages do not have them: e.g., Java, Modular-2 Loop exit statements are restricted and somewhat camouflaged goto’s Loop exit statements are restricted and somewhat camouflaged goto’s String[] wdays = { “Mon”, “Tue”, “Wed”, “Thu”, “Fri” }; … foreach (String name in wdays) Console.WriteLine(“Work Day: {0}”, name); C#

20 Spring 2005 20 ICE 1341 – Programming Languages © In-Young Ko, Information and Communications University Guarded Commands (Dijstra, 1975) If more than one are true, choose one nondeterministically If more than one are true, choose one nondeterministically Runtime error when non of the conditions is true Runtime error when non of the conditions is true if i = 0 -> sum := sum + i [] i > j -> sum := sum + j [] j > I -> sum := sum + I fi  Allow verification during program development  Can be used to represent concurrency do q1 > q2 -> temp := q1; q1 := q2; q2 := temp; [] q2 > q3 -> temp := q2; q2 := q3; q3 := temp; [] q3 > q4 -> temp := q3; q3 := q4; q4 := temp; od If more than one are true, choose one nondeterministically; then start loop again; If none are true, exit loop If more than one are true, choose one nondeterministically; then start loop again; If none are true, exit loop


Download ppt "ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University."

Similar presentations


Ads by Google