Structure of Programming Languages Lecture 5

Similar documents
Chapter 8 Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures

Chapter 8. Statement-Level Control Structures

Chapter 2: Functions and Control Structures

LECTURE 18. Control Flow

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Chapter 8. Statement-Level Control Structures

Shell CSCE 314 TAMU. Haskell Functions

Introduction. C provides two styles of flow control:

Second Term ( ) Department of Computer Science Foundation Year Program Umm Al Qura University, Makkah

G Programming Languages - Fall 2012

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

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

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

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

A Tour of Language Implementation

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

Repetition Structures

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

Chapter 8. Statement-Level Control Structures ISBN

Flow Control. CSC215 Lecture

Homework. Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25

UNIT 3

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

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

REPETITION CONTROL STRUCTURE LOGO

Test #2 October 8, 2015

Chapter 6 Control Flow. June 9, 2015

Reading: Chapter 17 Homework: All exercises from Chapter 17 Due: 10/27 Correction: Chapter 16 homework is due 10/25

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 09/17/2015

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

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

LESSON 3. In this lesson you will learn about the conditional and looping constructs that allow you to control the flow of a PHP script.

Functional Programming. Big Picture. Design of Programming Languages

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

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

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

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

Lecture 9: Control Flow

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

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CS 314 Principles of Programming Languages

Flow Control: Branches and loops

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

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

COP 1220 Introduction to Programming in C++ Course Justification

Text Input and Conditionals

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Lecture 15: Iteration and Recursion

CS 415 Midterm Exam Spring 2002

CSE 115. Introduction to Computer Science I

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

Functional Programming Languages (FPL)

Programming Paradigms

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

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 12 09/29/2016

Statement level control structures

Introduction. chapter Functions

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

Quiz 1: Functions and Procedures

Theory of control structures

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

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

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

LECTURE 5 Control Structures Part 2

The role of semantic analysis in a compiler

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

Scope and Parameter Passing 1 / 19

Page # Expression Evaluation: Outline. CSCI: 4500/6500 Programming Languages. Expression Evaluation: Precedence

Module 4: Decision-making and forming loops

Chapter 5. Names, Bindings, and Scopes

CMSC330. Objects, Functional Programming, and lambda calculus

Chapter 7 Control I Expressions and Statements

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Control Statements. Objectives. ELEC 206 Prof. Siripong Potisuk

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

Boolean Expressions and if 9/14/2007

CHAPTER ONE OVERVIEW. 1.1 Continuation-passing style

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Ch. 7: Control Structures

Intro. Scheme Basics. scm> 5 5. scm>

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

2.2 (a) Statement, subroutine, procedure, function, parameter, loop

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

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

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Programming Systems in Artificial Intelligence Functional Programming

Last class. CS Principles of Programming Languages. Introduction. Outline

Senthil Kumaran S

CSCI 1101B. While Loops

Types and Type Inference

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Introduction. A. Bellaachia Page: 1

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Python I. Some material adapted from Upenn cmpe391 slides and other sources

Method & Tools for Program Analysis & Design

CSC 533: Organization of Programming Languages. Spring 2005

Transcription:

Structure of Programming Languages Lecture 5 CSCI 6636 4536 June, 2017 CSCI 6636 4536 Lecture 10... 1/16 June, 2017 1 / 16

Outline 1 Expressions and Evaluation 2 Control Structures Conditionals Repetition Jumps 3 Homework CSCI 6636 4536 Lecture 10... 2/16 June, 2017 2 / 16

Expressions and Evaluation In Praise of Laziness A result from Lambda Calculus: Outside-in evaluation is strictly more powerful than inside-out: if ( x!= 0) answer = z/x; else answer = z; O-I: Evaluate if (x!= 0) first, then choose one clause. I-O: Evaluate z/x first and bomb with a divide-by-zero error. Every programming language evaluates conditionals outside in. Most languages evaluate everything else inside-out. Modern functional languages use outside-in evaluation consistently; it is called lazy evaluation. Nothing is evaluated until and unless the result is needed. CSCI 6636 4536 Lecture 10... 3/16 June, 2017 3 / 16

Expressions and Evaluation The Functional Philosophy A result of using lazy evaluation is that the sequence in which lines of code are written is not the same as the order in which they are evaluated. (A statement sequence is an illusion the results would be the same if the lines were scrambled.) Instead, evaluation order is determined by how function calls are nested in the code, and by whether local variables and parameters are used at all. Functional languages use dynamic binding (no declarations) to attach names to objects, and allow only one binding during the lifetime of the name. Parameter binding is the primary means naming a value. CSCI 6636 4536 Lecture 10... 4/16 June, 2017 4 / 16

Control Structures Expressions vs. Statements Conditionals Loops Jumps CSCI 6636 4536 Lecture 10... 5/16 June, 2017 5 / 16

Expressions vs. Statements All programming languages have control elements that allow testing and repetition. In statement-based languages, these are usually control statements In functional languages, these are usually control expressions or functions Some languages support both but rely more heavily on one or the other. Every aspect of the syntax for control varies widely from language to language. CSCI 6636 4536 Lecture 10... 6/16 June, 2017 6 / 16

Structured vs. Unconstrained The control capabilities built into a language can be structured or unconstrained. A goto statement or expression is unconstrained: It can go to a target anywhere. Function calls are structured control because the define a controlled interface between two parts of a program and control returns to the point at which the call started. An if...then...else statement or a while loop is structured: it has a defined beginning and end, and control flows through it in a prescribed way. Exceptions and break statements are structured because they can only end up at defined spots in the program. The use of unconstrained control capabilities makes a program hard to debug and harder to maintain. CSCI 6636 4536 Lecture 10... 7/16 June, 2017 7 / 16

Minimal necessary control A minimal level of control is required in a computer language language: the programmer needs a way to write conditional code and to repeat blocks of code. if...then...else and recursion form a minimal adequate basis for a language. Functional languages are built on this foundation. if...then...else, the while loop and statement sequences also form an adequate basis for programming. Procedural languages are built on this foundation. However, many languages of both sorts provide a wide assortment of other control structures for convenience and program clarity. Note: A simple if, without the possibility of an else clause is not an adequate basis for defining a language. CSCI 6636 4536 Lecture 10... 8/16 June, 2017 8 / 16

Conditionals Conditional Semantics A conditional statement or expression consists of a test and two clauses. The first clause (the then clause) is used when the test result is true. The second clause (the else clause) is executed when the result is false. A conditional statement must be evaluated outside-in. The condition must be evaluated first. The result of the condition is then tested and used to select either the then clause or the else clause. This control pattern lets us handle special cases and avoid infinite loops or computations that would crash the program. CSCI 6636 4536 Lecture 10... 9/16 June, 2017 9 / 16

Conditionals Variations on the Conditional Keywords vary: if, cond, WHEN, then, elif, elseif, endif, switch, case, EVALUATE The way of delimiting the clauses varies: parentheses, brackets, case labels, keywords (then, elif, elseif, endif) Conditionals can test one condition or a series of conditions. There can be one test per possible action to execute (as in LISP cond) or one test altogether, followed by a sequence of several possible clauses, chosen based on the outcome of the test, which is a small integer. CSCI 6636 4536 Lecture 10... 10/16 June, 2017 10 / 16

Conditionals Conditional Expressions A conditional expression makes a test and returns a value. It can therefore be used in the middle of any expression, including another conditional expression. The conditional expression was supported by Algol-60. C s conditional expression is <test>? <true-action> : <false-action> Scheme and Lisp provide not-quite-identical versions of Haskell and Miranda provide guarded expressions. cond CSCI 6636 4536 Lecture 10... 11/16 June, 2017 11 / 16

Repetition Loops vs. Recursion A programming language needs a way to specify recursion. This could be done using recursion. It could be done using loops. Modern languages usually support both kinds of repetition, in one way or another. Generally, a loop is faster than a recursion to do the same thing. Often, a recursion is more concise than a loop. CSCI 6636 4536 Lecture 10... 12/16 June, 2017 12 / 16

Repetition Loop Statements A loop statement executes a process that changes memory and/or produces a side-effect. A for-each loop applies an action to every element of an array or list. The general loop (FORTH and C) has a body of code with an exit test potentially anywhere within that body. Restricted loops have an exit test at the top or at the bottom: while and do...while Counted loops: the syntax does not determine the semantics. Issues are: Count up or count down The step size can vary. Exit condition can be: >= or > Some languages put restrictions on changing the loop variable within the loop. Trip counts might be predictable... or not. CSCI 6636 4536 Lecture 10... 13/16 June, 2017 13 / 16

Repetition Loop Expressions A loop expression evaluates a function repeatedly and calculates an answer, often in the form of an array or list. In Scheme: map operates on lists and produces a list as its result. (map 1+ (list 1 2 3 4 5)) ;Value 2: (2 3 4 5 6) In APL any function can be applied to an array and produce an array: A 1 2 3 4 5 B 2 3 ρ ι 6 A + 1 B 2 2 3 4 5 6 2 4 6 8 10 12 CSCI 6636 4536 Lecture 10... 14/16 June, 2017 14 / 16

Jumps Jumps and GO TOs goto: No longer good practice because it has bad effects on translation modularity maintainability break and continue: maintain modular structure and eliminate almost all need for goto s. Some people frown on them because they create one-in-two-out modules. Loop labels and loop exits implement multi-level breaks. Exceptions Superman jumps Difficult to implement because the stack must be cleaned up. They eliminate all remaining need for goto. They must be caught and handled to ensure application stability. CSCI 6636 4536 Lecture 10... 15/16 June, 2017 15 / 16

Homework Homework Read Chapters 8 and 10 1 This expression can be evaluated inside-out or outside in: x=sqrt( 3*z - floor(y)) Which function is called first in an inside-out evaluation? Which is called first in an outside-in evaluation? 2 What is the difference between a control statement and a control expression? 3 List the kinds of loops that are supported in Python. 4 List the jump statements (things like break, continue, goto, and exceptions) that are supported in Ruby. Explain how each one works. 5 After reading the relevant section in the text, explain why unrestricted control statements (goto) make it difficult to maintain a large program. CSCI 6636 4536 Lecture 10... 16/16 June, 2017 16 / 16