Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 of Programming Languages by Ravi Sethi

Similar presentations


Presentation on theme: "Chapter 3 of Programming Languages by Ravi Sethi"— Presentation transcript:

1 Chapter 3 of Programming Languages by Ravi Sethi

2 Chapter 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)

3 Static 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

5 Design 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 > Y, which relates the value of X and Y

8 3.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

10 Implementation 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

11 3.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

12 3.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

13 3.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 >= 0 and y> 0} - precondition
while x >= y do { y > 0 and x >= y} invariant x := x - y { x >= 0 and y > 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 = A[i] and x is not in A[i + 1 … n] and 0<= i <= n The condition for staying within the while loop is A[i] != x.

17 The final developed program fragment:
A[0] := x; i := n; while x != A[i] do i := i - 1; return i ;

18 Your chance at correct high-level pseudocode
You are to determine whether the sorted array X[1…N] contains the element T. Use a binary search

19 A Possible Answer first = 1; last = N; while(first <= last)
{ middle = (first + last) /2 if( T == X[middle] ) return middle; else if (T < X[middle]) last = middle -1; else first = middle + 1; } print “not found”;

20 Proof 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} (<- given formula) {P} S1; S2 {R} ( <- conclusion) Rule for conditionals {P^E} 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 P implies P’, {P’} S {Q’} ,Q’ implies Q {P} S {Q}
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. I>1) 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.

24 3.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; }


Download ppt "Chapter 3 of Programming Languages by Ravi Sethi"

Similar presentations


Ads by Google