1 / 18

Review of the Previous Lectures

Programming Languages (ICE 1341) Lecture #14 April 16, 2004 In-Young Ko iko .AT. i cu . ac.kr Information and Communications University (ICU). Review of the Previous Lectures. Language Evaluation Criteria Describing Syntax and Semantics Names and Bindings Data Types Expressions.

aviv
Download Presentation

Review of the Previous Lectures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Languages(ICE 1341)Lecture #14April 16, 2004In-Young Koiko .AT. icu.ac.krInformation and Communications University (ICU) ICE 1341 – Programming Languages (Lecture #14) In-Young Ko

  2. Review of the Previous Lectures • Language Evaluation Criteria • Describing Syntax and Semantics • Names and Bindings • Data Types • Expressions ICE 1341 – Programming Languages (Lecture #14) In-Young Ko

  3. Control Structures • Control Structure: a control statement and the statements whose execution it controls • Selection Statements – if, switch • Iterative Statements– do, for, while, … • Unconditional Branching– goto • Guarded Commands– nondeterministic if • Levels of Control Flow • Within expressions • Among program statements • Among program units ICE 1341 – Programming Languages (Lecture #14) In-Young Ko

  4. Selection Statements if (sum == 0) if (count == 0) result = 0; else result = 1; IF (SUM .NE. 0) GOTO 10 IF (COUNT .NE. 0) GOTO 20 RESULT = 0 GOTO 20 10 RESULT = 1 20 CONTINUE • Selection Statement: a means of choosing between two or more execution pathsin a program • Two general categories: Two-way selectors, Multiple-way selectors • Design Issues of Two-Way Selectors • What is the form and type of the control expression? • How are the then and else clauses specified? • How should the meaning of nested selectors be specified? ICE 1341 – Programming Languages (Lecture #14) In-Young Ko

  5. Nested Two-Way Selectors • Java – else goes with the nearestif if ... if ... else ... • ALGOL 60– disallowdirect nesting if ... then begin if ...then... end else ... • FORTRAN 90and Ada – closing special words if ... then if ... then... end if else... end if ICE 1341 – Programming Languages (Lecture #14) In-Young Ko

  6. Multiple Selection Statements (1) switch (month) { case 3: case 4: case 5: season = "Spring"; break; case 6: case 7: case 8: season = "Summer"; break; case 9: case 10: case 11: season = "Fall"; break; default: season = "Winter"; break; } 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? * AW Lecture Notes ICE 1341 – Programming Languages (Lecture #14) In-Young Ko

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

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

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

  10. Loop Variable Initial Value Loop Parameters Terminal Value Stepsize Counter-Controlled Loops • Design Issues: 1. What are the type and scope of the loop variable? 2. What is the value of the loop variable at looptermination? 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 20 SUM = SUM + N ICE 1341 – Programming Languages (Lecture #14) In-Young Ko

  11. FORTRAN ‘DO’ Loop • Syntax: DO label var = start, finish [, stepsize] • Stepsize can be any value but zero • Parameters can be expressions • Design Choices: 1. Loop variable must beinteger 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 ICE 1341 – Programming Languages (Lecture #14) In-Young Ko

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

  13. Flexible! ‘For’ Loops in C-based Languages • Syntax:for ([expr_1] ; [expr_2] ; [expr_3]) statement • 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--) printf(“%d, %d”, i, j); for (;;) … • In Java, the control expression must be Boolean • 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 ICE 1341 – Programming Languages (Lecture #14) In-Young Ko

  14. Other Counter-Controlled Loops • Pascal for variable := initial (to | downto) final do • Ada for var in [reverse] discrete_rangeloop ... end loop • 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 Sum := Sum + Count; end loop ICE 1341 – Programming Languages (Lecture #14) In-Young Ko

  15. Pretest Posttest Logically Controlled Loops while (n <= 100) { sum += n; n += 3; } do { sum += n; n += 3; } while (n <= 100) • 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 • Ada and Perl support only pretest versions • FORTRAN 77 and 90 support neither version ICE 1341 – Programming Languages (Lecture #14) In-Young Ko

  16. User-Located Loop Control while (n <= 100) { sum += n; if (sum == m) continue; n += 3; } while (n <= 100) { sum += n; if (sum == m) break; n += 3; } 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 out: for (int i=0; i<k; i++) { while (n <= 100) { sum += n; if (sum == m) break out; n += 3; } } Java ICE 1341 – Programming Languages (Lecture #14) In-Young Ko

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

  18. Guarded Commands (Dijstra, 1975) if i = 0 -> sum := sum + i [] i > j -> sum := sum + j [] j > I -> sum := sum + I fi • If more than one are true, choose onenondeterministically • Runtime error when non of the conditions is true  Allow verification during program development 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 ICE 1341 – Programming Languages (Lecture #14) In-Young Ko

More Related