PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU.

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

Statement-Level Control Structures
ISBN Chapter 7 Expressions and Assignment Statements.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions.
ICE1341 Programming Languages Spring 2005 Lecture #12 Lecture #12 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
ISBN Chapter 8 Statement-Level Control Structures.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
Chapter 8 Statement-Level Control Structure. Introduction Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program statements.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 8 Statement-Level Control Structures.
CSE 452: Programming Languages Expressions and Control Flow.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Lecture 07 Expressions and Assignment Statements.
Expressions, Evaluation and Assignments
Expressions & Assignments One of the original intentions of computer programs was the evaluation of mathematical expressions –languages would inherit much.
Copyright © 1998 by Addison -Wesley Longman, Inc. 1 Chapter 6 Arithmetic Expressions - Their evaluation was one of the motivations for the development.
1-1 University of Hail College of Computer Science and Engineering Department of computer Science and Software Engineering Course: ICS313: Fundamentals.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Chapter 7 Expressions and Assignment Statements. Chapter 7 Topics 1-2 Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational.
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
Structure of Programming Language
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
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.
C H A P T E R S E V E N Expressions and Assignment Statements.
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
ISBN Chapter 8 Statement-Level Control Structures.
第八章 敘述層級的控制結構 (Statement-Level Control Structures)
Arithmetic Expressions
Expressions and Assignment Statements
Chapter 7 Expressions and Assignment Statements. Copyright © 2009 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7 © 1998 by Addison -Wesley Longman, Inc Arithmetic Expressions - Their evaluation was one of the motivations for the development of the.
8-1 Statement-Level Control Structures Introduction Selection Statements Iterative Statements Unconditional Branching Guarded Commands Conclusions.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
ISBN Chapter 7 Expressions and Assignment Statements.
April 16, ICE 1341 – Programming Languages (Lecture #14) In-Young Ko Programming Languages (ICE 1341) Lecture #14 Programming Languages (ICE 1341)
1 Iterative Statements Repeated execution of a (compound) statement by iteration or recursion –Iteration is statement level –Recursion is unit-level control.
March 31, ICE 1341 – Programming Languages (Lecture #11) In-Young Ko Programming Languages (ICE 1341) Lecture #11 Programming Languages (ICE 1341)
Chapter Seven: Expressions and Assignment Statements Lesson 07.
Chapter 8 © 2002 by Addison Wesley Longman, Inc Introduction - Levels of Control Flow: 1. Within expressions 2. Among program units 3. Among program.
W E E K F I V E Control Flow. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements Iterative Statements.
1 CS Programming Languages Class 10 September 26, 2000.
ISBN Chapter 7 Expressions and Assignment Statements.
7-1/27 Chapter 7 Expressions and Assignment Statements Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean.
Expressions and Assignment Statements
Expressions and Assignment Statements
7.2 Arithmetic Expressions
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
8.1 Introduction - Levels of Control Flow: 1. Within expressions
Dr. Vamsi Paruchuri University of Central Arkansas
Chapter 7: Expressions and Assignment Statements
Expressions and Assignment Statements
Expressions and Assignment Statements
Arithmetic Expressions
Expressions and Assignment Statements
Expressions and Assignment Statements
College of Computer Science and Engineering
Expressions and Assignment Statements
Expression and Asignment Statements
Chapter 7 Expressions and Assignment Statements.
Chapter8: Statement-Level Control Structures April 9, 2019
Chapter 7: Expressions and Assignment Statements Sangho Ha
PRESENTED BY ADNAN M. UZAIR NOMAN
Expressions and Assignment Statements
Expressions and Assignment Statements
Presentation transcript:

PLLab, NTHU,Cs2403 Programming Languages Expression and control structure Kun-Yuan Hsieh Programming Language Lab., NTHU

PLLab, NTHU,Cs2403 Programming Languages Expressions Expressions are the fundamental means of specifying computations in a programming language Expression evaluation depends on the orders of operator and operand evaluation Issues: –Precedence –Associativity –Order of operand evaluation –Side effects –…

PLLab, NTHU,Cs2403 Programming Languages Operators A unary operator has one operand e.g. ++ A binary operator has two operands e.g. +, -, *, / A ternary operator has three operands e.g. ?:

PLLab, NTHU,Cs2403 Programming Languages Operator precedence Precedence rules define the order in which “ adjacent ” operators of different precedence levels are evaluated. Ex: a = 10; b = 3; c = 4; d = a + b * c; d (in C) =22

PLLab, NTHU,Cs2403 Programming Languages Operator associativity Associativity rules define the order in which adjacent operators with the same precedence level are evaluated Associativity in most PL is left-to-right, except for exponentiation op (right-to-left) Ex: a = 10; b = 3; c = 4; d = a – b + c; d (in C) = 7

PLLab, NTHU,Cs2403 Programming Languages Parentheses Precedence and associativity rules can be overriden with parentheses a = 10; b = 3; c = 4; d = (a + b) * c; d (in C) = 52 Use parentheses when not clear in your code to increase readability Ex: a = 10; b = 3; c = 4; d (in C) = a**b**c = a**(b**c) = 10**81

PLLab, NTHU,Cs2403 Programming Languages Functional side effects Side effect of a function occurs when a function changes one of its parameter or global variable Ex: int a = 5; int fun1() { a = 17; return 3; } int fun2(){ a = a + fun1(); } void main() { fun2(); } a (in C) = 8

PLLab, NTHU,Cs2403 Programming Languages Operator overloading Use of an operator for more than one purpose Increase readability but also decrease readability EX: –Matrix operation: A * B + C, instead of MatrixAdd( MatrixMult( A, B ), C ) –A + B is actually implemented as (A AND B)

PLLab, NTHU,Cs2403 Programming Languages Type Conversions Narrowing conversion converts an object to a type that cannot include all of the values of the original type e.g., float to int Widening conversion convets an object to a type that can include at least approximations to all of the values of the original type e.g., int to float Coercion is an implicit type conversion

PLLab, NTHU,Cs2403 Programming Languages Relational and Boolean Expressions Compares the values of its operands Evaluate to some Boolean representation C has no Boolean type--it uses int type with 0 for false and nonzero for true Ex: a = 3, b = 4, c = a + 1 > 2 * b c = 0;

PLLab, NTHU,Cs2403 Programming Languages Short Circuit Evaluation The result of an expression is determined without evaluating all the operands and/or operators EX: while ((index < length) && (list[index] != key)) { Index = index + 1; } Table lookup

PLLab, NTHU,Cs2403 Programming Languages Assignment Statements The operator symbol: –= FORTRAN, BASIC, PL/I, C, C++, Java –:= ALGOLs, Pascal, Ada Unary assignment ops –++count; (count = count + 1;) Undetected error in C/C++ –if (x == y) is different from if (x = y) … –Java only allows Boolean expressions in if statements

PLLab, NTHU,Cs2403 Programming Languages Control structure Control structure is a control statement and the statements whose execution it controls Selection statement provides the means of choosing between two or more paths of execution –Two-Way Selection Statements –Multi-Way Selection Statements

PLLab, NTHU,Cs2403 Programming Languages Two-Way Selection Statements if-then-else –matching else problem rules: else matches the most recent then –used in Java and most other imperative PL (C, Pascal) ALGOL 60: use compound statement for nesting if Ada: use “end if” reserve word

PLLab, NTHU,Cs2403 Programming Languages Multiple selection constructs FORTRAN: IF (exp) N1, N2, N3 FORTRAN: GO TO (lb1, lb2, …, lbN), exp Pascal: case (exp) of … end C, C++: switch (index) { case … break… default …}

PLLab, NTHU,Cs2403 Programming Languages Iterative Statements repeated execution of a statement or compound statement is accomplished either by iteration or recursion Counter-Controlled Loops Design Issues: 1. What are the type and scope of the loop variable? 2. What is the value of the loop variable at loop termination? 3. 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? 4. Should the loop parameters be evaluated only once, or once for every iteration?

PLLab, NTHU,Cs2403 Programming Languages examples Iterative statements –FORTRAN 90: DO lavel var = init, final [,step] –ALGOL 60: for count := 1 step 1 until 10 do for index := 1, 4, 13, 41 step 2 until 47, 3 * index while index < 1000, 34, 2, -24, do  1, 4,13, 41,43, 45, 47, 147, 441, 34, 2, -24 –Pascal: for var := init (to | downto) final do –Ada: for k in loop –C, C++, Java: for (k = 0; k <= 10; k++) {…} Iteration on data list = {“Bob”, “John”, “Tom”}; foreach $name { print $name; }

PLLab, NTHU,Cs2403 Programming Languages Unconditional Branching Problem: readability Some languages do not have them: e.g., Java Loop exit statements are restricted and somewhat camouflaged goto ’ s

PLLab, NTHU,Cs2403 Programming Languages Guarded statements Guarded commands –suggested by Dijkstra (1975) –all boolean exps are evaluated –one of the true-stmts is nondeterministically chose for execution –if all exps are false, run-time error occurs if i = 0 -> sum := sum + i; [ ] i > j -> sum := sum + j; [ ] i sum := sum + i; fi

PLLab, NTHU,Cs2403 Programming Languages Guarded statements do -> [ ] -> [ ] … [ ] -> od –all boolean exps are evaluated on each iteration –one of the true stmts are nondeterministically executed, and then the loop is evaluated again –when all exps are false, the loop terminates sort q1 to q4 such that q1 <= q2 <= q3 <= q4 do q1 > q2 -> temp := q1; q1 := q2; q2 := temp; [ ] q2 > q3 -> temp := q2; q2 := q3; q3 := temp; [ ] q3 > q4 -> temp := q3; q3 := q4; q4 := temp; od