Expressions and Statements. 2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

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.
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.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
P ROGRAMMING L ANGUAGES : C ONTROL 1. S LIDES R EFERENCES Kenneth C. Louden, Control I: Expressions and Statements: Principles and Practice. 2.
(8.1) COEN Control Structures  Control structure general issues  Compound statements  Selectors (conditional structures) – single – two-way –
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
True or false A variable of type char can hold the value 301. ( F )
ISBN Chapter 8 Statement-Level Control Structures.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
ISBN Chapter 8 Statement-Level Control Structures.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 8 . Sequence Control
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 7Louden, Programming Languages1 Chapter 7 - Control I: Expressions and Statements "Control" is the general study of the semantics of execution.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Sequence Control Chapter 6. 2 l Control structures: the basic framework within which operations and data are combined into programs. Sequence control.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Program Looping Making Decisions Copyright © 2012 by Yong-Gu Lee
PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.
ISBN Chapter 8 Statement-Level Control Structures.
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Exam Delay Exam II will be held during the first fifty (50) minutes of class on 13 November 2003.
8-1 Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 7 Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Chapter 8 © 2002 by Addison Wesley Longman, Inc Introduction - Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program.
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
ECE122 Feb 10, Unary Operator An operator that takes only a single operand Plus: + Minus: – Cast: (type). E.g. (double)
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Control I Expressions and Statements Lecture 9 – Control I, Spring CSE3302 Programming.
Repetition statements
Def: A control structure is a control statement and
Operators And Expressions
Expressions and Assignment
8.1 Introduction - Levels of Control Flow: 1. Within expressions
Decisions Chapter 4.
Test Review Computer Science History
Expressions and Control Flow in JavaScript
Chapter 8: Control Structures
Control Structures – Selection
Chap. 6 :: Control Flow Michael L. Scott.
Chapter 8 JavaScript: Control Statements, Part 2
Control Structures In Text: Chapter 8.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
Chap. 6 :: Control Flow Michael L. Scott.
CSE 3302 Programming Languages
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Chap 7. Advanced Control Statements in Java
Chapter 8: Statement Level Control Structures
Problem 1 Given n, calculate 2n
CSE 3302 Programming Languages
Presentation transcript:

Expressions and Statements

2 Contents Side effects: expressions and statements Expression notations Expression evaluation orders Conditional statements Loops Unstructured statements

3 Side Effects Expressions and statements represent the most fundamental computation mechanisms An expression, in its pure form, returns a value and produces no side effect A statement is mainly executed for its side effect and returns no value change variable values, do input or output

4 Expression Notations Operators can be written in infix, postfix, or prefix notation * * * 4 5 No operator precedence and associativity are required for postfix and prefix notations Lisp uses prefix notation Postscript and FORTH use postfix notation

5 Applicative Order Evaluation Applicative order (or strict) evaluation evaluates operands before operators (3 + 4) * (5 – 6) The evaluation order for operands is usually not specified (3 + 4) * (5 – 6)

6 An Example int x = 1; int f(void) { x += 1; return x; } int p( int a, int b) { return a + b; } main() { printf("%d\n",p( x, f() )); return 0; } f() produces side effect order of evaluation matters

7 Operators with side effects Sometimes expressions are explicitly constructed with side effects in mind C is an expression-oriented language. In C, assignment is an expression, not a statement x = y = z; In C, sequence operator is used to combine several expressions (with side effects) into a single expression x = (y += 1, x += y, x + 1);

8 Delayed Evaluation The evaluation of an expression can sometimes be performed without the evaluation of all its subexpressions. This is called delayed (or non- strict) evaluation Short-circuit evaluation of Boolean expressions true || x x || true false && x x && false if (i = x) … if (p != NULL && p->data == x) … if (x != 0 and y % x == 0) …

9 if and case Expressions An if-expression always evaluates the test- expression, but based on that value, only one of the then-expression or else- expression is ever evaluated e 1 ? e 2 : e 3 ; ML has case-expression: case e 1 of a => e 2 | b => e 3 | c => e 4 ;

10 Referential Transparency In the absence of side effects, the order of evaluation of subexpressions is immaterial to the final value of the expression Such expressions share an important property with mathematical expressions, called referential transparency Any two expressions in a program that have the same value may be substituted for each other anywhere in the program Variables would be unknown constants

11 Normal Order Evaluation Referential transparency allows for a very strong form of delayed evaluation to be used Normal order evaluation evaluates its operator before its operands and each operand is evaluated only it is needed for the calculation of the value of the operation

12 An Example int double(int x) { return x + x; } int square(int x) { return x * x; } square(double(2))  double(2) * double(2)  (2 + 2) * (2 + 2)  4 * (2 + 2)  4 * 4  16

13 Normal Order Evaluation Normal order evaluation might seem inefficient. It can be made efficient (2 + 2) * (2 + 2) Delayed evaluation does not need special syntax int if_exp(int x, int y, int z) { if (x) return y; else return z; }

14 Normal Order Evaluation The presence of side effects can substantially change the semantics of an expression int get_int(void) { int x; scanf(“%d”, &x); return x; } square(get_int()); get_int() * get_int();

15 If statements if-statement  if ( expression ) statement [ else statement ] if-statement  if expression then statement [ else statement ] end if ; if e1 then s1 elsif e2 then s2 elsif e3 then s3 end if; C Ada

16 Case-Statements switch (x - 1) { case 0: y = 0; z = 2; break; case 2: case 3: case 4: case 5: y = 3; z = 1; break; case 7: case 9: z = 10; break; default: break; }

17 Case-Statements case x - 1 is when 0 => y := 0; z := 2; when => y := 3; z := 1; when 7 | 9 => z := 10; when others => null; end case;

18 Guarded If Statements Guarded if statement: if B1 -> S1 | B2 -> S2 … | Bn -> Sn fi If one of the Bi’s evaluates to true, then Si is executed. If more than one of the Bi’s is true, then one and only one Si is selected for execution. If none of the Bi is true, then an error occurs Nondeterminism

19 Loop-Statements while (e) S do S while (e); for (e1; e2; e3) S;

20 Restrictions on Index Variable of For Loop The value of i cannot be changed within the body of the loop The value of i is undefined after the loop terminates i must be of restricted type and may not be declared in certain way

21 Questions about For Loop Are the bounds evaluated only once If the lower bound is greater than the upper bound, is the loop executed at all Is the value of the index variable undefined even if an exit or break statement is used to leave the loop before termination What translator checks are performed on loop structure

22 Guarded Loops Guarded do statement: do B1 -> S1 | B2 -> S2 … | Bn -> Sn od This statement is repeated until all the Bi’s are false. At each step, one of the true Bi’s is selected nondeterministically, and Si is executed

23 CLU Iterators numcount = proc (s: string) returns (int); count: int = 0; for c: char in stringchars(s) do if numeric (c) then count := count + 1; end; return (count); end numcount;

24 CLU Iterators stringchars = iter (s: string) yields (char); index: int := 1; limit: int := string$size(s); while index <= limit do yield (string$fetch(s, index)) index := index + 1; end; end stringchars;

25 The goto Controversy Bohm and Jacopini (1966) demonstrated that every possible control structure could be expressed using structured control constructs, i.e., gotos are unnecessary Dijkstra (1968) argued that the use of gotos was damaging in many ways Rubin (1987) argued that the use of gotos was justified in certain cases

26 Labeled Break in Java if (ok) { while (more) { … while (!found) { … if (disaster) goto 99; … } } 99: ok_break: if (ok) { while (more) { … while (!found) { … if (disaster) break ok_break; … } }