CPSC 3740 Programming Languages University of Lethbridge. Control Structures

Similar documents
Chapter 8. Statement-Level Control Structures

Chapter 8 Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures

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

Chapter 8. Statement-Level Control Structures ISBN

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

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

Statement level control structures

LECTURE 18. Control Flow

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

Ch. 7: Control Structures

G Programming Languages - Fall 2012

Lecture 15: Iteration and Recursion

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

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

COP4020 Programming Languages. Control Flow Prof. Robert van Engelen

Lecture 7 Tao Wang 1

Chapter 7 Control I Expressions and Statements

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Principles of Programming Languages Lecture 22

Flow Control: Branches and loops

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

Control Structures in Java if-else and switch

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

Introduction. C provides two styles of flow control:

Flow of Control Execution Sequence

Expressions vs statements

Statement-Level Control Structures

Statement-Level Control Structures

PLD Semester Exam Study Guide Dec. 2018

1 Little Man Computer

Chapter 8 Statement-Level Control Structure

Control Structures of C++ Programming (2)

Structure of Programming Languages Lecture 5

CS302: Self Check Quiz 2

CSE 307: Principles of Programming Languages

Symbolic Computation and Common Lisp

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

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

15.083J Integer Programming and Combinatorial Optimization Fall Enumerative Methods

Control Structures 1 / 17

COP4020 Programming Assignment 1 - Spring 2011

Chapter 7. Expressions and Assignment Statements ISBN

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Loops. CSE 114, Computer Science 1 Stony Brook University

Software II: Principles of Programming Languages. Why Expressions?

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

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Chapter 7. Expressions and Assignment Statements

Loops and Conditionals. HORT Lecture 11 Instructor: Kranthi Varala

Control Structures in Java if-else and switch

CSC Java Programming, Fall Java Data Types and Control Constructs

CSE 230 Computer Science II (Data Structure) Introduction

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CMSC 330: Organization of Programming Languages. Rust Basics

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Chapter 4 The If Then Statement

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

5 The Control Structure Diagram (CSD)

Outline. 1 If Statement. 2 While Statement. 3 For Statement. 4 Nesting. 5 Applications. 6 Other Conditional and Loop Constructs 2 / 19

Computer Programming I - Unit 5 Lecture page 1 of 14

COS 140: Foundations of Computer Science

SOFT 161. Class Meeting 1.6

Indicate the answer choice that best completes the statement or answers the question. Enter the appropriate word(s) to complete the statement.

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Project 2: Scheme Interpreter

DECISION MAKING STATEMENTS

Information Science 1


ECE 122. Engineering Problem Solving with Java

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Example of Objective Test Questions for Test 4

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Conditionals and Loops

CS323 Lecture: Control Structures last revised 3/4/09

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

CS201 - Introduction to Programming Glossary By

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

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

CSI33 Data Structures

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

Key Differences Between Python and Java

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

Chapter 6 Control Flow. June 9, 2015

Expressions and Statements. Department of CSE, MIT, Manipal

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators


B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Senet. Language Reference Manual. 26 th October Lilia Nikolova Maxim Sigalov Dhruvkumar Motwani Srihari Sridhar Richard Muñoz

Flow Control. CSC215 Lecture

Following is the general form of a typical decision making structure found in most of the programming languages:

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Programming Languages Semantics

Transcription:

Control Structures A control structure is a control statement and the collection of statements whose execution it controls. Common controls: selection iteration branching Control Structures 1 15 Howard Cheng

Selection Statements Two-way selection is commonly implemented as if-then-else The control expression is usually a Boolean expression C, C++, Python allow arithmetic expressions Some require single statements in clauses (but can be blocks). Others have ways to delimint clauses (e.g. END, indentation) Without explicit END, else is matched with closest unmatched if. In functional languages, selection is an expression Control Structures 2 15 Howard Cheng

Multiple-Selection Statements Allows one of any number of statement groups to be selected. Typically as switch or case Issues: What expressions are allowed for controlling the selection? How are the segments specificied? Is execution flow restricted to only one segment? How are case values specified? Unrepresented selector values? Control Structures 3 15 Howard Cheng

C/C++ switch statement Only discrete types (e.g. integer, character, enum) can be used in the selection expression Case values are compile time constants Allows fall through to other cases without a break Optional default case Control Structures 4 15 Howard Cheng

Multiple-Selection Statements Other types may be allowed in selection expressions. e.g. C# allows strings Ruby allows any Boolean expressions One way to implement multiple-selection would be to use linear search through the cases. Inefficient if there are many cases Compiler can build a hash table of addresses to jump to If ranges in case values are allowed, binary search is needed C/C++: tables are easy to build Control Structures 5 15 Howard Cheng

Multiple-Selection Statements Sometimes we need to use if-then-else if then else if... for multiple selection Some languages have special words for else-if. e.g. elif Lisp-type languages have a special form called cond Control Structures 6 15 Howard Cheng

Iterative Statements Allows statements to be executed 0, 1, or more times Often called a loop There is a loop body, and a test to determine if another iteration should be performed The test can be a pretest or posttest Control Structures 7 15 Howard Cheng

Counter-controlled Loops A loop variable with initial and terminal values is used to control the loop execution There may be a step size allowed Issues: type and scope of loop variable is loop variable read-only inside the loop body loop parameters evaluated once or every iteration Control Structures 8 15 Howard Cheng

C-based for loops Three parts: initialization, test, and increment Local variable can be defined in initialization Test evaluated each time No strict requirement about test or increment, any of the parts can be empty Used for counting, but not necessarily Loop variable may be modified Control Structures 9 15 Howard Cheng

Python for loops Loop variable is assigned a value in some object, usually a range: e.g. for x in [1,2,3,4] : There is an optional else clause after termination range can be used to construct ranges Control Structures 10 15 Howard Cheng

Functional Languages Pure functional languages have no counter and cannot implement counter-controlled loops iteratively Recursion is used Control Structures 11 15 Howard Cheng

Logically Controlled Loops Whether an iteration is performed is determined by the value of some Boolean test Test can be done before the loop iteration (pretest) or after the loop iteration (posttest) while loops, do-while, repeat-until, etc. Posttest loops: always execute at least once Many languages allow for user-located loop control: break, continue, labelled break. Control Structures 12 15 Howard Cheng

Iteration Based on Data Structures Loop over elements in some data structure Sometimes called range based for loops Available in Java, C++, and many newer languages (foreach) Control Structures 13 15 Howard Cheng

Unconditional Branching Implemented by goto Allows control to be transferred to a specified location Most languages have some restrictions on possible transfer locations Reduces readability Often used for handling errors or other unusual conditions Control Structures 14 15 Howard Cheng

Guarded Commands A guarded command is a set of Boolean expressions and associated statements to execute For each Boolean expression that evaluates to true, one of the associated statements is chosen to execute nondeterministically If none of the Boolean expression evaluates to true, a run-time error occurs Useful to simplify program correctness proofs, but not so useful in practical languages Control Structures 15 15 Howard Cheng