Structure of Programming Language

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.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 7 Expressions and Assignment Statements.
ISBN Chapter 8 Statement-Level Control Structures.
ISBN Chapter 8 Statement-Level Control Structures.
Statement-Level Control Structures Sections 1-4
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.
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.
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.
Arithmetic Expressions
Chapter 8: Statement-Level Control Structures
Expressions and Assignment Statements
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.
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)
Chapter Seven: Expressions and Assignment Statements Lesson 07.
W E E K F I V E Statement-Level Control Structures.
Structure of Programming Language Statements. Expression.
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.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection 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
Def: A control structure is a control statement and
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
Statement-Level Control Structures
Expressions and Assignment Statements
Chapter 8: Control Structures
Expressions and Assignment Statements
Arithmetic Expressions
Statement-Level Control Structures
Expressions and Assignment Statements
Expressions and Assignment Statements
Control Structures In Text: Chapter 8.
College of Computer Science and Engineering
Expressions and Assignment Statements
Statement-Level Control Structures
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:

Structure of Programming Language Statements

Statements Expression

An expression is a formal description of a value. What is an expression ? The notion of value is central to programming. Program variables get instantiated to values at run-time. Integer variables to integer values String variables to array of characters etc. With this perspective, we could define an expression simply as: An expression is a formal description of a value.

Expression Examples 2 2 * 5 F(4) + 2*5 // Need to define function F A < B A < B \/ C = D // A,B,C,D are variables P(A, B) \/ Q(C, D) // P,Q are predicates

Prefix, Infix, Postfix Notation Position of Function Examples Prefix Left of argument(s) sqrt(16), f(3,4) Infix Between two arguments 3 f 4, 3 + 4 Postfix Right of arguments 16 sqrt, 3 4 f

Postfix evaluation - Example Expression Code Stack Contents 3 5 + 8 6 - * push 3 <3> ^ push 5 <3,5> add <8> 3 5 + 8 6 - * push 8 <8,8> ^ push 6 <8,8,6> sub <8, 2> 3 5 + 8 6 - * mul <16> ^

Arithmetic Expressions: Design Issues Design issues for arithmetic expressions Operator precedence rules? Operator associativity rules? Order of operand evaluation? Operand evaluation side effects? Operator overloading? Type mixing in expressions?

Operator Precedence C, C++, and Java have over 50 operators and 17 different levels of precedence Pascal: not, unary - *, /, div, mod, +, - Ada: ** *, /, mod, rem unary -, not +, -, & and, or, xor

Arithmetic Expressions: Operator Associativity Rule The operator associativity rules for expression evaluation define the order in which adjacent operators with the same precedence level are evaluated Typical associativity rules Left to right, except **, which is right to left Sometimes unary operators associate right to left (e.g., in FORTRAN) APL is different; all operators have equal precedence and all operators associate right to left

Relational Expressions - Use relational operators and operands of various types - Evaluate to some boolean representation - Operator symbols used vary somewhat among languages (!=, /=, .NE., <>, #)

Boolean Expressions - Operands are boolean and the result is boolean - Operators: FORTRAN 77 FORTRAN 90 C Ada .AND. and && and .OR. or || or .NOT. not ! not xor - C has no boolean type--it uses int type with 0 for false and nonzero for true -

Relational and Boolean Expressions: No Boolean Type in C C has no Boolean type--it uses int type with 0 for false and nonzero for true One odd characteristic of C’s expressions: a < b < c is a legal expression, but the result is not what you might expect: Left operator is evaluated, producing 0 or 1 The evaluation result is then compared with the third operand (i.e., c)

Short Circuit Evaluation Evaluating an expression without evaluating all the operands. e.g. (a > b) and (c > 5) If we know that a > b is false, then there is no need To determine whether (c > 5) is true.

Short Circuit Evaluation Pascal: does not use short-circuit evaluation index := 1; while (index <= length) and (LIST[index] <> value) do index := index + 1 If value is not in LIST, then ???

Short circuit evaluation C, C++, and Java: use short-circuit evaluation for the usual Boolean operators (&& and ||), but also provide bitwise Boolean operators that are not short circuit (& and |) Ada: programmer can specify either (short-circuit is specified with and then and or else) FORTRAN 77: short circuit, but any side-affected place must be set to undefined Short-circuit evaluation exposes the potential problem of side effects in expressions e.g. (a > b) || (b++ / 3)

Conditional Expressions C-based languages (e.g., C, C++) An example: average = (count == 0)? 0 : sum / count Evaluates as if written like if (count == 0) average = 0 else average = sum /count

Let expressions Example: let square(x) = x*x in square(square(2)) Of the form: let function_definition in sub_expression The function definition defines a function f in equational form. The sub-expression contains function applications of f We assume that definition of f is non-recursive.

Let expressions Evaluation proceeds by replacing applications of f in sub-expression with the definition of f Example: let square(x) = x*x in square(square(2)) square(2) * square(2) 2 * 2 * 2 * 2 = 16 Let expressions allow for function definitions. Their evaluation is same as macro-expansion.

Type Conversions A narrowing conversion is one that converts an object to a type that cannot include all of the values of the original type e.g., float to int A widening conversion is one in which an object is converted to a type that can include at least approximations to all of the values of the original type e.g., int to float

Type Conversions: Mixed Mode A mixed-mode expression is one that has operands of different types A coercion is an implicit type conversion Disadvantage of coercions: They decrease in the type error detection ability of the compiler In most languages, all numeric types are coerced in expressions, using widening conversions In Ada, there are virtually no coercions in expressions

Explicit Type Conversions Called casting in C-based languages Examples C: (int)angle Ada: Float (Sum) Note that Ada’s syntax is similar to that of function calls

Statement Assignment statement

Assignment Statements The general syntax <target_var> <assign_operator> <expression> The assignment operator = FORTRAN, BASIC, PL/I, C, C++, Java := ALGOLs, Pascal, Ada

Assignment Statements: Compound Operators A shorthand method of specifying a commonly needed form of assignment Introduced in ALGOL; adopted by C Example a = a + b is written as a += b

Mixed-Mode Assignment Assignment statements can also be mixed-mode, for example int a, b; float c; c = a / b; In Pascal, integer variables can be assigned to real variables, but real variables cannot be assigned to integers In Java, only widening assignment coercions are done In Ada, there is no assignment coercion

Combination Assignment Operators (cont’d) Syntactic sugar for often used combinations. Useful in combination with autoincrement operators: A[--i]=b; equivalent to A[i -= 1] = b;

Combination Assignment Operators (cont’d) *p++ = *q++; /* ++ has higher precedence than * */ equivalent to *(t=p, p += 1, t) = *(t=q, q += 1, t); Advantage of autoincrement operators: Increment is done in units of the (user-defined) type.

Comma Operator In C, merely a sequence: a,b = 6; /* now a=2 and b=6 */ int a=2, b=3; a,b = 6; /* now a=2 and b=6 */ a,b = 7,6; /* now a=2 and b=7 */ /* = has higher precedence than , */

Statement Selection

Selection Statements A selection statement provides the means of choosing between two or more paths of execution Two general categories: Two-way selectors Multiple-way selectors

Two-Way Selection Statements General form: if control_expression then clause else clause Design Issues: In C, Python, and C++, the control expression can be arithmetic In languages such as Ada, Java, Ruby, and C#, the control expression must be Boolean

Two-Way Selection: Examples FORTRAN: IF (boolean_expr) statement Problem: can select only a single statement; to select more, a GOTO must be used, as in the following example IF (.NOT. condition) GOTO 20 ... 20 CONTINUE This problem was solved in FORTRAN 77

Two-Way Selection: Examples ALGOL 60: if (boolean_expr) then statement (then clause) else statement (else clause) The statements could be single or compound

Nesting Selectors Java example if (sum == 0) Which if gets the else? if (count == 0) result = 0; else result = 1; Which if gets the else? Java's static semantics rule: else matches with the nearest if

Nesting Selectors (continued) To force an alternative semantics, compound statements may be used: if (sum == 0) { if (count == 0) result = 0; } else result = 1; The above solution is used in C, C++, and C# Perl requires that all then and else clauses to be compound

Multiple-Way Selection Early multiple selectors: FORTRAN arithmetic IF (a three-way selector) IF (arithmetic expression) N1, N2, N3 Segments require GOTOs

Multiple-Way Selection Modern multiple selectors C’s switch statement switch (expression) { case const_expr_1: stmt_1; … case const_expr_n: stmt_n; [default: stmt_n+1] }

Switch in C, C++, Jave switch (x) default: if (prime(x)) case 2: case 3: case 5: case 7: process_prime(x); else case 4: case 6: case 8: case 9: case 10: process_composite(x); 38 38

Multiple-Way Selection in C# It has a static semantics rule that disallows the implicit execution of more than one segment Each selectable segment must end with an unconditional branch (goto or break) The control expression and the case constants can be strings switch (value) { case -1: Negatives++; break; case 0: Zeros++; goto case 1; case 1: Positives++; break; default: Console.WriteLine(“!!!\n”); } 39 39

Case/Switch Statements (cont’d) C is different in other respects: A label can have an empty arm, Control "falls" through to next label. Effectively allows lists of values. Example: switch (grade) { case 10: case 9: case 8: case 7: printf("Pass"); break; default: printf("Fail"); break; }

Multiple-Way Selection: Examples Design choices for C’s switch statement Control expression can be only an integer type Selectable segments can be statement sequences, blocks, or compound statements default clause is for unrepresented values (if there is no default, the whole statement does nothing)

The Ada case statement case Next_Char is when ‘I’ => Val := 1; when ‘V’ => Val := 5; when ‘X’ => Val := 10; when ‘C’ => Val := 100; when ‘D’ => Val := 500; when ‘M’ => Val := 1000; when others => raise Illegal_Numeral; end case;

Statement Iterative

Iterative Statements The repeated execution of a statement or compound statement is accomplished either by iteration or recursion

Counter-Controlled Loops A counting iterative statement has a loop variable, and a means of specifying the initial and terminal, and stepsize values Design Issues: What are the type and scope of the loop variable? What is the value of the loop variable at loop termination? 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? Should the loop parameters be evaluated only once, or once for every iteration?

Iterative Statements: Examples FORTRAN 90 syntax DO label var = start, finish [, stepsize] Stepsize can be any value but zero Design choices: 1. Loop variable must be INTEGER 3. The loop variable cannot be changed in the loop; because they are evaluated only once, it does not affect loop control

Iterative Statements Pascal’s for statement for variable := initial (to|downto) final do statement Design choices: Loop variable must be an ordinal type of usual scope After normal termination, loop variable is undefined The loop variable cannot be changed in the loop but they are evaluated just once, so it does not affect loop control

Iterative Statements: Examples Ada for var in [reverse] discrete_range loop ... end loop A discrete range is a sub-range of an integer or enumeration type Scope of the loop variable is the range of the loop Loop variable is implicitly undeclared after loop termination

Iterative Statements: Examples C’s for statement for ([expr_1] ; [expr_2] ; [expr_3]) statement The expressions can be whole statements, or even statement sequences, with the statements separated by commas The value of a multiple-statement expression is the value of the last statement in the expression Everything can be changed in the loop The first expression is evaluated once, but the other two are evaluated with each iteration

Iterative Statements: Examples C++ differs from C in two ways: The initial expression can include variable definitions (scope is from the definition to the end of the loop body) Java and C# Differs from C++ in that the control expression must be Boolean

Iterative Statements: Logically-Controlled Loops Repetition control is based on a Boolean Design issues: Pre-test or post-test? Should the logically controlled loop be a special case of the counting loop statement ? General forms: while (ctrl_expr) do loop body loop body while (ctrl_expr)

Iterative Statements: Logically-Controlled Loops: Examples Pascal has separate pre-test and post-test logical loop statements (while-do and repeat-until) C and C++ also have both, but the control expression for the post-test version is treated just like in the pre-test case (while-do and do- while) Java is like C, except the control expression must be Boolean (and the body can only be entered at the beginning -- Java has no goto

Iterative Statements: Logically-Controlled Loops: Examples Ada has a pretest version, but no post-test FORTRAN 77 and 90 have neither Perl has two pre-test logical loops, while and until, but no post-test logical loop

Iterative Statements: User-Located Loop Control Mechanisms break and continue C , C++, Java, Python, Ruby, C# : break statement Unconditional; for any loop or switch; one level only Java and C# have a labeled break statement: control transfers to the label An alternative: continue statement; it skips the remainder of this iteration, but does not exit the loop

Unconditional Branching Transfers execution control to a specified place in the program Represented one of the most heated debates in 1960’s and 1970’s Well-known mechanism: goto statement Major concern: Readability Some languages do not support goto statement (e.g., Module-2 and Java) C# offers goto statement (can be used in switch statements)