This chapter delves into the fundamental concepts of control structures in programming languages, focusing on expressions, statements, and exception handling. It explores the role of expressions in building complex computations, examines various types of conditional statements and loops, and discusses the importance of exception handling for robust program design. The chapter also provides a case study on computing the values of static expressions in tinyada, illustrating the practical application of these concepts.
Download Control Structures in Programming: Expressions, Statements, and Exceptions and more Study notes Programming Languages in PDF only on Docsity!
Programming Languages
Third Edition
Chapter 9
Control I – Expressions and Statements
Objectives
(^) Understand expressions
(^) Understand conditional statements and guards
(^) Understand loops and variation on WHILE
(^) Be familiar with the GOTO controversy and loop exits
(^) Understand exception handling
(^) Compute the values of static expressions in TinyAda
Introduction (cont’d.)
(^) C could be called an expression-orientedlanguage - (^) Expressions make up a much larger portion of the language than statements
(^) If no side effects, expressions are closest in appearance to mathematics - (^) Have semantics similar to those of mathematical expressions
(^) Semantics of expressions with side effects have a significant control component
Introduction (cont’d.)
(^) Explicit control structures first appeared as GOTOs
(^) Algol60 brought structured control
(^) Control statements transfer control to and from statements that are single-entry , single exit , such as blocks
(^) Some languages do away with GOTOs altogether, but there is still debate on the utility of GOTOs within the context of structured programming
Expressions (cont’d.)
(^) Operators can be written in infix, postfix, or prefix notation - (^) Postfix and prefix forms do not require parentheses to express the order in which operators are applied
(^) Operators are predefined, written in infix form (if binary), with special associativity and precedence rules
(^) Functions are user-defined, with the operands viewed as arguments or actual parameters
(^) This distinction is arbitrary, since operators and functions are equivalent concepts
Expressions (cont’d.)
(^) Distinction is significant, since built-in operators were implemented as highly optimized inline code - (^) Functions required the building of activations
(^) Modern translators often inline even user-defined functions
(^) Lisp requires expressions to be fully parenthesized because it can take variable numbers of arguments as operands
(^) Applicative order evaluation (or strict evaluation ) rule: all operands are evaluated first, then operators are applied to them
Expressions (cont’d.)
(^) Natural order to process (3+4) and (5-6) is left to right, but many languages do not specify an order - (^) Machines may have different requirements for the structure of calls to procedures and functions - (^) Translators may attempt to rearrange for efficiency
(^) If there are no side effects, order of evaluation of subexpressions will make no difference - (^) If there are side effects, there may be differences
Expressions (cont’d.)
Expressions (cont’d.)
(^) Short-circuit evaluation : Boolean expressions are evaluated left to right up to the point where the truth value of the entire expression becomes known, and then evaluation stops - (^) Example: in Ada: - (^) Is always true, regardless of the value of x
(^) Order of evaluation is important in short-circuit evaluation
(^) If expressions and case expressions also may not be completely evaluated
Expressions (cont’d.)
(^) If (or if-then-else) operator: is a ternary operator with three operands
(^) Mix-form: distributes parts of the syntax of the operator throughout the expression - (^) Example: in ML code:
(^) If-expressions never have all of their subexpressions evaluated
(^) Case expression : similar to a series of nested-if expressions
(^) Delayed evaluation (or nonstrict evaluation ): when operators delay evaluating their operands
Expressions (cont’d.)
(^) Example: in C code:
(^) Consider the expression
(^) Square is replaced by
(^) Without evaluating double(2)
(^) Then it is replaced by 2 + 2
(^) Normal order evaluation implements a kind of code inlining
Expressions (cont’d.)
(^) With no side effects, normal order evaluation does not change the semantics of a program
(^) Example with side effects in C code:
(^) Expression would be expanded into - (^) Would read in two integer values instead of one
Conditional Statements and Guards
(^) if-statement : typical form of structured control
(^) Execution of a group of statements occurs only under certain conditions
(^) Guarded if statement:
(^) All Bi’s are Boolean expressions called guards
(^) All Si’s are statement sequences
(^) If one Bi evaluates to true, the corresponding Si is executed
(^) If more than one Bi is true, only one Si is executed
Conditional Statements and Guards
(cont’d.)
(^) It does not say that the first true Bi is chosen
(^) This introduces nondeterminism into programming
(^) It leaves unspecified whether all guards are evaluated - (^) A useful feature for concurrent programming
(^) Usual implementation is to sequentially evaluate all Bi’s until a true one is found, then execute the corresponding Si
(^) If-statements and case-statements are the major ways that the guarded if are implemented