Expression and Statements

Cards (59)

  • Expressions
    The most fundamental computation mechanism in programs. An expression, in its pure, mathematical, form, returns a value and produces no side effect.
  • Statements
    Executed for their side effects and return no value
  • In functional languages, expressions are sometimes called expression languages
  • Even in non-functional languages, expressions play a significant role, and in some languages, it is called an expression-oriented language
  • Expressions
    • They are closest in appearance to mathematics
    • In the absence of side effects, program expressions have semantics that are very similar to those of mathematical expressions, which leads to semantic simplicity and precision
    • In the presence of side effects, expressions can behave in ways that are very different from their mathematical counterparts, which can be a source of confusion and error
  • Building up complex expressions

    Basic expressions are combined recursively by the application of operators and functions, sometimes involving grouping symbols such as parentheses
  • Unary operator

    An operator that takes one operand
  • Binary operator
    An operator that takes two operands
  • Infix notation
    Operators are written between their operands
  • Postfix notation
    Operators are written after their operands
  • Prefix notation
    Operators are written before their operands
  • Postfix and prefix forms
    • They do not require parentheses to express the order in which operators are applied
    • Operator precedence is not required to disambiguate an unparenthesized expression
  • Predefined operators
    They are written in infix form, with specified associativity and precedence rules
  • Functions
    They can be either predefined or user-defined, and are written in prefix form with operands viewed as arguments or actual parameters
  • Applicative order evaluation
    1. All operands are evaluated first and then operators are applied to them
    2. Corresponds to a bottom-up evaluation of the values at nodes of the syntax tree representing an expression
  • If the evaluation of an expression causes no side effects, then the expression will yield the same result, regardless of the order of evaluation of its subexpressions
  • In the presence of side effects, the order of evaluation can make a difference
  • In C, assignment is an expression, not a statement, and can be combined in expressions
  • Sequence operator
    Allows several expressions to be combined into a single expression and evaluated sequentially (e.g. the comma operator in C)
  • Guarded if statement

    A general form of conditional statement where each guard is a Boolean expression and each statement sequence is executed if its corresponding guard is true, introduced by Edsger W. Dijkstra
  • If statement
    The basic form of conditional statement is as given in the extended Backus-Naur form rule for the if statement in C, with an optional "else" part
  • The dangling-else problem is an ambiguity in the syntax of if statements, solved by the "most closely nested rule" in C and Pascal
  • Case/switch statement
    A special kind of guarded if, where the guards are ordinal values selected by an ordinal expression, invented by C.A.R Hoare
  • Guarded do
    A general form of loop construct, given by the structure corresponding to Dijkstra's guarded if, where the loop is repeated until all the guards are false (Bi)
  • While loop
    The most basic form of loop construct, a guarded do with only one guard that eliminates nondeterminism
  • Do-while loop
    An alternative loop statement that ensures the code is executed at least once
  • Different Expressions
    infix: 3 + 4 * 5
    postfix: 3 4 5 *+
    prefix: + 3 * 4 5
  • The advantage of postfix and prefix forms is that they do not require parentheses to express the order in which operators are applied. Thus, operator precedence is not required to disambiguate an unparenthesized expression.
  • Operators are predefined and (if binary) written in infix form, with specified associativity and precedence rules, whereas functions, which can be either predefined or user-defined, are written in prefix form and the operands are viewed as arguments or actual parameters to calls of the functions.
  • According to one common evaluation rule, all operands are evaluated first and then operators are applied to them. The most common rule in programming languages
    Applicative order evaluation, or strict evaluation
  • Unlike most other operators in C, the order of evaluation is specified as left to right, and the value of the rightmost expression is the value returned by the entire expression.
  • The well-known if-statement is the most common form of this construct.
  • The semantics of this statement are as follows: each Bi ’s is a Boolean expression, called a guard, and each Si ’s is a statement sequence. If one Bi ’s evaluates to true, then the corresponding statement sequence Si is executed. If more than one Bi ’s is true, then the one and only corresponding Si ’s is selected for execution. If no Bi is true, then an error occurs
    Control Statements
  • The ambiguity called dangling-else problem is solved by C and Pascal by enforcing ___

    Disambiguating rule
  • The else is associated with the closest preceding if that does not already have an else part. This rule is also referred to as the most closely nested rule for if-statements.
    Disambiguating rule
  • Loops and their use to perform repetitive operations
  • do-statement is “syntactic sugar” (a language construct that is completely expressible in terms of other constructs), but it does express a common situation.
  • The expression e1 is the initializer of the for-loop, e2 is the test, and e3 is the update.
    Loops and Variations on WHILE
  • Many languages severely restrict the format of the for-loop, so that it can only be used for indexing situations.
  • used to be the mainstay of a number of programming languages, such as Fortran77 and BASIC
    Goto/Gotos