Title: Chapter 3 of Programming Languages by Ravi Sethi
1Chapter 3of Programming LanguagesbyRavi Sethi
2Chapter 3 Structured Programming
- 3.1 The Need for Structured
- programming
- Values of variables can change
- The basic units of imperative
- programming are actions, which
- can change the values of variables (assignments,
procedure calls, read(x)
3Static Programs, Dynamic Computations
- Programs specify computations
- A sequential computation consists of a sequence
of actions, such as - writeln (1, 1 1)
- writeln (2, 2 2)
- writeln (3, 3 3)
-
4- The text is static in a program, but the
computation is dynamic which occurs every time a
program runs. - The gap between the static program and dynamic
execution has motivated the need for structured
programming. - So the reader of a program can understand the
actions that occur when a program runs
5Design Principles for Imperative Languages
- Built with structure and efficiency in mind
- Structured Programming
- The structure of the program text
- should help us understand what
- the program does.
- (easier to modify and tune for efficiency)
6- Efficiency
- A language must allow an underlying
assignment-oriented machine to be used directly
and efficiently. - A Running Example
- Skip duplicates - removal of adjacent
duplicates - Invariants Program Design
- Invariants relate programs and computations -
tells us about the property of its computations
7- Programming language design must deal at some
level with program design. The purpose is to make
programming easier. - -- An Invariant at some point in a program is an
assertion that holds whenever that point is
reached at run time, that is, whenever control
reaches that point - -- An Assertion is a true / false condition about
the state of a computation. An example is the
condition X gt Y, which relates the value of X and
Y
83.2 Syntax-Directed Control Flow
- --Structured statements are by definition
single-entry and single-exit - --Structured control flow A program is
structured if the flow of control through the
program is evident from the syntactic structure
of the program text - Composition of Statements
- Control flows sequentially through a sequence
of statements, as in -
9- temp x x y y temp
- Selection Conditional Statements
- A conditional statement selects one of two
alternative substatements for execution - Looping constructs, divided into two forms
- Definite (for)
- Indefinite (while, repeat until)
- Selection case statements - case constants,
appear in any order
10Implementation of Case Statements
- Except for case statements, the statement
constructs in imperative languages can be used
without thinking about how they were implemented - some implementations recommend only be used when
the case constants are essentially adjacent
113.3 Design Consideration Syntax
- Syntax affects usability
- Sequences separators versus terminators, e.g.,
semicolons - Fewer programming errors are believed to
occur if semicolons terminate statements than if
they separate statements - Avoiding dangling else
123.4 Handling Special Cases in loops
- Break and continue statements in loops
- -- A break statement sends control out of the
enclosing loop to the statement following the
loop. - -- A continue statement repeats the enclosing
loop by sending control to the beginning of the
loop - Return statements
- Go to Statements
133.5 Programming with Invariants
- constructed program is defined as
single-entry/single-exit. - Precondition and postcondition
- -- attached just before and after a statement
both are assertions. -
14- x gt 0 and ygt 0 - precondition
- while x gt y do
- y gt 0 and x gt y invariant
- x x - y
- x gt 0 and y gt 0 postcondition
- Linear Search
- Linear search proceeds by examining all the
elements until either x is found or no elements
remain to be examined. - Invariants can describe data structures -A Table
Organized Around an invariant -
15- An approach to implementing linear search
- while elements remain to be examined do begin
- if this element is x then
- return its position
- else
- not found, so return 0
16- Linear Search with a sentinel
- The post condition after the search
- x Ai and x is not in Ai 1 n and
- 0lt i lt n
- The condition for staying within the while loop
is Ai ! x. -
17- The final developed program fragment
- A0 x
- i n
- while x ! Ai do
- i i - 1
- return i
18Your chance at correct high-level pseudocode
- You are to determine whether the sorted array
X1N contains the element T. Use a binary search
19A Possible Answer
first 1 last N while(first lt last)
middle (first last) /2 if( T
Xmiddle ) return middle else
if (T lt Xmiddle) last middle -1
else first middle 1
print not found
20Proof rules for partial correctness
- Proof rules -- the theory behind invariants
- -- good training for dealing with subtle code.
- Partial correctness
- -- the program is correct if it terminates
- Assertions and formulas
- -- P (pre), Q(post) ,(and), v(or) ,!(this is
my not since PP cannot easily do the hook
21- Some examples of proof rule and axioms
- Rule for statement composition
- S1 S2
- P S1 Q, S2 R (lt- given formula)
- P S1 S2 R ( lt- conclusion)
- Rule for conditionals
- PE S1 Q, P !E S2 Q
- P if E then S1 else S2 Q
- Rule for while statement
- P E S P
- P while E do S P !E
-
-
22- Rule for Assignments
- Q E / x x E Q (assignment axiom -
since it is an atomic statement, its proof rule
has no formulas above the line) - -- The relationship between the postassignment
value and preassignment value of E is of x - Rule for Simplification (predicates ex. Igt1)
- P implies P, P S Q ,Q implies Q
- P S Q
23- Statements in Pascal
- control flow in Pascal is purely
syntax-directed - the flow of control through a construct can be
described purely in terms of components of the
construct.
243.7 Control Flow in C
- Comparison of C to Pascal
- C Pascal
- if ( E ) S if E then S
- if ( E ) s1 else s2 if E then s1 else s2
- while ( E ) s while E do s
- Assignment Operations
- , , !
25- Assignments within Expressions
- c getchar() can appear as a sub expression
within a larger expression - while ( (c getchar()) ! EOF)
- For loops in C Indefinite Iteration
- --An empty test expression is assumed to be true
- for ( ) can be read as forever (the
expressions E1, E2, E3 are optional in a for loop)
26- Break and continue statements in loops
- example
- for( c getchar( ) )
- if ( c c \t )
- continue
- if ( c ! \n )
- break
- lineno