Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.

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

Programming Languages and Paradigms
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 –
COEN Expressions and Assignment
Copyright © by Curt Hill Expressions and Assignment Statements Part 2.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
ISBN Chapter 7 Expressions and Assignment Statements.
CSE 452: Programming Languages Expressions and Control Flow.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
Expressions, Evaluation and Assignments
Expressions & Assignments One of the original intentions of computer programs was the evaluation of mathematical expressions –languages would inherit much.
Imperative Programming
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
COMP4730/2002/lec7/H.Melikian Arithmetic Expressions Overloaded Operators Type Conversations Relational and Boolean Expressions Short Circuit Evaluation.
CS 363 Comparative Programming Languages Expressions and Assignment Statements.
Gary MarsdenSlide 1University of Cape Town Principles of programming language design Gary Marsden Semester 2 – 2001.
Names Variables Type Checking Strong Typing Type Compatibility 1.
CS 2104 Prog. Lang. Concepts Subprograms
Programming Languages and Design Lecture 7 Subroutines and Control Abstraction Instructor: Li Ma Department of Computer Science Texas Southern University,
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Programming Languages The Beginning. In the beginning... Computers were very expensive; programmers were cheap Programming was by plugboards or binary.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
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.
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
CSI 3120, Expressions and assignment, page 1 Expressions and assignment Points for discussion Arithmetic expressions Overloading Logical expressions Assignment.
1 Type Checking Type checking ensures that the operands and the operator are of compatible types Generalized to include subprograms and assignments Compatible.
Subprograms subroutines, procedures, functions, methods.
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Programming Languages and Paradigms Imperative Programming.
Arithmetic Expressions
Control Structures sequence of execution of high-level statements.
3. Controlling Program Flow Methods, parameters, and return values Boolean expressions Conditional branching Loops.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
I Power Higher Computing Software Development High Level Language Constructs.
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
JavaScript, Fourth Edition
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
 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.
C Part 1 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens A History Lesson Development of language by Dennis Ritchie at Bell.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Information and Computer Sciences University of Hawaii, Manoa
7.2 Arithmetic Expressions
Lecture 18 Control Flow.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Chapter 8: Control Structures
Chap. 6 :: Control Flow Michael L. Scott.
Arithmetic Expressions
Chap. 6 :: Control Flow Michael L. Scott.
Coding Concepts (Basics)
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Homework Any Questions?.
Languages and Compilers (SProg og Oversættere)
CSC215 Lecture Control Flow.
Presentation transcript:

Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000

Gary MarsdenSlide 2University of Cape Town Expressions  “spare the rod and spoil the child”  Expressions are comprised of operands and operators  Languages have precise precedence rules about which operand should be evaluated first –4 + 4 * 3 = 24 or 16  This is not universal (though almost so) –APL evaluates operators from right to left  Operators usually binary (dyadic) or unary (monadic)

Gary MarsdenSlide 3University of Cape Town Boolean  Boolean values crop up everywhere  Some languages use true/false, some use zero/non-zero  Can use short-circuit evaluation –in a&&b, if ‘a’ is false, don’t evaluate ‘b’ –in a||b, if ‘a’ is true, don’t evaluate ‘b’ –side effect problems  Operators can be confusing to novices –&&, AND, and then –||, OR, or else

Gary MarsdenSlide 4University of Cape Town Assignment statements  General form a:= b –‘a’ is a reference and ‘b’ is a value  In C, assignment is an expression, not a statement: allows a = b = c = 5;  Type incompatibility is handled in different ways –some resolved through automatic coercion int -> real is OK (widening) real -> int looses information –Information losses Pascal not allowed, ok in C, but float -> int is truncated double -> float is rounded

Gary MarsdenSlide 5University of Cape Town Sequence and control  FORTRAN introduced ‘IF’ –IF (C1) L1, L2, L3 –really a glorified goto jumping to L1 when C1 0  Nested IF’s –if C1 then if C2 then S1 else S2  Dangling else solve by –else binds to inner if in Pascal and C –use ‘elsif’ in Ada and Modula 2 –use switch or case

Gary MarsdenSlide 6University of Cape Town Conditional Iteration  While statement a big favourite –while C do S (S may never be executed)  Pascal introduced Repeat –repeat S until C (S executed at least once)  Less structured version – Loop –loop S end loop where S contains exit when C –this allows multiple exits

Gary MarsdenSlide 7University of Cape Town Fixed iteration  The for statement –loops for a set number of times –control variable should be ordinal (Algol60 used reals) –shouldn’t change control variable in loop body –shouldn’t force at least one execution (Algol66) –value of the loop variable at end of loop is a problem some languages limit loop variable scope to loop body officially undefined in Pascal –Languages with enumerated types also permit iterators for x in do

Gary MarsdenSlide 8University of Cape Town Exceptions  Introduced in PL/1, exceptions allow programs to react to exception circumstances by jumping to an exception handler  Usually there is a chunk of code (“try block” in Java, “frame” in Ada) which could throw an exception –control is passed (caught) to exception handler –if handled correctly control is returned to end of frame / block in Ada / Java line after exception generator in PL/1

Gary MarsdenSlide 9University of Cape Town Sub-programs  Complexity control and stepwise refinement required that code be split into sub-programs  Some languages (Pascal,Ada) have functions and procedures  Other languages (C++) treat procedures as a special case of function –good idea

Gary MarsdenSlide 10University of Cape Town Declaring sub-programs  Usually something to denote difference between program and sub-program –Pascal: “Procedure” “Function” “Program” –Clean: “Module”  Form is similar – each allow (local) declarations  “Formal” parameters matched to “actual” during execution –FORTRAN and K&R C had no type checking on parameters

Gary MarsdenSlide 11University of Cape Town FORTRAN’s model  Fortran supports parameter passing but also has “Common” variables  Each subroutine has a “Common” list, were values are matched on position, not name sub1: COMMON A,B,C sub2: COMMON D,E,F sub3: COMMON P,X –this is called “overlaying”  Fortran also supports separate compilation of subroutines –promoted NAG library

Gary MarsdenSlide 12University of Cape Town Other approaches  The Algol family store code in one file to permit parameter type checking  This does little for code re-use –Modula-2 cludges round this using extra files and EXPORT lists identical to Clean  C has always had separate compilation  Variables external to a file can be inculded using the “external” keyword –no type checking though

Gary MarsdenSlide 13University of Cape Town Parameter passing  Three basic things happen to parameters 1.Value is passed in but unchanged (in) 2.Value is undefined initially but a value is placed in the parameter after subroutine completion (out) 3.Parameter passes in value which is modified by executing subroutine (in-out)  Although there are three basic concepts, each language implements these differently

Gary MarsdenSlide 14University of Cape Town Going in  Values can be passed in by “value” or  by “constant-value” (Ada) –The idea of constant-value is that the value of the parameter cannot be altered in the subroutine –This is to stop accidental modification of parameters which were intended as “in” but end up as “in-out”

Gary MarsdenSlide 15University of Cape Town Coming out  This really only exists in Ada in its pure form  Ada uses “Call by result”  This creates a instantiation of the formal parameter for use in the subroutine  At the end of evaluation, the value of the formal parameter is copied to the actual parameter (which must be a variable)

Gary MarsdenSlide 16University of Cape Town Updating  Two main methods –Call by value-result: Another Ada foible, like “Call by result” value of actual instantiated formal –Call by reference: Most common, where a pointer is passed to the subroutine this is the most common form

Gary MarsdenSlide 17University of Cape Town Other thoughts  “Call-by-name” – Algol copied text of actual parameter to replace formal parameter  Default values to formal parameters –Good old Ada; a bit redundant in my opinion  Overloading – provided different formal parameters – Ada, C++, Java  Functional languages – no ‘out’ or ‘in-out’ parameters