Imperative Programming: Expressions, Assignments, and Operators

expressions assignments n.w
1 / 15
Embed
Share

Learn about imperative programming concepts such as expressions, assignments, and operators. Explore how programming languages handle precedence, associativity, and side effects in expressions and assignments.

  • Imperative Programming
  • Expressions
  • Assignments
  • Operators
  • Programming Languages

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Expressions & Assignments Imperative programming is based on the concept that the running program has a state variables with values Program code will use assignment statements to alter those variables assignment statements often use expressions as their RHS expressions can be arithmetic boolean relational because of the reliance on arithmetic expressions, programming languages would inherit much from math conventions (variables, math operators, parens)

  2. Continued Expressions consist of operands and operators Three forms of operators Binary (2 operands, the standard form of mathematical operator) Unary (1 operand as in C s i++ or in unary as in -5) Ternary (3 operands, as in C s conditional statement) Expressions requiring fetching the operator followed by operands, then applying the operator to those operands Design issues: what are the precedence and associativity rules? what order are operands evaluated? are there side effects? is operator-overloading available? what type mixing is allowed?

  3. Operators Precedence FORTRAN C-languages Ada Ruby Highest ** *, / prefix ++, -- +, - Lowest postfix ++, -- **, abs *, /, mod, rem unary +, - ** unary +, - unary +,- *, /, % *, /, % binary +,- binary +, - binary +, - ( and ) have the highest precedence in all languages it should also be noted that floating point arithmetic is not necessarily associative because of issues with precision a + b + c may not equal a + (b + c)

  4. Associativity If two or more operators are of the same precedence, they are applied based on associativity all languages use left-to-right associativity except when dealing with exponent and unary operators in FORTRAN, exponent is the only right-to-left operator in Ada, a**b**c is illegal and so most be parenthesized in Ada, unary has lower precedence than mod a mod b is (a mod b), not (-a) mod b in C-langauges, ++/-- and unary +/- are right associative and there is no exponent operator in VB, exponent (^) is left associative

  5. Side Effects A side effect is when a function changes a parameter passed to it (or a global variable) This may lead to associativity not being maintained Example A + Fun(A) Fun(A) changes A to A+1 with left-to-right associativity, the sum is A + Fun(A), as expected with right-to-left associativity, the sum is A+1+ Fun(A) because A is changed to A+1 when Fun(A) executes Since A + Fun(A) should equal Fun(A) + A, the side effect would cause the language not to support associativity entirely

  6. More on Side Effects Side effects can occur when the parameter is passed by reference (a pointer) side effects are irrelevant if the language requires any function call be located at the end of an expression (but this limits you to 1 function call per expression) some languages disallow side effects parameters may only be passed by copy, not by reference and no global variables are permissible in the function Here is how some languages deal with side effects C/C++, Ada, Pascal side effects are allowed FORTRAN 77: expressions with function calls are legal only if the function does not change the value of any operands in the expression Java: operands evaluated left-to-right, programmer can avoid side effects by placing function calls to the right in an expression

  7. Overload Operators Same operator is usable with different types arithmetic operators (+, -, *, /) are all used for byte, short, int, long, float and double, so they are all overloaded implementation differs for each (or for many) overloading aids readability and writability overloading requires type checking + may also be used for and, union or string concatenation * is overloaded in C/C++ for pointer dereferencing & is overloaded in C/C++ to generate an address and bitwise AND compiler must choose the correct meaning of the operator given operand types Question: can the programmer overload an operator? available in Ada, C++, FORTRAN 95, Common Lisp, C# C++ restricts which operators can be overloaded (., .*, ::, ?:, sizeof)

  8. Type Conversions Two dimensions of type conversions explicit versus implicit conversion implicit conversions are called coercions and are usually initiated by the compiler Ada does not do implicit coercions, so the assignment below is not legal since * cannot apply to both an integer and a float A : Integer; B, C : Float; C = A * B; in C-languages, an explicit conversion is called a cast is the conversion increasing or decreasing precision? narrowing going from a larger type to smaller type (e.g. int to short int) narrowing is rarely safe widening going from a smaller type to larger widening is almost always safe however, consider going from a 32-bit int to a 32-bit float while this is thought of as widening, you might lose precision!

  9. Relational and Boolean Expressions Evaluate to True/False relational operators always have lower precedence than arithmetic so that the relational comparison is performed after any arithmetic operations (ex. a+1 > b-2) boolean operators usually have lower precedence than relational operators except possibly NOT FORTRAN abbreviations originated because early keypunches did not have <, > symbols available Operation Equal Not equal Greater Than Less Than Greater/Equal >= Less/Equal AND OR NOT Ada C/Java FORTRAN77 = = = /= != > > < < >= <= <= and && .AND. or | | not ! .NOT. .EQ. .NE. .GT. .LT. .GE. .LE. FORTRAN 95 continued to use the abbreviations, but also included = =, <>, <, >, <=, >= JavaScript/PHP add = = = and != = for non- coercible comparison .OR.

  10. Example expressions In FORTRAN: A+B .GT. 2 * C . AND. K .NE. 0 order of operations: *, +, GT, NE, AND In Ada A > B and A < C or K=0 is illegal because and/or have equal precedence and therefore must parenthesize, the proper expression is (A > B and A < C) or K = 0 with order of precedence as <, > and = performed first in a left-to-right order, followed by and followed by or In C: a > b > c is legal as a > b returns a 1 or 0 which is then compared to c C-language operator precedence postfix++,-- prefix ++, --, ! unary +, - *, /, % binary +, - <, >, <=, >= = =, != && | |

  11. Short Circuiting A short circuited evaluation of an expression occurs if the result can be determined without evaluating the entire expression (13 * A) * (B / 13 - C) if A=0, expression evaluates to 0, right term (B / ) is ignored (A >= 0) and (B < 10) if A < 0, right side ignored since entire expression is false We take advantage of short circuiting to simplify code to avoid errors while(ptr!=NULL && ptr->value > target) ptr = ptr->next; without short circuiting, we either risk an error arising or we have to separate this into a while loop (ptr!=NULL) with an if statement inside it (ptr->value > target)

  12. Short Circuiting in Various Languages In Ada, short-circuiting is available using AND THEN and OR ELSE While (Index <= Listlen) and then (List[index] /= key) do In Modula-2, Turbo Pascal, AND and OR are short- circuited this is not true of Standard Pascal In C, C++, Java, && and || are short circuited, & and | are not one must be cautious in C-languages when using short circuiting, consider this: (a > b) && ((b++) / 3) if a > b is false, the right side of the expression is not performed and so b remains unchanged!

  13. The Assignment Statement Syntactic form: <target_var> <ass_op> <expr> In order to differentiate assignment and equality, two different operators are used FORTRAN, BASIC, PL/I, C, C++, Java, C#, Python all use = for <ass_op> Ada/Algol/Pascal/Modula all use := in F#, you must precede any assignment with the word let as in let x = 0 In PL/I, = is used for both assignment and equality this leads to the instruction A = B = C being difficult to understand unlike in C where it does B = C and then A = B, this sets A equal to the boolean expression B = C if B = C then A is set to true, otherwise false

  14. Compound Assignments Starting in C, languages began offering the ability to do multiple assignments in one statement C offers three forms: a = b + (c = d / b) 1 assigns c to d / b, then assigns a to b + c 1 prefix/postfix incr/decr can be placed in an expression as in a = b++; which differs from a = ++b; a = b = c (b = c; a = b; ) You can also do assignment in conditions as in while((c = getchar)!= \n ) And C-like languages have short-cut operators +=, -=, etc

  15. In Other Languages PL/I SUM, TOTAL = 0 Python a, b, c = <expr1>, <expr2>, <expr3> Common Lisp (setf a b c d) same as a=b, c=d Perl ($a, $b, $c) = (1, 2, 3); C-languages also have the conditional statement of the form flag ? count1 : count2 = 0

Related


More Related Content