Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Statement-Level Control Structures
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
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.
The Flow of Control Among Statements.  Selection Statements  Iterative Statements  Unconditional Branching  Guarded Commands.
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.
CSE 452: Programming Languages Lecture 8 9/18/2003.
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.
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
9/20/2015Assoc. Prof. Stoyan Bonev1 COS220 Concepts of PLs AUBG, COS dept Lecture 06 Components of Programming Languages, Part III Reference: R.Sebesta,
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.
ISBN Chapter 8 Statement-Level Control Structures.
1 CS Programming Languages Class 11 September 26, 2000.
sequence of execution of high-level statements
第八章 敘述層級的控制結構 (Statement-Level Control Structures)
CSI 3120, Control, page 1 Control statements Simple statements Basic structured statements Sequence Selection Iteration The jump statement.
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 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.
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.
Perl Chapter 3 Conditional statements. Control Expressions Control expressions – interpreted as T/F (evaluated as strings or numbers) – simple, relational,
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
CSE 452: Programming Languages
Statement-Level Control Structures
Chapter 8: Statement Level Control Structures
Presentation transcript:

Chapter 8 Chapter 8 Control Structures

Control Structures  A control structure is a control statement and the statements whose execution it controls  Types of control statements:  Selection statements  Iterative statements  Unconditional branching statement  Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements

Design Issues  What control statements should a language have?  Can a control structure have multiple entries?  Single entry:  execution of the code segment begins with the first statement in the segment  Multiple entries are possible in languages that include gotos and statement labels  Multiple entries may add flexibility to a control construct  Can a control structure have multiple exits?

Selection Statements  Provides the means of choosing between two or more execution paths in a program  Types of Selection Statements:  One-way selection statements  Two-way selection statements  N-way (multiple) selection statements  Nested selection statements?

Selection Statements  Single-Way Examples FORTRAN IF: IF (boolean_expr) statement  Problem:  can select only a single statement; to select more, a GOTO must be used, as in the following example IF (.NOT. condition) GOTO CONTINUE

Selection Statements  Two-way selection statements if control_expression then clause else clause  Control_expression  arithmetic/Boolean expressions  Clause form  Can be single statements or compound statements (statements in a program block)

Selection Statements  Nested Selectors if (sum == 0) if (count == 0) result = 0; else result = 1;  Which if gets the else?  Java's static semantics rule: else goes with the nearest if  To force alternative semantics, use compound statement if (sum == 0) { if (count == 0) result = 0; } else result = 1;

Selection Statements  FORTRAN 90 and Ada solution  use special words to resolve semantics of nested selectors  e.g. (Ada) if... then if... then else end if... else end if... end if end if  Advantage: flexibility and readability

Selection Statements  Multiple Selection Constructs C: switch (expression) { case const_expr_1: statement_1; … case const_expr_k: statement_k; [default:def_statement;] (optional) }

Selection Statements  Early Multiple Selectors:  FORTRAN arithmetic IF (a three-way selector) IF (arithmetic expression) N1, N2, N3  Multiple Selection using if: if Expr1 then statement_1 elsif Expr2 then statement_2 … else statement_k end if;

Iterative Statements  Repeated execution of a statement or compound statement  accomplished either by iteration or recursion;  here we look at iteration, because recursion is a unit-level control (e.g., using functions)  General design issues for iteration control statements: 1. How is the iteration controlled?  Counter-controlled vs logical-controlled 2. Where should the control mechanism appear in the loop?  pretest (before loop body is executed) vs posttest (after loop body is executed)

Iterative Statements  Counter-Controlled Loops for (i = init_value; i < terminal_value; i+=stepsize) { … }  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? Loop parameters Loop variable

Counter-Controlled Loops  FORTRAN 95  Syntax: Do label var = initial, terminal [, stepsize]  stepsize can be any value but zero  parameters can be expressions  Design choices: 1. Loop var must be integer; loop parameters can be expressions 2. The loop var cannot be changed in the loop, but loop parameters can because they are evaluated only once, it does not affect loop control 3. Single entry structure – loop can be entered through Do statement

Counter-Controlled Loops  Fortran 95: Do label var = init_expression, terminal_expression [, step_expression] Operational Semantics for Fortran 95 Do statement init_value = init_expression terminal_value = terminal_expression step_value = step_expression do_var = init_value iteration_count = max(int((terminal_value–init_value+step_value)/step_value), 0) loop: if iteration_count  0 goto out [loop body] do_var = do_var + step_value iteration_count = interation_count – 1 goto loop out:…

Counter-Controlled Loops  Another form of Do statement for FORTRAN 95 [name:] DO variable = initial, terminal [, stepsize] … END DO [name]  Uses a special word for closing: END DO

Counter-Controlled Loops  Ada  Syntax: for var in [reverse] discrete_range loop... end loop;  reverse indicates that values of discrete range are assigned in reverse order  Step size is always one (or next element in discrete_range)

Counter-Controlled Loops  Ada Design choices: 1. Type of the loop var is that of the discrete range  discrete range is subrange of integer or enumeration type, such as or Monday..Friday 2. Scope of loop var is the loop body (it is implicitly declared); loop var does not exist outside the loop Count : Float := 1.35; for Count in loop Sum := Sum + Count; end loop; …Count gets the value of The loop var cannot be changed in the loop, but the discrete range can; it does not affect loop control 4. The discrete range is evaluated just once

Counter-Controlled Loops  C-based languages  Syntax: for ([expr_1] ; [expr_2] ; [expr_3]) loop body  Loop body can be single statement, compound statement, or null statement  Expressions can be whole statements, or even statement sequences, with the statements separated by commas  The value of a multiple-statement expression is the value of the last statement in the expression for (i = 0, j = 10; j == i; i++) …  All expressions of C’s for statement are optional  If expr_2 is absent, it is an infinite loop

Counter-Controlled Loops for ([expr_1] ; [expr_2] ; [expr_3]) loop body  Operational Semantics: expr_1 loop: if expr_2 = 0 goto out [loop_body] expr_3 goto loop out:…

Counter-Controlled Loops  Design choices for C: 1. There is no explicit loop variable or loop parameters  All involved variables can be changed in the loop body 2. It is legal to branch into loop body 3. The first expression is evaluated once, but the other two are evaluated with each iteration

Counter-Controlled Loops #include main() { int count, i = 2; count = 2; /* count = 5 */ if (count % 2 == 0) { goto loop; } for (i=0; i<5; i++) { loop: printf("i=%d\n", i); } count = 2: i = 2; i = 3; i = 4; count = 5: i = 0; i = 1; i = 2; i = 3; i = 4;

Counter-Controlled Loops  C++  Differs from C in two ways: 1.The control expression can also be Boolean 2.The initial expression can include variable definitions (scope is from the definition to the end of the loop body) for (int i=0; i < len; i++) { … }  Java  Differs from C++ in that the control expression must be Boolean

Logically-Controlled Loops  Repetition control is based on a Boolean expression, rather than a counter  More general than counter-controlled loops  Every counting loop can be built with a logical loop, but not vice-versa  Design Issues: 1. Pretest or postest? 2. Should logically controlled loop be a special form of counting loop statement or a separate statement?

Logically-Controlled Loops  Pascal has separate pretest and posttest logical loop statements (while-do and repeat-until)  C and C++ also have both while (count > 0) { … } do { … } while (count > 0);  Legal to branch into both while and do loop bodies

Logically-Controlled Loops  Ada has a pretest version, but no posttest  FORTRAN 77, 90, and 95 have neither  Perl has two pretest logical loops, while and until, but no posttest logical loop while (count > 0) { … } until (count == 0) { … }

User-Located Loop Control  User modify the control flow of program  Design issues: 1.Should the conditional mechanism be an integral part of the exit? 2.Should only one loop body be exited or can enclosing loops also be exited?

User-Located Loop Control  Exit statement:  Unconditional unlabeled exit: break (C, C++) for (index=0; index<10; index++) { … if (value < 0) break; }  Unconditional labeled exit: break (Java, C#), last (Perl) C#: outerloop: for (row=0; row<numRows; row++) for (col = 0; col < numCols; col++) { sum += matrix[row][col]; if (sum > 1000) break outerLoop; } Perl: LINE: while ( ) { last LINE if /^$/;... }

User-Located Loop Control  Skip the rest of loop body:  C/C++ have unlabeled control statement (continue) while (sum < 1000) { value = getNextValue(); if (value < 0) continue; sum += value; }  Java, Perl, and C# have statements similar to continue, except they can include labels that specify which loop is continued

Iteration Based on Data Structures  Loops are controlled by number of elements in a data structure  Perl, Javascript, PHP, and C# have such statements = (1, 2, 3, 4, 5); foreach $value { print “Value is $value\n”; } C#: String[] strList = {“Bob”, “John”, “Carol” }; foreach (String name in strList) …  More general approach  uses a user-defined data structure and a user-defined function (called an iterator) to go through the structure’s elements  Java has a Collection interface that contains two methods: boolean add(Object obj) - adds elements to collection Iterator iterator() - used to visit the elements in the collection one by one.

Unconditional Branching  Transfers execution control to a specified location in the program goto label  Problem: readability  Some languages do not have them: e.g., Java  Loop exit statements are restricted and somewhat camouflaged goto’s  Label forms: 1. Unsigned int constants: Pascal (with colon) FORTRAN (no colon) 2. Identifiers with colons: ALGOL 60, C 3. Variables as labels: PL/I