Imperative Programming - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Imperative Programming

Description:

make code more understandable. Efficiency: remain a driving influence. ... A program fragment of removing adjacent duplicates example ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 27
Provided by: kgo55
Category:

less

Transcript and Presenter's Notes

Title: Imperative Programming


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 23
  • This expression 23 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 Ai the value of A at ith element assign
    to x
  • Ai x the value of x assign to A at ith
    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,11) writeln(2,22) writeln(3,33)
for i from 1 to 3 do writeln(i,ii)
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.
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 xy y temp
Begin Temp x xy y temp End.
12
SelectionConditional Statement
  • A condition statement selects one of two
    alternative sub statements for execution.

If ltexpressiongt then ltstmt1gt If ltexpressiongt then
ltstmt1gt else ltstmt2gt 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 ConstructsWhile 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 ltexpressiongt do ltstatementgt
repeat ltstatement-listgt until ltexpressiongt
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

For ltnamegt ltexpressiongt to ltexpressiongt do
ltstatementgt
Example For i1 to 10 do writeln(Hello,i)
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 ltexpressiongt of ltconstant1gt
ltstatement1gt ltconstant2gt ltstatement2gt ltconst
antNgt ltstatementNgt 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
PascalEBNF rules for statements
ltstatementgt ltemptygt ltexpressiongt
ltexpressiongt ltnamegt(ltexpression-listgt) begin
ltstatement-listgt end if ltexpressiongt then
ltstatementgt else ltstatementgt while
ltexpressiongt do ltstatementgt repeat
ltstatement-listgt until ltexpressiongt for ltnamegt
ltexpressiongt to ltexpressiongt do ltstatementgt
case ltexpressiongt of ltcasesgt end ltstatement-listgt
ltstatementgt ltstatementgt
ltstatement-listgt ltcasesgt ltconstantgt
ltstatementgt ltconstantgt ltstatementgt ltcasesgt
21
PascalSemicolons 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
23
A parse tree 2
S
begin statement1 statement2
statement3 end
SL
begin
end
S
SL

S
SL

empty
stmt3
S
SL

S
stmt2
stmt1
24
Modula-2
  • Closing Keywords avoids confusion misplaced
    semicolons
  • Additional Keywords avoids the dangling-else
    ambiguity

ltStatementgt if ltexpressiongt then
ltstatement-listgt end
ltStatementgt if ltexpressiongt then
ltstatement-listgt elsif
ltexpressiongt then ltstatement-listgt
else ltstatement-listgt end
25
HANDLINGSPECIAL 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 breakcontinue
  • 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
Write a Comment
User Comments (0)
About PowerShow.com