UNITII EXPRESSIONS AND ASSIGNMENT STATEMENTS AND STATEMENT LEVEL

  • Slides: 45
Download presentation
UNIT-II EXPRESSIONS AND ASSIGNMENT STATEMENTS AND STATEMENT- LEVEL CONTROL STRUCUTERS By Mr. T. M.

UNIT-II EXPRESSIONS AND ASSIGNMENT STATEMENTS AND STATEMENT- LEVEL CONTROL STRUCUTERS By Mr. T. M. Jaya Krishna M. Tech

t n e m n g i s s A d n a s

t n e m n g i s s A d n a s n o i s s e r s Exp t n e Statem

Arithmetic expressions • Consists = operands & operators – Unary – Binary • r

Arithmetic expressions • Consists = operands & operators – Unary – Binary • r infix (almost all prog. Lang. ’s) • except Perl (prefix) – Ternary • Purpose – Arithmetic computation do this we need • Fetching operands • Executing Expressions and Assignment Statements:

Arithmetic expressions Design Issues What are the operator precedence rules? What are the operator

Arithmetic expressions Design Issues What are the operator precedence rules? What are the operator associativity rules? What is the order of operand evaluation? Are there restrictions on operand evaluation side effects? • Does the language allow user-defined operator overloading? • What type mixing is allowed in expressions? • • Expressions and Assignment Statements:

Arithmetic expressions Operator Evaluation Order • Operator precedence & associativity rules – dictate •

Arithmetic expressions Operator Evaluation Order • Operator precedence & associativity rules – dictate • Order = evaluation = operators • Precedences = arithmetic operators = Ruby & C-based lang. ’s: Note: ** exponentiation Expressions and Assignment Statements:

Arithmetic expressions Associativity • expression contains 2 adjacent occurrences of operators w same level

Arithmetic expressions Associativity • expression contains 2 adjacent occurrences of operators w same level = precedence • Question? – which operator - - evaluated 1 st • associativity rules of the language (answer) • Operator can have either – left associativity (most common lang. ’s except **) | right associativity • Left operator evaluated 1 st | right Expressions and Assignment Statements:

Arithmetic expressions Operand Evaluation Order • Parentheses: • Operand Evaluation Order: – alter the

Arithmetic expressions Operand Evaluation Order • Parentheses: • Operand Evaluation Order: – alter the precedence & associativity rules by placing parentheses in expressions. • parenthesized part = expression has precedence over its adjacent unparenthesized parts – Variables in expressions r evaluated by fetching their values memory. – Constants r sometimes evaluated the same way. • In other cases, a constant may be part = machine lang. instruction and not require a memory fetch. – If an operand is a parenthesized expression, all = operators it contains must be evaluated before its value can be used as an operand. Expressions and Assignment Statements:

Arithmetic expressions Operand Evaluation Order – side effects • If neither = operands =

Arithmetic expressions Operand Evaluation Order – side effects • If neither = operands = an operator has side effects – then operand evaluation order - - irrelevant • the only interesting case arises wn? the evaluation = an operand does have side effects. • Consider the expression: – a + fun(a) – fun doesn’t have side effect = changing a » Order = evaluation = 2 operands has effect – Otherwise Executing 1 and 2 » effect will result in a = 10 & • Example: 1 b = 20 • a = 10; 2 Executing 2 , 1 and 2 • b = a + fun(a); Expressions and Assignment Statements: will result in b = 20 & a = 10 b = 30

Arithmetic expressions Operand Evaluation Order – side effects • 2 possible solutions problem =

Arithmetic expressions Operand Evaluation Order – side effects • 2 possible solutions problem = operand evaluation order & side effects – language designer could disallow functional side effects. • Difficult in imperative lang. ’s – language definition could state that operands in expressions r evaluated in a particular order • demand that implementers guarantee that order • Problem w strict evaluation order - – code optimization techniques used by compilers involve reordering operand evaluations Expressions and Assignment Statements:

Arithmetic expressions Operand Evaluation Order Referential Transparency & Side Effects • It - -

Arithmetic expressions Operand Evaluation Order Referential Transparency & Side Effects • It - - related & affected by functional side effects • Referential transparency: – if any 2 expressions in the prog. that have same value can be substituted for 1 another anywhere in the prog. , without affecting the action = prog. – Value = it depends entirely – function parameters result 1 = (fun(a) + b) / (fun(a) - c); temp = fun(a); result 2 = (temp + b) / (temp - c); Expressions and Assignment Statements: fun() has no side effects result 1 == result 2

Overloaded Operators • Arithmetic operators c used multiple purposes – E. g. operator +

Overloaded Operators • Arithmetic operators c used multiple purposes – E. g. operator + - - used integer addition, floating addition etc. . – Problem w overloaded operators • Consider & (ampersand i. e. address-of operator) • E. g. execution = the stmt – X = &Y (2 problems) 1. Using same symbol 2 completely unrelated operations - harm readability 2. leaving out the 1 st operand a bitwise AND operation can go undetected by the compiler » Reason • interpreted as an address-of operator Expressions and Assignment Statements:

Overloaded Operators • Lang. ’s (C++, C#, & F#) that support abstract data types

Overloaded Operators • Lang. ’s (C++, C#, & F#) that support abstract data types – Allow programmer further overload operator symbols. • Wn? sensibly used – user-defined operator overloading can aid readability. • On the other hand – It can be harmful to readability Expressions and Assignment Statements:

Type Conversions • These r either narrowing | widening – Safe • result in

Type Conversions • These r either narrowing | widening – Safe • result in reduced accuracy • Type conversions c either – Explicit | Implicit Expressions and Assignment Statements:

Type Conversions • These r either narrowing | widening – Safe • result in

Type Conversions • These r either narrowing | widening – Safe • result in reduced accuracy • Type conversions – Explicit • ML, F# – Implicit • java – Errors in expressions: • If lang. requires type checking then operand type errors occur. Expressions and Assignment Statements:

Relational and Boolean Expressions Relational Expressions • Relational Operator (RO) – Compares values =

Relational and Boolean Expressions Relational Expressions • Relational Operator (RO) – Compares values = 2 operands • Relational Expression (RE) – 2 operands & 1 RO • Value = RE - - Boolean • Syntax = relational operator – Inequality • • • C based lang. ’s != Ada /= Lua ~= Fortran 95+. NE. | <> ML & F# <> Expressions and Assignment Statements:

Relational and Boolean Expressions • Consists = – Boolean variables – Boolean constants –

Relational and Boolean Expressions • Consists = – Boolean variables – Boolean constants – Relational expressions & – Boolean operators • In mathematics = Boolean algebra equal precedence – OR & AND • Equal precedence – ADA • High precedence (AND) –C Expressions and Assignment Statements:

Relational and Boolean Expressions Precedence of arithmetic, relational, & Boolean operators in the C-based

Relational and Boolean Expressions Precedence of arithmetic, relational, & Boolean operators in the C-based languages Expressions and Assignment Statements:

Relational and Boolean Expressions • Versions = C prior C 99 r odd among

Relational and Boolean Expressions • Versions = C prior C 99 r odd among the popular imperative lang. ’s – Boolean type & thus Boolean values. • One odd result = C’s design = relational expression – a > b > c (legal) • reason – RO = C r left associative producing either 0 | 1 » Result > C (b > C) Expressions and Assignment Statements:

Short-Circuit Evaluation • In this – Result - - determined w out evaluating all

Short-Circuit Evaluation • In this – Result - - determined w out evaluating all = Short Circuit operands &/| operators. Prog. Lang. ’s Evaluation Operator – Example: Ada and then & or else • (13 * a) * (b / 13 - 1) C – Consider a = 0 then Ruby, Perl, ML, » (b / 13 - 1) F# & Python • Necessary Evaluate ? • (a >= 0) && (b < 10) – Consider a < 0 then » b < 10 (evaluate? ) Expressions and Assignment Statements: AND, OR, && and || All logical operators

Assignment Statements 1. Simple Assignments: – ALGOL 60 (: =) – Ada (: =)

Assignment Statements 1. Simple Assignments: – ALGOL 60 (: =) – Ada (: =) 2. Conditional Targets: – Perl allows • Example: – ($flag ? $count 1 : $count 2) = 0; ≡ if ($flag) { $count 1 = 0; } else { $count 2 = 0; } Expressions and Assignment Statements:

Assignment Statements 3. Compound Assignment Operators: – Destination variable appeared on R. S •

Assignment Statements 3. Compound Assignment Operators: – Destination variable appeared on R. S • Example: – a=a+b – Introduced by • ALGOL 68 – Later Adopted by • C • Perl, Java. Script, Python, & Ruby. Expressions and Assignment Statements:

Assignment Statements 4. Unary Assignment Operators: – C based lang. ’s • Perl &

Assignment Statements 4. Unary Assignment Operators: – C based lang. ’s • Perl & Java. Script +de – 2 unary arithmetic operators (assignments) » Combine increment & decrement operations w assignment 5. Assignment as an Expression: – Example: • while ((ch = getchar()) != EOF) {. . . } Expressions and Assignment Statements:

Assignment Statements 6. Multiple Assignments: – Perl, Ruby, Lua • Multiple-target & multiple-source assignment

Assignment Statements 6. Multiple Assignments: – Perl, Ruby, Lua • Multiple-target & multiple-source assignment stmts. – Perl • ($first, $second, $third) = (20, 40, 60); – Semantics » 20 - - assigned $first » 40 - - assigned $second » 60 - - assigned $third Expressions and Assignment Statements:

Mixed-Mode Assignment • Design issue: – Does the type = expr have same as

Mixed-Mode Assignment • Design issue: – Does the type = expr have same as type = variable being assigned. – Fortran, C, C++ & Perl use coercion rules mixed mode assignment – Ada allow it – C++, Java & C# allow Expressions and Assignment Statements:

m e t a St l e v e L t en S l

m e t a St l e v e L t en S l o r t n o C s e r u t c u r t

Selection Statements • Choosing between 2 | more execution paths in a prog. •

Selection Statements • Choosing between 2 | more execution paths in a prog. • Two categories: – Two-way selection stmt’s • General = two-way selector: if control_expression then clause else clause – N-way | Multiple selection stmt’s Statement-Level Control Structures

Selection Statements Design Issues – Two Way Selection Statements • Design issues: – W?

Selection Statements Design Issues – Two Way Selection Statements • Design issues: – W? - - the form & type = expression that controls the selection? – H? r then & else clauses specified? – H? should the meaning = nested selectors be specified? • The Control Expressions (CE): – Specified in parentheses (then reserved word) – Less need = parentheses (used otherwise) • C 89 (Boolean data type) – Arithmetic expressions (used as CE) • C 99, Python & C++ – either Boolean | arithmetic expressions (used as CE) • Other contemporary lang. ’s – only Boolean expressions (used as CE) Statement-Level Control Structures

Selection Statements Design Issues – Two Way Selection Statements • Clause Form: – then

Selection Statements Design Issues – Two Way Selection Statements • Clause Form: – then & else clauses appear as • single statements | compound statements • One variation = this - - Perl – Where all m compound stmts • Python (use) – Indentation if x > y : x=y print "case 1 • Nesting Selectors • Issue: if (sum == 0) if (count == 0) result = 0; else result = 1; • Interpreted? – else clause matched w 1 st then | 2 nd then clause Statement-Level Control Structures

Selection Statements Design Issues – Two Way Selection Statements • Clause Form: – then

Selection Statements Design Issues – Two Way Selection Statements • Clause Form: – then & else clauses appear as • single statements | compound statements • One variation = this - - Perl – Where all m compound stmts • Python (use) – Indentation if x > y : x=y print "case 1 • Nesting Selectors • Issue: if (sum == 0) if (count == 0) result = 0; else result = 1; • Interpreted? – else clause matched w 1 st then | 2 nd then clause Statement-Level Control Structures

Selection Statements Design Issues – Two Way Selection Statements • Selector Expressions: – ML,

Selection Statements Design Issues – Two Way Selection Statements • Selector Expressions: – ML, F#, and LISP (functional lang. ’s) • selector - - statement ↔ - - expression that results in a value • Example (F#): let y = if x > 0 then x else 2 * x; ; Statement-Level Control Structures

Selection Statements Design Issues – Multiple Selection Statements • allows the selection = 1

Selection Statements Design Issues – Multiple Selection Statements • allows the selection = 1 = any number = statements or statement groups. • Design Issues: – W? - - the form & type = expression that controls the selection? – H? selectable segments specified? – Is execution flow thru the structure restricted +de a single selectable segment? – H? case values specified? – H? should unrepresented selector expression values be handled? Statement-Level Control Structures

Selection Statements Design Issues – Multiple Selection Statements • Example: multiple selectors • Example:

Selection Statements Design Issues – Multiple Selection Statements • Example: multiple selectors • Example: – C multiple-selector (switch) stmt switch (index) { • It - - also part = C++, Java & Java. Script • general form - - switch (expression) { case constant_expression 1: statement 1; . . . case constantn: statement_n; [default: statementn+1] } } Statement-Level Control Structures case 1: case 3: odd += 1; sumodd += index; case 2: case 4: even += 1; sumeven += index; default: printf("Error in switch, index = %dn", index);

Selection Statements Design Issues – Multiple Selection Statements • break (switch uses) – Restrict

Selection Statements Design Issues – Multiple Selection Statements • break (switch uses) – Restrict each execution single selectable segment – Example: switch (index) { case 1: case 3: odd += 1; sumodd += index; break; case 2: case 4: even += 1; sumeven += index; break; default: printf("Error in switch, index = %dn", index); } Statement-Level Control Structures

Selection Statements Design Issues – Multiple Selection Statements • Multiple Selection Using if –

Selection Statements Design Issues – Multiple Selection Statements • Multiple Selection Using if – switch | case • Inadequate (most situations) • Example: – When selection m made on the basis = Boolean expression » Nested 2 -way selectors (used) Statement-Level Control Structures

Selection Statements Design Issues – Multiple Selection Statements • Implementing Multiple Selection Structures –

Selection Statements Design Issues – Multiple Selection Statements • Implementing Multiple Selection Structures – Multiple selection stmt - - n-way branch segments = code • n - - no. = selectable segments – Implemented? » Conditional branch instructions (multiple) » Example: switch (expression) { case constant_expression 1: statement 1; break; . . . case constantn: statementn; break; [default: statementn+1] } Statement-Level Control Structures

Iterative Statements • Causes a stmt | collection = stmt. ’s – Execute 0

Iterative Statements • Causes a stmt | collection = stmt. ’s – Execute 0 | 1 | more times – Loop (another name) • Plankalkül (Procedural Lang. ) – Designed by Konrad Zuse • Categories: – based on questions: » H? - - iteration controlled? • Primary possibilities r: • Logical • Counting | • Combination = both » Where should control mechanism appear in loop stmt? • top of the loop or the bottom of the loop Statement-Level Control Structures

Iterative Statements Categories • Counter-Controlled Loops: – Has variable (loop variable) • Count -

Iterative Statements Categories • Counter-Controlled Loops: – Has variable (loop variable) • Count - - maintained • Also +des – Initial & terminal values & – Stepsize – Design Issues: • type and scope = loop variable? • loop variable or loop parameters changed in loop (legal? ), – If so, does the change affect loop control? • loop parameters be evaluated only once, or once for every iteration? Statement-Level Control Structures

Iterative Statements Categories – Ada for Stmt: for variable in [reverse] discrete_range loop. .

Iterative Statements Categories – Ada for Stmt: for variable in [reverse] discrete_range loop. . . end loop; – It - - a subrange = integer | enumeration type » E. g. 1. . 10 | Monday. . Friday Statement-Level Control Structures

Iterative Statements Categories – C based for stmt: for (expression_1; expression_2; expression_3) loop body

Iterative Statements Categories – C based for stmt: for (expression_1; expression_2; expression_3) loop body – single | compound | null stmt • Expression_1 – Initialization stmt • Expression_2 – Control stmt • Expression_3 – Increment | decrement operational semantics description count 1 = 0 count 2 = 1. 0 loop: if count 1 > 10 goto out if count 2 > 100. 0 goto out count 1 = count 1 + 1 sum = count 1 + count 2 = count 2 * 2. 5 goto loop out: … • • operational semantics description expression_1 loop: if expression_2 = 0 goto out [loop body] expression_3 goto loop out: . . . for (count 1 = 0, count 2 = 1. 0; count 1 <= 10 && count 2 <= 100. 0; sum = ++count 1 + count 2, count 2 *= 2. 5); Statement-Level Control Structures

Iterative Statements Categories • Logically Loops: Controlled • Example: – Pretest – Collection =

Iterative Statements Categories • Logically Loops: Controlled • Example: – Pretest – Collection = stmt. ’s m repeatedly executed • Repetition control - - based on Boolean expression ↔ counter – Design Issues: • Should while (control_expression) loop body – Posttest do loop body while (control_expression); – control be pretest or posttest? – It be a special form = counting loop |separate stmt? Statement-Level Control Structures

Iterative Statements Categories • User-Located Loop Control Mechanisms: – Design issues: • Should –

Iterative Statements Categories • User-Located Loop Control Mechanisms: – Design issues: • Should – conditional mechanism be an integral part = exit? – only one loop body be exited | can enclosing loops also be exited? – C, C++, Python, Ruby, and C# » unconditional unlabeled exits (break) – Java & Perl » unconditional labeled exits (break in Java, last in Perl) – C, C++, and Python » unlabeled control statement (continue) Statement-Level Control Structures

Unconditional Branching – transfers execution control – Java, Python & Ruby (goto) – C#

Unconditional Branching – transfers execution control – Java, Python & Ruby (goto) – C# (goto) Statement-Level Control Structures

Guarded Commands • Dijkstra’s selection statement has the form if <Boolean expression> -> <statement>

Guarded Commands • Dijkstra’s selection statement has the form if <Boolean expression> -> <statement> []. . . [] <Boolean expression> -> <statement> fi • fi taken ALGOL 68 • [] (fatbars) – Used separate the guarded clauses & allow them stmt sequences. – Each line in the selection stmt • Consisting = Boolean expression (a guard) & stmt or stmt sequence (guarded command) Statement-Level Control Structures

Guarded Commands • Dijkstra’s selection statement has the form if <Boolean expression> -> <statement>

Guarded Commands • Dijkstra’s selection statement has the form if <Boolean expression> -> <statement> []. . . [] <Boolean expression> -> <statement> fi • fi taken ALGOL 68 • [] (fatbars) – Used separate the guarded clauses & allow them stmt sequences. – Each line in the selection stmt • Consisting = Boolean expression (a guard) & stmt or stmt sequence (guarded command) Statement-Level Control Structures

 • 377 Statement-Level Control Structures

• 377 Statement-Level Control Structures