Presentation is loading. Please wait.

Presentation is loading. Please wait.

Imperative Programming Statements and invariants.

Similar presentations


Presentation on theme: "Imperative Programming Statements and invariants."— Presentation transcript:

1 Imperative Programming Statements and invariants

2 Imperative programming The value of variables can change as a program runs, and Programs are not the same as computations, the actions that occur when a program runs.

3 Variables can be changed The basic units of imperative programming are actions, which can change the values of variables. An assignment changes the value of x x := 2+3; This expression 2+3 will be computed equal 5 and associate it with x

4 Variables can be changed(cont’) An array supports random access to a sequence of elements of the same type. Random access means that elements can be selected by their position in the sequencem, called index. x := A[i]; the value of A at i th element assign to x A[i] := x; the value of x assign to A at i th element

5 Design Principles for Imperative Languages Structured programming: make code more understandable. Efficiency: remain a driving influence. A language must allow an underlying assignment-oriented machine to be used directly and efficiently.

6 Static Programs, Dynamic Computations Sequential computation consists of a sequence of actions. A program can be succinct by another The static text of a program is distinct from the dynamic computations that occur when the program runs. writeln(1,1*1); writeln(2,2*2); writeln(3,3*3); for i from 1 to 3 do writeln(i,i*i);

7 A Running Example For example, the removal of adjacent duplicates from a list of elements 1 1 2 2 2 3 1 4 4 1 2 3 1 4 The approach is Do repeatedly these statements read element x; write x; skip past the duplicates of x;

8 Invariants: Program Design An invariant at some point in a program is an assertion that holds whenever that point is reached at run time. Invariants are attached to a program point and tell us about a property of its computations; They are a bridge between the static program and the dynamic computation.

9 A program fragment of removing adjacent duplicates example Invariant will be enclosed with { and } Read(x); While x is not the end marker do Begin {here, x is the first element of a run} writeln(x); repeat read(next) until next != x; x := next; End;

10 SYNTAX-DIRECTED CONTROL FLOW 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. evidentsingle-entry/single-exit “evident” is defined as single-entry/single-exit Constructs called statements specify actions and flow of control around actions. All of the statement constructs of Pascal, except gotos, are single-entry/single-exit.

11 Composition of Statements Normally, Control flow sequentially through a sequence of statements like A sequence can be grouped into a compound statement. Temp := x; x:=y; y:= temp; Begin Temp := x; x:=y; y:= temp; End.

12 Selection:Conditional Statement A condition statement selects one of two alternative sub statements for execution. If then If then else If... Then... Else if... Then... Else...

13 Example Determine leap year; true. If (year mod 400) = 0 then true Else if (year mod 100) = 0 then false Else if (year mod 4) = 0 then true Else false

14 Loop Constructs:While & Repeat The expression and the body are evaluated alternately as long as the expression is true. The statements in statement-list are executed before the condition represented by expression is evaluated. While do repeat until

15 Definite Iteration: For Each Element Do Design of For statements depends on The index variable; which controls the flow through the loop The step; which determines the value added to the index variable each time through loop The limit; which determines when control leaves the loop

16 For statement in Pascal The index variable is i The step +1 add per time The limit is 10 Example For i:=1 to 10 do writeln(“Hello”,i) For := to do ;

17 The treatment of index, variable, step and limit Are the step and the limit computed once, just before loop entry, or are they recomputed each time control flows through the loop? Is the limit tested at the beginning or at the end of each pass through the loop? Can the value of the index variable be changed, say by an assignment, within the loop?

18 Selection: Case Statements A case statement uses the value of an expression to select one of several sub statements for execution. Case of : ; … : ; end

19 Design considerations: SYNTAX Following Question: Can the sequence be empty? If there is a delimiter, does it separate elements or terminate them?

20 Pascal: EBNF rules for statements ::= | := | ( ) | begin end | if then [else ] | while do | repeat until | for := to do | case of end ::= | ; ::= : | : ;

21 Pascal: Semicolons and Empty statement Pascal uses semicolons primarily to separate statements Then, It allow empty statement Begin Statement1; statement2; statement3 end Begin Statement1; statement2; statement3; end Begin ; Statement1;; statement2;;; end

22 A parse tree [1] begin statement1; statement2; statement3 end S S S S SL beginend stmt1 ; ; stmt2 stmt3

23 A parse tree [2] begin statement1; statement2; statement3; end S S S S SL beginend stmt1 ; ; stmt3 empty SSL ; stmt2

24 Modula-2 Closing Keywords: avoids confusion misplaced semicolons Additional Keywords: avoids the dangling-else ambiguity ::= if then end ::= if then {elsif then } [else ] end

25 HANDLING SPECIAL CASES IN LOOPS Break : jump out of a loop. Continue : jump to next iteration. while condition do if special case then take care of special case; break; end if; handle the normal cases; end while while condition do if normal case then take care of normal case; break; end if; handle the special cases; end while

26 No break&continue The preceding fragment can be rewritten without a continue statement as while condition do if normal case then handle normal case; else take care of the special cases; end if; end while


Download ppt "Imperative Programming Statements and invariants."

Similar presentations


Ads by Google