Imperative languages Most familiar: computation modifies store

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

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:
Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
Control Structures. Hierarchical Statement Structure Standard in imperative languages since Algol60. Exceptions: Early FORTRAN, COBOL, early BASIC, APL.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Control Structures Any mechanism that departs from straight-line execution: –transfer of control (closest to machine): gotos –Selection: if-statements.
Chapter 8 . Sequence Control
COMP4730/2002/lec8/H.Melikian Statement-Level Control Structures Introduction Compound Statements Selection Statements Iterative Statements Unconditional.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Control Structures Programs have 4 basic control structures:
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
ISBN Chapter 8 Statement-Level Control Structures.
1 CS Programming Languages Class 11 September 26, 2000.
sequence of execution of high-level statements
Control Structures sequence of execution of high-level statements.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
LECTURE 18 Control Flow. CONTROL FLOW Sequencing: the execution of statements and evaluation of expressions is usually in the order in which they appear.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Flow of Control Joe McCarthy CSS 161: Fundamentals of Computing1.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Imperative Programming Statements and invariants.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Information and Computer Sciences University of Hawaii, Manoa
Review 1.
Lecture 18 Control Flow.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Def: A control structure is a control statement and
Structure of Programming Language
Expressions and Assignment
8.1 Introduction - Levels of Control Flow: 1. Within expressions
CS 326 Programming Languages, Concepts and Implementation
Dr. Vamsi Paruchuri University of Central Arkansas
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Statement-Level Control Structures
Chapter 8: Control Structures
Expressions and Assignment Statements
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Java - Data Types, Variables, and Arrays
Control statements Simple statements Basic structured statements
Control Structures In Text: Chapter 8.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 6: Repetition Statements
Statement-Level Control Structures
CISC124 Labs start this week in JEFF 155. Fall 2018
CMPE212 – Reminders The other four assignments are now posted.
Chapter8: Statement-Level Control Structures April 9, 2019
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Conditional Loops Counted Loops
Chap 7. Advanced Control Statements in Java
Review of Previous Lesson
CSC215 Lecture Control Flow.
Expressions and Assignment Statements
Controlling Program Flow
Looping and Repetition
Presentation transcript:

Imperative languages Most familiar: computation modifies store Computation evolves through assignments Control structures determine flow of execution Data structures establish organization of memory Type checking usually done at compile time Languages usually compiled for efficiency

Control Structures Any mechanism that departs from straight- line execution: Selection: if-statements Multiway-selection: case statements Unbounded iteration: while-loops Definite iteration: for-loops Iterations over collections transfer of control: gotos unbounded transfer of control: exceptions, backtracking (not common)

Selection if Condition then Statement -- Pascal, Ada if (Condition) Statement -- C, C++ Java All you need for a universal machine: increment, decrement, branch on zero. All the rest is programmer convenience! To avoid ambiguities, use end marker: end if, “}” To deal with alternatives, use keyword or bracketing: if Condition then if (Condition) { Statements Statements } elsif Condition then else if (Condition) { Statements Statements} else else { Statements Statements} end if;

Nesting if Condition then if (Condition) Statements Statements end if; } else else { Statements Statements end if; }

Statement Grouping Pascal introduces begin-end pair to mark sequence C/C++/Java abbreviate keywords to { } Ada dispenses with brackets for sequences, because keywords for the enclosing control structure are sufficient: for J in 1 .. N loop … end loop; More writing => more readable The use of grouping in C a reminder that it is an expression language The use of grouping in C++/Java is just syntactic tradition Another possibility (ABC, Python): make indentation significant

Short-Circuit Evaluation If x is more than five times greater than y, compute z: if x / y > 5 then z := … -- but what if y is 0? If y /= 0 and x/ y > 5 then z := … -- but operators evaluate their arguments Solutions: a lazy evaluation rule for logical operators (LISP, C, etc) a control structure with a different syntax C1 && C2 does not evaluate C2 if C1 is false if C1 and then C2 then ditto C1 || C2 does not evaluate C2 if C1 is true if C1 or else C2 then ditto

Multiway selection The case statement is the most useful control structure because most programs are interpreters. Can be simulated with a sequence of if- statements, but logic become obscured. case Next_Char is when ‘I’ => Val := 1; when ‘V’ => Val := 5; when ‘X’ => Val := 10; when ‘C’ => Val := 100; when ‘D’ => Val := 500; when ‘M’ => Val := 1000; when others => raise Illegal_Numeral; end case;

The well-structured case statement Type of expression must be discrete: an enumerable set of values (floating-point numbers not acceptable) each choice is independent of the others (no flow-through) There are no duplicate choices All possible choices are covered There is mechanism to specify a default outcome for choices not given explicitly.

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 All cases have the same execution cost All choices must be static: computable at compile-time

Complications case (X + 1) is -- any integer value (discrete but large) when integer’first .. 0 => Put_Line (“negative”); when 1 => Put_Line (“unit”); when 3 | 5 | 7 | 11 => Put_Line (“smal prime”); when 2 | 4 | 6 | 8 | 10 => Put_Line (“small even”); when 21 => Put_Line (“the house wins”); when 12 .. 20 | 22 .. 99 => Put_Line (“manageable”); when others => Put_Line (“Irrelevant”); end case; Implementation must be combination of tables and if-statements.

Unstructured flow (Duff’s device) void send (int* to, int* from, int count) { int n = (count + 7 ) / 8; switch (count % 8) { case 0 : do { *to++ = *from++; case 7 : *to++ = *from++; case 6 : *to++ = *from++; case 5 : *to++ = *from++; case 4 : *to++ = *from++; case 3 : *to++ = *from++; case 2 : *to++ = *from++; case 1 : *to++ = *from++; } while (--n >0); } What does this do? Why bother?

Indefinite loops All loops can be expressed as while-loops Condition is evaluated at each iteration If condition is initially false, loop is never executed while Condition loop .. end loop; equivalent to if Condition then while Condition loop … end loop; end if; (provided Condition has no side-effects…)

What if we want to execute at least once? Pascal introduces until-loop. C/C++ use different syntax with while: while (Condition) { … } do { … } while (Condition) While form is most common Can always simulate with a boolean variable: first := True; while (Condition or else first) loop … first := False; end loop;

Breaking out More common is the need for an indefinite loop that terminates in the middle of an iteration. C/C++/Java: break Ada : exit statement loop -- infinite loop compute_first_part; exit when got_it; compute_some_more; end loop;

Multiple exits Within nested loops, useful to specify exit from several of them Ada solution: give names to loops Otherwise: use a counter (Modula) or use a goto. Outer: while C1 loop ... Inner: while C2 loop... Innermost: while C3 loop... exit Outer when Major_Failure; exit Inner when Small_Annoyance; ... end loop Innermost; end loop Inner; end loop Outer;

Definite loops Counting loops are iterators over discrete domains: for J in 1..10 loop … for (int I = 0; I < N; I++ ) .. Design issues: Evaluation of bounds (only once, ever since Algol60) Scope of loop variable Empty loops Increments other than one Backwards iteration non-numeric domains

Evaluation of bounds for J in 1 .. N loop … N := N + 1; end loop; -- terminates? In Ada, bounds are evaluated once before iteration starts. The above always terminates (and is abominable style). The C/C++/Java loop has hybrid semantics: for (int J = 0; J < last; J++) { last++; -- does not terminate! }

The loop variable Best if local to loop and treated as constant Avoids issue of value at termination, and value on abrupt exit counter : integer := 17; -- outer declaration ... for counter in 1 ..10 loop do_something; -- 1 <= counter <= 10 end loop; … -- counter is still 17

Different increments In Ada: The universal Algol60 form: for J from Exp1 to Exp2 by Exp3 do… Too rich for most cases. Exp3 is most often +1, -1. What is meaning if Exp1 > Exp2 and Exp3 < 0 ? In C/ C++ for (int J = Exp1; J <= Exp2; J = J + Exp3) … In Ada: for J in 1 .. N loop -- increment is +1 for J in reverse 1 .. N loop -- increment is -1 Everything else can be programmed with while-loop

Non-numeric domains Ada form generalizes to discrete types: for M in months loop … Basic pattern on other data-types: define primitive operations: first, next, more_elements: Iterator = Collection.Iterate(); // build an iterator over a collection of elements element thing = iterator.first; // define loop variable while (iterator.more_elements ()) { ... thing = iterator.next (); // value is each successive element }

How do we know it’s right? Pre-Conditions and Post-conditions: {P} S {Q} means: if Proposition P holds before executing S, and the execution of S terminates, then proposition Q holds afterwards. Need to formulate pre- and post-conditions for all statement forms, and syntax-directed rules of inference. {P and C} S {P} (P and C} while C do S end loop; {P and not C} i.e. on exit from a while-loop we know the condition is false.

Efficient exponentiation function Exp (Base : Integer; Expon : Integer) return integer is N : Integer := Expon; -- to pick up successive bits of exponent Res : Integer := 1; -- running result Pow : Integer := Base; -- successive powers: Base ** (2 ** K) begin while N > 0 loop if N mod 2 = 1 then Res := Res * Pow; end if; Pow := Pow * Pow; N := N / 2; end loop; return Res; end Exp;

Adding invariants function Exp (Base : Integer; Expon : Integer) return integer is … declarations … {i = 0} to count iterations begin while N > 0 loop {i := i + 1} if N mod 2 = 1 then { N mod 2 is ith bit of Expon from left} Res := Res * Pow; { Res := Base ** (Expon mod 2 ** i} end if; Pow := Pow * Pow; { Pow := Base ** (2 ** i)} N := N / 2; { N := Expon / ( 2 ** i) } end loop; return Res; {i = log Expon; N = 0; Res = Base ** Expon} end Exp;