UT Dallas CS 4337 - #Sebesta ch08 control (38 pages)

Previewing pages 1, 2, 3, 18, 19, 36, 37, 38 of 38 page document View the full content.
View Full Document

#Sebesta ch08 control



Previewing pages 1, 2, 3, 18, 19, 36, 37, 38 of actual document.

View the full content.
View Full Document
View Full Document

#Sebesta ch08 control

24 views


Pages:
38
School:
University of Texas at Dallas
Course:
Cs 4337 - Organization of Programming Languages
Unformatted text preview:

Chapter 8 Statement Level Control Structures Chapter 8 Topics Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions Copyright 2012 Addison Wesley All rights reserved 1 2 Levels of Control Flow Within expressions Chapter 7 Among program units Chapter 9 Among program statements this chapter Copyright 2012 Addison Wesley All rights reserved 1 3 Control Statements Evolution FORTRAN I control statements were based directly on IBM 704 hardware Much research and argument in the 1960s about the issue One important result It was proven that all algorithms represented by flowcharts can be coded with only two way selection and pretest logical loops Copyright 2012 Addison Wesley All rights reserved 1 4 Control Structure A control structure is a control statement and the statements whose execution it controls Design question Should a control structure have multiple entries Copyright 2012 Addison Wesley All rights reserved 1 5 Selection Statements A selection statement provides the means of choosing between two or more paths of execution Two general categories Two way selectors for example if then else Multiple way selectors for example switch Copyright 2012 Addison Wesley All rights reserved 1 6 Two Way Selection Statements General form if control expression then clause else clause Design Issues 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 In many contemporary languages the then and else clauses can be single statements or compound statements Copyright 2012 Addison Wesley All rights reserved 1 7 Nesting Selectors Java example if sum 0 if count 0 result 0 else result 1 Which if gets the else Java s static semantics rule with the nearest previous if Copyright 2012 Addison Wesley All rights reserved else matches 1 8 Nesting Selectors continued To force an alternative semantics compound statements may be used if sum 0 if count result else result 0 0 1 The above solution is used in C C and C Copyright 2012 Addison Wesley All rights reserved 1 9 Nesting Selectors continued Statement sequences as clauses Ruby if sum 0 then if count 0 then result 0 else result 1 end end Copyright 2012 Addison Wesley All rights reserved 1 10 Selector Expressions In ML F and LISP the selector is an expression F let y if x 0 then x else 2 x If the if expression returns a value there must be an else clause the expression could produce output rather than a value Copyright 2012 Addison Wesley All rights reserved 1 11 Multiple Way Selection Statements Allow the selection of one of any number of statements or statement groups e g switch statement in C or C 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 How are case values specified 5 What is done about unrepresented expression values Copyright 2012 Addison Wesley All rights reserved 1 12 Multiple Way Selection Examples C C Java and JavaScript switch expression case const expr1 stmt1 case const exprn stmtn default stmtn 1 Copyright 2012 Addison Wesley All rights reserved 1 13 Multiple Way Selection Examples Design choices for C s switch statement 1 Control expression can be only an integer type 2 Selectable segments can be statement sequences blocks or compound statements 3 Any number of segments can be executed in one execution of the construct there is no implicit branch at the end of selectable segments 4 default clause is for unrepresented values if there is no default the whole statement does nothing Copyright 2012 Addison Wesley All rights reserved 1 14 Multiple Way Selection Examples C Differs from C in that it has a static semantics rule that disallows the implicit execution of more than one segment Each selectable segment must end with an unconditional branch goto or break Also in C the control expression and the case constants can be strings Copyright 2012 Addison Wesley All rights reserved 1 15 Implementing Multiple Selectors Approaches Multiple conditional branches Store case values in a table and use a linear search of the table When there are more than ten cases a hash table of case values can be used If the number of cases is small and more than half of the whole range of case values are represented an array whose indices are the case values and whose values are the case labels can be used Copyright 2012 Addison Wesley All rights reserved 1 16 Multiple Way Selection Using if Multiple Selectors can appear as direct extensions to two way selectors using else if clauses for example in Python if count 10 bag1 True elif count 100 bag2 True elif count 1000 bag3 True Copyright 2012 Addison Wesley All rights reserved 1 17 Multiple Way Selection Using if The Python example can be written as a Ruby case case when count 10 then bag1 true when count 100 then bag2 true when count 1000 then bag3 true end Copyright 2012 Addison Wesley All rights reserved 1 18 Lisp or Scheme s Multiple Selector General form of a call to COND COND predicate1 expression1 predicaten expressionn ELSE expressionn 1 The ELSE clause is optional ELSE is a synonym for true Each predicate expression pair is a parameter Semantics The value of the evaluation of COND is the value of the expression associated with the first predicate expression that is true Copyright 2012 Addison Wesley All rights reserved 1 19 Iterative Statements The repeated execution of a statement or compound statement is accomplished either by iteration or recursion General design issues for iteration control statements 1 How is iteration controlled 2 Where is the control mechanism in the loop Copyright 2012 Addison Wesley All rights reserved 1 20 Counter Controlled Loops A counting iterative statement has a loop variable and a means of specifying the initial and terminal and stepsize values Design Issues 1 What are the type and scope of the loop variable 2 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 3 Should the loop parameters be evaluated only once or once for every iteration Copyright 2012 Addison Wesley All rights reserved 1 21 Counter Controlled Loops Examples Ada for var in reverse discrete range loop end loop Design choices Type of the loop variable is that of the discrete range A discrete range is a sub range of


View Full Document

Access the best Study Guides, Lecture Notes and Practice Exams

Loading Unlocking...
Login

Join to view #Sebesta ch08 control and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view #Sebesta ch08 control and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?