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.

Slides:



Advertisements
Similar presentations
Statement-Level Control Structures
Advertisements

CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
ICE1341 Programming Languages Spring 2005 Lecture #12 Lecture #12 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Chapter 8 Statement-Level Control Structures. 1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative.
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
ISBN Chapter 8 Statement-Level Control Structures.
ICE1341 Programming Languages Spring 2005 Lecture #14 Lecture #14 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Chapter 8 Statement-Level Control Structure. Introduction Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
Controlling Program Flows
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Statement-Level Control Structures Sections 1-4
ISBN Chapter 8 Statement-Level Control Structures.
CSE 452: Programming Languages Expressions and Control Flow.
1 Chapter 8 Statement-Level Control Structures In Chapter 7, the flow of control within expressions, which is governed by operator associativity and precedence.
ISBN Lecture 08 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
COMP4730/2002/lec8/H.Melikian Statement-Level Control Structures Introduction Compound Statements Selection Statements Iterative Statements Unconditional.
Statement-Level Control Structures
1 Statement-Level Control Structures Levels of flow control Control Statements 1. Sequence 2. Selection 3. Iteration Unconditional branching Guarded commands.
Structure of Programming Language
ISBN Chapter 8 Statement-Level Control Structures Sections 1-4.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
Control Structures Programs have 4 basic control structures:
ISBN Chapter 8 Statement-Level Control Structures.
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
ISBN Chapter 8 Statement-Level Control Structures.
1 CS Programming Languages Class 11 September 26, 2000.
sequence of execution of high-level statements
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
第八章 敘述層級的控制結構 (Statement-Level Control Structures)
Chapter 8: Statement-Level Control Structures
Control Structures sequence of execution of high-level statements.
8-1 Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions.
C HAPTER 8 Statement-Level Control Structures. CCSB314 Programming Language C HAPTER 8 T OPICS Introduction Selection Statements Iterative Statements.
April 2, ICE 1341 – Programming Languages (Lecture #12) In-Young Ko Programming Languages (ICE 1341) Lecture #12 Programming Languages (ICE 1341)
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 7 Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
Statement-Level Control Structures
Chapter 8 © 2002 by Addison Wesley Longman, Inc Introduction - Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program.
W E E K F I V E Statement-Level Control Structures.
Structure of Programming Language Statements. Expression.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Chapter 8 Statement-Level Control Structures. 2 Chapter 8 Topics  Introduction  Selection Statements  Iterative Statements  Unconditional Branching.
Def: A control structure is a control statement and
Structure of Programming Language
8.1 Introduction - Levels of Control Flow: 1. Within expressions
Dr. Vamsi Paruchuri University of Central Arkansas
Statement-Level Control Structures
Chapter 8: Control Structures
Statement-Level Control Structures
Statement-Level Control Structures
Control statements Simple statements Basic structured statements
Control Structures In Text: Chapter 8.
Control Structures Programs utilize 4 basic control structures
Statement-Level Control Structures
Chapter8: Statement-Level Control Structures April 9, 2019
Statement-Level Control Structures
Statement-Level Control Structures
Chapter 8: Statement Level Control Structures
Presentation transcript:

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)

Spring 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

Spring 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

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

Spring 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

Spring 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

Spring 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.

Spring 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

Spring 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

Spring 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., Subranges e.g., Boolean OR operators – e.g., 1..5 | 7 | Boolean OR operators – e.g., 1..5 | 7 | 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

Spring 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

Spring 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

Spring 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

Spring 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

Spring 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!

Spring 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 loop for Count in loop Sum := Sum + Count; end loop end loop Count is an integer loop variable Count is a float variable

Spring 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

Spring 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

Spring 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#

Spring 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