Iterative Statements. Iterative Statements: Examples. Counter-Controlled Loops. ICOM 4036 Programming Languages Statement-Level Control Structure

Similar documents
Chapter 8. Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures ISBN

Chapter 8. Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures

Statement level control structures

Chapter 8 Statement-Level Control Structures

Chapter 7. - FORTRAN I control statements were based directly on IBM 704 hardware

Control Structures. Outline. In Text: Chapter 8. Control structures Selection. Iteration. Gotos Guarded statements. One-way Two-way Multi-way

Chapter 8 Statement-Level Control Structure

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

Ch. 7: Control Structures

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

Flow of Control Execution Sequence

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

G Programming Languages - Fall 2012

Principles of Programming Languages Lecture 22

LECTURE 18. Control Flow

Array Initialization. Rectangular and Jagged Arrays. Arrays Operations. ICOM 4036 Programming Languages. Data Types

Statement-Level Control Structures

Statement-Level Control Structures

CS 199 Computer Programming. Spring 2018 Lecture 5 Control Statements

Expressions vs statements

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

Lecture 7 Tao Wang 1

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

Example Scheme Function: equal

Principles of Programming Languages. Lecture Outline

Chapter 7 Control I Expressions and Statements

Software Engineering using Formal Methods

Chapter 6 Control Flow. June 9, 2015

Formal Specification and Verification

ECE 122. Engineering Problem Solving with Java

Software Engineering using Formal Methods

Flow Control. CSC215 Lecture

Concepts of Programming Languages

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Lecture 15: Iteration and Recursion

Programming for Engineers Iteration

Ruby: Introduction, Basics

CSC 326H1F, Fall Programming Languages. What languages do you know? Instructor: Ali Juma. A survey of counted loops: FORTRAN

Structure of Programming Languages Lecture 5

1 Lexical Considerations

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

Applications of Formal Verification

Chapter 5: Control Structures II (Repetition) Objectives (cont d.) Objectives. while Looping (Repetition) Structure. Why Is Repetition Needed?

Programming Languages Semantics

Applications of Formal Verification

Applications of Formal Verification

Expressions and Statements. Department of CSE, MIT, Manipal

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Introduction to C/C++ Lecture 3 - Program Flow Control

Ordering Within Expressions. Control Flow. Side-effects. Side-effects. Order of Evaluation. Misbehaving Floating-Point Numbers.

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 5: Control Structures II (Repetition)

Chapter 5. Names, Bindings, and Scopes

Repetition Structures

Ruby: Introduction, Basics

Software II: Principles of Programming Languages. Why Expressions?

The role of semantic analysis in a compiler

Computer Programming I - Unit 5 Lecture page 1 of 14

INF 212 ANALYSIS OF PROG. LANGS ELEMENTS OF IMPERATIVE PROGRAMMING STYLE. Instructors: Crista Lopes Copyright Instructors.

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

Lexical Considerations

C-LANGUAGE CURRICULAM

Introduction. C provides two styles of flow control:

Control Flow COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Copyright 2008 CS655 System Modeling and Analysis. Korea Advanced Institute of Science and Technology

Python review. 1 Python basics. References. CS 234 Naomi Nishimura

Why Is Repetition Needed?

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Programming Languages, Summary CSC419; Odelia Schwartz

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Lexical Considerations

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Programming Languages 2nd edition Tucker and Noonan"

Imperative Programming Languages (IPL)

Chapter 7. Expressions and Assignment Statements ISBN

UNIT 3

Chapter 7. Expressions and Assignment Statements

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

PLD Semester Exam Study Guide Dec. 2018

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

The SPL Programming Language Reference Manual

CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Theory of control structures

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Iteration: Intro. Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times. 2. Posttest Condition follows body Iterates 1+ times

REPETITION CONTROL STRUCTURE LOGO

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

C++ Programming: From Problem Analysis to Program Design, Third Edition

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

LECTURE 5 Control Structures Part 2

R13 SET Discuss how producer-consumer problem and Dining philosopher s problem are solved using concurrency in ADA.

int foo() { x += 5; return x; } int a = foo() + x + foo(); Side-effects GCC sets a=25. int x = 0; int foo() { x += 5; return x; }

Transcription:

ICOM 4036 Programming Languages Statement-Level Control Structure Selection Statement Iterative Statements Unconditional Branching Guarded Commands This lecture covers review questions 8-16 This lecture covers problems 1-8 Iterative Statements The repeated execution of a statement or compound statement is accomplished either by iteration or recursion General design issues for iteration control statements: 1. How is iteration controlled? 2. Where is the control mechanism in the loop? Dr. Amirhossein Chinaei, ECE, UPRM Spring 2010 ***Some slides are adapted from the Sebesta s textbook ICOM 4036. Ch. 8. 312 Counter-Controlled Loops A counting iterative statement has a loop variable, and a means of specifying the initial and terminal, and stepsize values Design Issues: 1. What are the type and scope of the loop variable? 2. Should it be legal for the loop variable or loop parameters to be changed in the loop body, and if so, does the change affect loop control? 3. Should the loop parameters be evaluated only once, or once for every iteration? ICOM 4036. Ch. 8. 313 FORTRAN 95 syntax DO label var = start, finish [, stepsize] Stepsize can be any value but zero Parameters can be expressions FORTRAN 95: a second form: [name:] Do variable = initial, terminal [,stepsize]... End Do [name] Design choices: 1. Loop variable must be INTEGER 2. The loop variable cannot be changed in the loop, but the parameters can; because they are evaluated only once, it does not affect loop control (Loop parameters are evaluated only once) 3. Cannot branch into either of Fortran s Do statements 314 1

Ada for var in [reverse] discrete_range loop... end loop Design choices: 1. Type of the loop variable is that of the discrete range (A discrete range is a sub-range of an integer or enumeration type). 2. Loop variable does not exist outside the loop 3. The loop variable cannot be changed in the loop, but the discrete range can; it does not affect loop control 4. The discrete range is evaluated just once 5. Cannot branch into the loop body ICOM 4036. Ch. 8. 315 C-based languages for ([expr_1] ; [expr_2] ; [expr_3]) statement The expressions can be whole statements, or even statement sequences, with the statements separated by commas The value of a multiple-statement expression is the value of the last statement in the expression If the second expression is absent, it is an infinite loop Design choices: 1. There is no explicit loop variable 2. Everything can be changed in the loop 3. The first expression is evaluated once, but the other two are evaluated with each iteration ICOM 4036. Ch. 8. 316 C++ differs from C in two ways: 1. The control expression can also be Boolean 2. The initial expression can include variable definitions (scope is from the definition to the end of the loop body) Java and C# Differs from C++ in that the control expression must be Boolean Python for loop_variable in object: - loop body [else: - else clause] The object is often a range, which is either a list of values in brackets ([2, 4, 6]), or a call to the range function (range(5)), which returns 0, 1, 2, 3, 4 The loop variable takes on the values specified in the given range, one for each iteration The else clause, which is optional, is executed if the loop terminates normally ICOM 4036. Ch. 8. 317 ICOM 4036. Ch. 8. 318 2

Logically-Controlled Loops Repetition control is based on a Boolean expression Design issues: Pretest or posttest? Should the logically controlled loop be a special case of the counting loop statement or a separate statement? Logically-Controlled Loops: Examples C and C++ have both pretest and posttest forms, in which the control expression can be arithmetic: while (ctrl_expr) loop body do loop body while (ctrl_expr) Java is like C and C++, except the control expression must be Boolean (and the body can only be entered at the beginning Java has no goto) ICOM 4036. Ch. 8. 319 ICOM 4036. Ch. 8. 320 Logically-Controlled Loops: Examples Ada has a pretest version, but no posttest FORTRAN 95 has neither Perl and Ruby have two pretest logical loops, while and until. Perl also has two posttest loops User-Located Loop Control Mechanisms Sometimes it is convenient for the programmers to decide a location for loop control (other than top or bottom of the loop) Simple design for single loops (e.g., break) Design issues for nested loops 1. Should the conditional be part of the exit? 2. Should control be transferable out of more than one loop? ICOM 4036. Ch. 8. 321 ICOM 4036. Ch. 8. 322 3

User-Located Loop Control Mechanisms break and continue C, C++, Python, Ruby, and C# have unconditional unlabeled exits (break) Java and Perl have unconditional labeled exits (break in Java, last in Perl) C, C++, and Python have an unlabeled control statement, continue, that skips the remainder of the current iteration, but does not exit the loop Java and Perl have labeled versions of continue Iteration Based on Data Structures Number of elements of in a data structure control loop iteration Control mechanism is a call to an iterator function that returns the next element in some chosen order, if there is one; else loop is terminate C's for can be used to build a user-defined iterator: for (p=root; p==null; traverse(p)){} ICOM 4036. Ch. 8. 323 ICOM 4036. Ch. 8. 324 Iteration Based on Data Structures (cont d) PHP current points at one element of the array next moves current to the next element reset moves current to the first element Java For any collection that implements the Iterator interface next moves the pointer into the collection hasnext is a predicate remove deletes an element Perl has a built-in iterator for arrays and hashes, foreach Iteration Based on Data Structures (cont d) C# s foreach statement iterates on the elements of arrays and other collections: Strings[] = strlist = {"Bob", "Carol", "Ted"}; foreach (Strings name in strlist) Console.WriteLine ("Name: {0}", name); - The notation {0} indicates the position in the string to be displayed ICOM 4036. Ch. 8. 325 ICOM 4036. Ch. 8. 326 4

Unconditional Branching Transfers execution control to a specified place in the program Represented one of the most heated debates in 1960 s and 1970 s Major concern: Readability Some languages do not support goto statement (e.g., Java) C# offers goto statement (can be used in switch statements) Loop exit statements are restricted and somewhat camouflaged goto s Guarded Commands Designed by Dijkstra Purpose: to support a new programming methodology that supported verification (correctness) during development Basis for two linguistic mechanisms for concurrent programming (in CSP and Ada) Basic Idea: if the order of evaluation is not important, the program should not specify one ICOM 4036. Ch. 8. 327 ICOM 4036. Ch. 8. 328 Selection Guarded Command Form if <Boolean exp> -> <statement> [] <Boolean exp> -> <statement>... [] <Boolean exp> -> <statement> fi Semantics: when construct is reached, Evaluate all Boolean expressions If more than one are true, choose one nondeterministically If none are true, it is a runtime error ICOM 4036. Ch. 8. 329 Loop Guarded Command Form do <Boolean> -> <statement> [] <Boolean> -> <statement>... [] <Boolean> -> <statement> od Semantics: for each iteration Evaluate all Boolean expressions If more than one are true, choose one nondeterministically; then start loop again If none are true, exit loop ICOM 4036. Ch. 8. 330 5

Guarded Commands: Rationale Connection between control statements and program verification is intimate Verification is impossible with goto statements Verification is possible with only selection and logical pretest loops Verification is relatively simple with only guarded commands Summary Variety of statement-level structures Choice of control statements beyond selection and logical pretest loops is a trade-off between language size and writability Functional and logic programming languages are quite different control structures (not discussed here) ICOM 4036. Ch. 8. 331 ICOM 4036. Ch. 8. 332 6