Principle of Programming Lanugages 2: Imperative languages

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Chapter 6 Path Testing Software Testing
Statement-Level Control Structures
Adapted from Scott, Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Principles of programming languages 3: Answers for exercises Isao Sasano Department of Information Science and Engineering.
0 Chap. 3 Control Flow 3.1 Statements and Blocks Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 4, 3 April if, if … else.
Chapter 8 Statement-Level Control Structure. Introduction Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
Control Flow C and Data Structures Baojian Hua
PZ07B Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07B - Basic statements Programming Language Design.
Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.
COMP4730/2002/lec8/H.Melikian Statement-Level Control Structures Introduction Compound Statements Selection Statements Iterative Statements Unconditional.
Principles of programming languages 2: Answers for exercises
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
CPS120 Introduction to Computer Science Iteration (Looping)
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
1 Control Flow: Expressions, Sequencing & Selection (Section ) A compilation of material developed by Felix Hernandez-Campos and Michael Scott.
Principle of Programming Lanugages 2: Imperative languages Isao Sasano Department of Information Science and Engineering ( structured programming, control.
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
1 CS Programming Languages Class 11 September 26, 2000.
sequence of execution of high-level statements
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Ch. 5 Ch. 51 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (more notes) Dr. Carter Tiernan.
CSI 3120, Control, page 1 Control statements Simple statements Basic structured statements Sequence Selection Iteration The jump statement.
Chapter 8: Statement-Level Control Structures
Control Structures sequence of execution of high-level statements.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 7 Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
CPS120 Introduction to Computer Science Iteration (Looping)
Ch. 5 Ch. 51 jcmt Summer 2003Programming Languages CSE3302 Programming Languages (more notes) Summer 2003 Dr. Carter Tiernan.
Statement-Level Control Structures
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
LECTURE 18 Control Flow. CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
Imperative Programming Statements and invariants.
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSE3302 Programming Languages (notes continued)
Lecture 18 Control Flow.
Chapter 3 of Programming Languages by Ravi Sethi
Def: A control structure is a control statement and
Programmation impérative - Prof. Béat Hirsbrunner
Statements (6 of 6) A statement causes an action to be performed by the program. It translates directly into one or more executable computer instructions.
Principles of programming languages 4: Parameter passing, Scope rules
Chapter 8: Control Structures
Flow of Control.
FLOW OF CONTROL.
Flow of Control.
Coding Design, Style, Documentation and Optimization
CS1100 Computational Engineering
Control statements Simple statements Basic structured statements
Control Structures In Text: Chapter 8.
Flow of Control.
CSC215 Lecture Flow Control.
Statement-Level Control Structures
Program Flow.
ECE 103 Engineering Programming Chapter 20 Change in Flow of Control
CSC215 Lecture Control Flow.
Chapter 8: Statement Level Control Structures
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
PZ07B - Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Basic statements Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Presentation transcript:

Principle of Programming Lanugages 2: Imperative languages (structured programming, control flow) Department of Information Science and Engineering Isao Sasano

Imperative languages In imperative languages, computations are sequences of actions such assignments and procedure calls. Famous imperative languages: Fortran (1957, John Backus, American, 1977 Turing award), Algol 60 (1960, International committee) Pascal (1970, Niklaus Wirth, Swiss, 1984 Turing award), C (1972, Dennis Ritchie, American, 1983 Turing award) Original Fortran does not have loop constructs like while statements, so many goto statements are used. (Fortran 90 (1991) has loop constructs. ) Languages like Algol and Pascal have constructs like while statements and compound statements like begin-end.

Assignments Basic units of imperative languages are assignments, which change the values of variables. (Examples) x := 2+3 x := a[i] a[i] := x Procedure calls can indirectly change the values of variables. (Example) read(x)

Structured programming We should design programming languages so that we easily understand what kind of computations are performed when we see the program. Structured programming:The structure of program text should help us understand what the program does. Carefully designed structured programs can be just as efficient as unstructured ones. [Reference] Edsger Dijkstra, “Go to statement considered harmful”, Communication of the ACM, Vol. 11, No. 3, pp. 147-148, 1968.

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. Statements specify actions and the flow of control around actions, which we illustrate in Pascal. Compound statements (begin-end) Conditional statements (if-then, if-then-else) Looping constructs (while) Selection (case)

Basic statements --- assignments Assignments are single entry / single exit. For example, an assignment statement x := 3 has the following control flow. Entry point x := 3 Exit point

Sequences of statements In Pascal, a sequence of statements is represented by delimiting statements s1, s2, …, sn with semicolons. s1; s2; …; sn (ex. ) temp := x; x := y; y := temp

Compound statements (or blocks) In Pascal, by surrounding a sequence of statements by begin and end, we get a statement called a compound statement or a block. (ex.) begin temp := x; x := y; y := temp end We can write a compound statement wherever a statement can appear. The number of statements in a compound statement can be 0, so begin end is a compound statement. A compound statement is single entry / single exit when each of the statements in the compound statement is single entry / single exit. Entry temp := x x := y y := temp Exit

Conditional statements In Pascal, a conditional statement has either of the following form (E is an expression, S (with a subscript) is a statement). if E then S1 else S2 if E then S Entry (ex. ) if x=0 then begin x:=1; y:=3 end else x:=2 T F x=0 x:=1 x:=2 A conditional statement if E then S1 else S2 is single entry / single exit when S1 and S2 are single entry and single exit. y := 3 Exit

if-then statements (ex.) if x=0 then begin x:=1; y:=3 end Entry x=0 A conditional statement if E then S is single entry / single exit when S is single entry and single exit. y := 3 Exit

Loop statements (while statements) In Pascal, a while statement has the following form. while E do S Entry (ex.) while x > 0 do x := x-1 x>0 F T A while statement while E do S is single entry / single exit if the body S is single entry/ single exit. x := x-1 Exit

Selection In Pascal, a selection statement has the following form. case E of constant1 : S1; … constantn : Sn end (In C, a selection is a switch statement.) Entry 1 4 x 2 y := x y := x+2 y:= x+1 x := 0 (ex.) case x of 1 : begin y:=x; x:=0 end; 2: y:=x+1; 4: y:=x+2 end Exit

Handling special cases in loops Break and continue statements (in C) A break statement sends control out of the inner-most enclosing loop to the statement following the loop. A continue statement repeats the inner-most enclosing loop by sending control to the beginning of the loop.

An example using a break statement Entry while x>0 do begin if x=5 then break; x := x-1 end T F x>0 T x=5 The if statement is single entry/ two exits but the while statement is single entry / single exit. F x := x-1 Exit

An example using a continue statement while x>0 do begin if x  8 then x := x-1; continue end; x := x-5 end Entry T F x>0 F x8 T x := x-5 x := x-1 The if statement is single entry/ two exits but the while statement is single entry / single exit. Exit

goto statements Entry goto statements has the following form. goto label x := x - 4 T F x>0 (ex.) L: x := x - 4; while x>0 do if x=8 then goto L else x := x-1 T x=8 F x := x-1 Now the while statement as well as the if statement is single entry / two exits. Exit

return statements In Modula-2, return statements have either of the following forms. return return E (In C, they are return; or return E;.) A return statement sends control back to the caller (with carrying the value of E). A return statement sends control out of the enclosing procedure (or function), while a break statement sends control out of the enclosing loop.

Exercise 1 Illustrate the control flow graph of the following program fragment. if x > 0 then x := x – 1 else if y > 0 then y := y – 1 else y := y + 1

Exercise 2 Illustrate the control flow graph of the following program fragment. while x>0 do begin if x=3 then break; y := y + 1; x := x - 1 end

Exercise 3 Illustrate the control flow graph of the following program fragment. while x>0 do begin while y>0 do if x=3 then break; z := z + 1; y := y - 1 end; x := x – 1 end

Exercise 4 Illustrate the control flow graph of the following program fragment. while x>0 do begin while y>0 do if x3 then y := y – 1; continue end z := z + 1; y := y - 1 end; x := x – 1

Exercise 5 Illustrate the control flow graph of the following program fragment. x := 10; sum := 0; L: sum := sum + x; x := x – 1; if x > 0 then goto L

Exercise 6 Illustrate the control flow graph of the following program fragment. y := 3; case x of 1 : y := 1; 2 : y := x * 2; 3 : if z = 0 then y := y * y else y := y * y * y end

Exercise 7 How many entries and exits do the two if statements have? while x>0 do begin while y>0 do if x=3 then break; L: z := z + 1; y := y - 1 end; x := x – 1; if x = 2 then goto L end