Expressions: Prepared by Haydee-Dulay Limson

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Expressions

Prepared by Haydee-Dulay Limson


Objective

•At the end of this module you should be


able to :
•Specify an expression;
Kinds of Expressions
• Expressions may be composed in various ways in a programming
language. There are five basic kinds that make up an expression.
These are:
• Literals
• Aggregates
• Function calls
• Conditional expressions
• Constants and variables
Literal
• The simplest kind of expression is the literal. The
literal is the basic element of all other expressions. It
is an expression that is fixed and manifests value of
some type. Examples in C are the literals:
• 100 5.25 ‘c’
• They denote the integer, real and character type
literals:
Aggregate
• An aggregate is an expression that constructs an aggregate
value from its component values. An example is the ML
record aggregate:
• {y = 2000, m = “Jan”, d = 1}
• Constructs, a value of the record type { y : int, m:string, d:
int}
• In Pascal, there is no equivalent expression for this record
aggregate. To initialize a record in Pascal, one has to assign
the values one by one to the components.
Function Call
• A function call basically does computation on arguments passed to the
function and procedure result. This result is returned back to the caller. All
explicit function call has the form:
• F(actual parameters).
• An operator may be thought of as denoting a function. Applying a unary or
binary operator on its operand(s) is essentially the same as a functions call
passing the operands as arguments. For example, the arithmetic expression
• a+b*c–d
• Is essentially equivalent to the function calls:
• * (+ (a,b), - (c,d))
Conditional Expression
• It allows several subexpressions to be included in the expression but only
one of these subexpressions is chosen to be evaluated. An example of this
is the ML if expression.
• if n mod 2 = 0 then true else false
• This should not be confused with conditional commands. Pascal and Ada
do not support conditional expressions but support conditional commands.
For example, in ML we might have the following statement:
• val even := if n mod 2 = 0 then true else false
• Which in Pascal has to be written
• If n mod 2 =0 then even := true else even :=false.
Constants and Variables
• Other very basic expressions are the constants and variables. In
pascal, the typed and variable declaration
• const a = 5.25;
• var b : real;
• Declares this expression type. For example, in the expression:
• 52.35 + a * b
• a is a constant access while b is a variable access. The a in the
expression is replaced by 6.25 and b is whatever is stored in the data
object named b.
Syntax for Expressions

•Notations of expressions in all


programming languages can be
classified into three forms:
prefix, infix and postfix
notations.
Prefix Notation
• In prefix notation the operation symbol is placed ahead of
the operands in left to right order. If the operand is also an
expression, then the same rules apply. The most commonly
used notation is to enclose the operands with parentheses,
separating operands by commas. The following is an example
of an expression prefix notation:
• +(*(a,b),-(c,d))
• A variant of this notation called Cambridge Polish is used in
Lisp.
Cambridge Notation
•. In Cambridge notation, the parenthesis after an
operator is moved before the operator and the
commas are removed. This makes the expression
look like a nested set of lists, where each list
begins with an operator symbol followed by the
lists representing the operands:
(+ (* a b ) (– c d))
Polish
• Another variant is the Polish form which drops all the
parentheses. Hence, the expression above becomes:
+*ab- cd
Although prefix notation is used in very few
programming languages for binary and logical
operations, it is the notation used for most monadic
operations, e.g., the negative value of a, -a, and
function calles, e.g., power (b,n).
Postfix Notation
• Postfix Notation can be viewed as the opposite of prefix. In postfix
notation, the operator symbol follows that of the operands. For
example, the same expression above can be written in postfix as:
((a,b) *, (c,d) -) or a b * c d - +
Infix Notation
• It is suitable for binary operations. In infix notation, the operator is
placed in between two operands ( which is why it is suitable for
binary operations). For example, the expression given earlier may be
written in infix as follows:

(a * b) + ( c – d)

You might also like