0% found this document useful (0 votes)
52 views2 pages

06 Expressions and Assignment Statements

The document discusses expressions and assignment statements in programming languages. It defines arithmetic, relational, and Boolean expressions, and covers operator precedence and associativity. It also discusses type conversions, operator overloading, and short-circuit evaluation. Finally, it defines assignment statements and covers compound assignment operators and prefix/postfix operators.

Uploaded by

Hyper Sirios
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views2 pages

06 Expressions and Assignment Statements

The document discusses expressions and assignment statements in programming languages. It defines arithmetic, relational, and Boolean expressions, and covers operator precedence and associativity. It also discusses type conversions, operator overloading, and short-circuit evaluation. Finally, it defines assignment statements and covers compound assignment operators and prefix/postfix operators.

Uploaded by

Hyper Sirios
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

IT2009

Expressions and Assignment Statements


Arithmetic Expressions
· Expressions are the fundamental means of specifying computations in a programming language.
· An arithmetic expression consists of operators, operands, parentheses, and function calls.
· Three (3) types of operators:
o A unary operator has a single operand.
o A binary operator has two (2) operands.
o A ternary operator has three (3) operands.
· In most programming languages, binary operators are inflix, which means they appear between
operands.
· Operator precedence specifies the order of operations in expressions that contain more than one (1)
operator.
Precedence Ruby C-Based Languages
Highest ** postfix ++, --
unary +, - prefix ++, --, unary +, -
*, /, % *, /, %
Lowest binary +, - binary +, -
· Operators are considered adjacent if they are separated by a single operand.
· When an expression has two (2) operators with the same precedence, the expression is evaluated
according to its associativity.
o Left associativity: When there are two (2) adjacent operators with the same precedence, the
left operator is evaluated first.
o Right associativity: When there are two (2) adjacent operators with the same precedence,
the right operator is evaluated first.
Language Associativity Rule
Ruby Left: *, /, +, -
Right: **
C-Based Languages Left: *, /, %, binary +, binary -
Right: ++, --, unary -, unary +
Ada Left: all except **
Nonassociative: **
· The precedence and associativity rules can be altered by placing parentheses in expressions.
· The if-then-else statements can be used to perform a conditional expression assignment.
· Variables in expressions are evaluated by fetching their values from memory.
· If an operand is a parenthesized expression, all of the operators it contains must be evaluated before
its value can be used as an operand.
· A side effect of a function (or functional side effect) occurs when the function changes either one of
its parameters or a global variable. A global variable is declared outside the function but is accessible
in the function.
· A program has the property of referential transparency if any two (2) expressions in the program that
have the same value can be substituted for one another anywhere in the program, without affecting
the action of the program.

Operator Overloading
· An operator can be used for more than one (1) purpose. This multiple use of an operator is called
operator overloading. Example: x = &y;
The ampersand in C++ is usually used to specify a logical AND operation; but in this case, it was used
to place the address of y in x.

06 Handout 1 *Property of STI


Page 1 of 2
IT2009

Type Conversions
· Two (2) types of type conversion:
o A narrowing conversion converts a value to a type that cannot store even approximations of
all the values of the original type. Example: Converting a double to a float in Java
o A widening conversion converts a value to a type that can include at least approximations of
the values of the original type. Example: Converting an int to a float in Java
· An expression with operands that have different data types is called a mixed-mode expression.
· A cast is an explicit type conversion, which is performed by placing the desired type in parentheses
just before the expression to be converted. Example: (int) angle
· An error that occurred when the result of an operation is too large is called overflow. An error that
occurred when the result of an operation is too small is called underflow.

Relational and Boolean Expressions


· A relational operator is an operator that is used to compare the values of two (2) operands.
· A relational expression has at least two (2) operands and one (1) relational operator.
· Boolean expressions consist of Boolean variables, Boolean constants, relational expressions, and
Boolean operators.
· The precedence of the arithmetic, relational, and Boolean operators in the C-based languages:
Highest postfix ++, --
unary +, -, prefix ++, --, !
*, /, %
binary +, -
<, >, <=, >=
=, !=
&&
Lowest ||

Short-Circuit Evaluation
· A short-circuit evaluation of an expression is one in which the result is determine without evaluating
all of the operands and/or operators. Example: (13 * a) * (b / 13 – 1)

Assignment Statements
· An assignment statement stores a value or the result of an expression in the variable.
· A compound assignment operator is a shorthand method of specifying commonly needed form of
assignment. Example: sum += value;
· Prefix operators are operators that precede the operands. Example: sum = ++count;
· Postfix operators are operators that follow the operands. Example: sum = count++;
· An assignment can be used as an expression and as an operand in other expressions.
· Several recent programming languages provide multiple-target, multiple-source assignment
statements. Example: ($first, $second, $third) = (20, 40, 60);
· An assignment that consists of different data types is called a mixed-mode assignment.

References:

Sebesta, Robert W. (2012). Concepts of Programming Languages. 10th ed. USA: Pearson Education, Inc.

Ben-Ari, Mordechai (2006). Understanding Programming Languages. Chichester: John Wiley & Sons, Inc.

Tucker, Allan B. and Noonan, Robert E. (2002). Programming Languages: Principles and Paradigms. 2nd ed.
New York: Mc-Graw Hill

06 Handout 1 *Property of STI


Page 2 of 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy