Control Structures - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Control Structures

Description:

... and put in R; JMZ R,L2 Code for Statement2 JMP L1 L2: Next statement Gotos If you don t have hierarchical statement structure, you need goto ... – PowerPoint PPT presentation

Number of Views:58
Avg rating:3.0/5.0
Slides: 26
Provided by: dav94
Category:

less

Transcript and Presenter's Notes

Title: Control Structures


1
Control Structures
2
Hierarchical Statement Structure
  • Standard in imperative languages since Algol60.
  • Exceptions Early FORTRAN, COBOL, early BASIC,
    APL. (Fortran kludged definite loops.)

3
Control structures within function
  • Sequence S1, S2 Sk
  • Conditional if statement, case statement.
  • Loop while loop, for loop.
  • Jump goto, exception raising

4
Expression evaluation
  • Ambiguity of grouping is 123 interpreted as
    (12)39 or 1(23)7?
  • Solutions
  • Operator precedence. binds more tightly than
    , so 123 7.
  • Order precedence. Always compute left to right
    so 123(12)3 9
  • Best to use a lot of parentheses.

5
Short circuit evaluation
  • If Y ltgt 0 and X/Y gt 5, compute Z.
  • if (Y ltgt 0 and X/Y gt 5) Z
  • --- but operators evaluate their arguments
  • Solutions
  • Lazy evaluation of Booleans
  • if (Y ltgt 0 X/Y gt 5) Z
  • Explicit conditionals
  • if (Y ltgt 0) then if (X/Y gt 5) Z

6
Prefix and postfix notation
  • Prefix Operator precedes its arguments
  • Postfix Operator follows its arguments
  • e.g. (13N)/(1N)(12N)
  • Prefix / 1 3 N 1 N 1 2 N
  • Postfix 1 3 N 1 N 1 2 N /
  • If each operator has a fixed number of arguments,
    then prefix and postfix are unambiguous with no
    need for brackets, precedence, or associativity
    conventions.

7
Control structure between functions
  • Function call and return
  • Coroutine yield
  • Parallel synchronization
  • Exception raising

8
Assembly language
  • In assembly language, (essentially) the only
    control structures are
  • Progression Move to the next statement
    (increment the program counter).
  • Unconditional jump
  • JMP A Jump to address A
  • Conditional jump
  • JMZ R,A If (R0) then jump to A
  • Possible forms of conditions and addresses vary.

9
Sequence
  • Pascal begin end
  • C, C, Java
  • Ada Brackets for sequence are unnecessary.
    Keywords for control structures suffice.
  • for J in 1 .. N loop end loop
  • ABC, Python Indicate structure by indentation.

10
Semicolons
  • Pascal Semicolons are separators
  • C etc. Semicolons are terminators
  • begin X 1 X 1
  • Y 2 Y 2
  • end

11
Conditionals
  • if Condition then Statement -- Pascal, Ada
  • if (Condition) Statement -- C, C, Java
  • To avoid ambiguities, use end marker end if,
  • To deal with alternatives, use keyword or
    bracketing
  • if Conditions if (Conditions)
  • then Statements Statements
  • elseif Conditions else if
    (Conditions)
  • then Statements then
    Statements
  • else Statements else
    Statements
  • end if

12
if-else ambiguity
  • if Conditions if (Conditions)
  • then then
  • if Conditions if (Conditions)
  • then Statements Statements
  • else Statements else Statements
  • end if
  • end if

13
Compiling conditionals
  • if (Cond) then Statement1 else Statement2
  • Assembly
  • . Compute Cond in register R
  • JMZ R,L
  • Code for Statement1
  • JMP END
  • L Code for Statement2
  • END Next statement

14
Multi-way selection
  • The case statement is the most useful control
    structure because most programs are
    interpreters. (Ed Schonberg)
  • Can achieve same with conditionals, but case
    statements are clearer.
  • case (NextChar)
  • I N1
  • V N5
  • X N10
  • L N50

15
The well-structured case statement
  • Discrete type.
  • No flow-through (hideous C misdesign).
  • Every value is in exactly one option.
  • Default option for values not covered.
  • Labels are computable at compile time.

16
Implementation
  • Finite set of possibilities can build a table of
    addresses, and
  • convert expression into table index
  • compute value
  • transform into index
  • retrieve address of corresponding code fragment
  • branch to code fragment and execute
  • branch to end of case statement

17
Loops
  • while loop test at beginning
  • while (Condition) Statement
  • repeat loop test at end
  • repeat Statement until Condition
  • do Statement while (Condition)
  • breaking out test in middle
  • loop Statement
  • if (condition) then exitloop
  • Statement
  • end loop

18
Multiple exits
  • If you want to exit from imbedded loops, you need
    to specify which one youre exiting.
  • Ada solution Label loops
  • Outer while C1 loop
  • Inner while C2 loop
  • Innermost while C3 loop
  • exit Outer when MajorFailure
  • exit Inner when SmallAnnoyance
  • end loop Innermost
  • end loop Inner
  • end loop Outer

19
Definite loops
  • for J in 1 .. 10 loop end loop
  • for (int I0 I lt N I)
  • Design issues
  • Evaluation of bounds (only at start, since
    Algol60)
  • Scope of loop variables
  • Empty bodies
  • Increments other than 1
  • Backward iteration
  • Non-numeric domains

20
Definite loops
  • Since the C for loop
  • for (start test increment) body
  • allows arbitrary test conditions and arbitrary
    increment actions, this is not really a definite
    loop at all, just an distinctively formatted
    while loop.
  • However, the compiler is sensitive to the
    particular construction
  • for (int I0 I lt N I)
  • and optimizes the code for this.

21
C break and continue
  • break exit innermost loop (exitloop)
  • continue go to next iteration of innermost loop.

22
Loop counter
  • Local to loop?
  • If not, what is the value on exit?
  • Settable in body of loop?
  • Multiple loop counters?

23
Non-numeric domains
  • Ada for M in months loop end loop
  • Other data types
  • iterator Collection.iterator()
  • element thing iterator.first
  • while (iterator.moreElements())
  • Body
  • thing interator.next()

24
Implementation of loops
  • loop Statement1
  • if (condition) exit loop
  • Statement2
  • end loop
  • L1 Code for Statement1
  • Compute not(condition) and put in R
  • JMZ R,L2
  • Code for Statement2
  • JMP L1
  • L2 Next statement

25
Gotos
  • If you dont have hierarchical statement
    structure, you need gotos.
  • Go To Statements Considered Harmful ---
    Dijkstra, 1968
  • Many more recent languages (e.g. Java) dont have
    them.
  • Certainly must prohibit jumping into the middle
    of a embedded structure e.g.
  • go to L
  • for (int I0 IltN I)
  • L
Write a Comment
User Comments (0)
About PowerShow.com