0% found this document useful (0 votes)
2 views

Lecture 19 & 20

The document discusses expressions and assignment statements in programming languages, covering topics such as simple assignments, conditional targets, and various types of assignment operators. It also introduces control structures, including selection statements and their nesting, explaining how these structures determine the flow of execution in programs. Additionally, it highlights mixed-mode assignments and the differences in assignment behavior across different programming languages.

Uploaded by

Hanzala Shafique
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)
2 views

Lecture 19 & 20

The document discusses expressions and assignment statements in programming languages, covering topics such as simple assignments, conditional targets, and various types of assignment operators. It also introduces control structures, including selection statements and their nesting, explaining how these structures determine the flow of execution in programs. Additionally, it highlights mixed-mode assignments and the differences in assignment behavior across different programming languages.

Uploaded by

Hanzala Shafique
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/ 31

COMSATS University Islamabad,

Wah Campus

Lecture No. 19 & 20


Expressions and Assignment Statements:
• Simple Assignments • Conditional Targets • Compound Assignment Operators •
Unary Assignment Operators • Assignment as an Expression • Multiple Assignments
• Assignment in Functional Programming Languages

Mixed-Mode Assignment, Control Structures – Introduction, Selection Statements

Theory Of Programming Languages


Fall 2024 1
~~~ Dr. Khalid Iqbal Khattak ~~~
Expressions and Assignment Statements
• An expression in programming is a combination of literals,
variables, operators, and function calls that produce a value
when evaluated.
– Expression represents a computation and can be as simple as a single
variable or as complex as a mathematical equation.

Theory Of Programming Languages


Fall 2024 2
~~~ Dr. Khalid Iqbal Khattak ~~~
Expressions and Assignment Statements
• An Assignment statement is a statement that is used
to set a value to the variable name in a program.
• Assignment statement allows a variable to hold
different types of values during its program lifespan

Theory Of Programming Languages


Fall 2024 3
~~~ Dr. Khalid Iqbal Khattak ~~~
Simple Assignments
ALGOL 60 pioneered the use of := as the assignment
operator
• avoids the confusion of assignment with equality.
• Ada also uses this assignment operator.

Wide use of Assignment with variation in


Programming languages

In Fortran and Ada, an assignment is a stand-alone


statement
• Destination is restricted to a single variable.

Theory Of Programming Languages


Fall 2024 4
~~~ Dr. Khalid Iqbal Khattak ~~~
Conditional Targets
• Perl allows conditional targets on assignment
statements
e.g. ($flag ? $count1 : $count2) = 0;

which is equivalent to

if ($flag) {
$count1 = 0;
} else {
$count2 = 0;}
Theory Of Programming Languages
Fall 2024 5
~~~ Dr. Khalid Iqbal Khattak ~~~
Compound Assignment Operators
Compound A compound assignment operator is
assignment combines a shorthand method of specifying a
commonly needed form of
assignment with assignment (e.g. x = x + y or a=a + b)
another operator.

The value of the expression on


the RHS of the equal sign is
stored in the variable, subscript
element, or range on the LHS.

The old value of the variable, if


any, is discarded, and the value
of the expression is stored in
the variable.

Theory Of Programming Languages


Fall 2024 6
~~~ Dr. Khalid Iqbal Khattak ~~~
Compound Assignment Operators
• Compound assignment operators were
introduced by ALGOL 68
• Later adopted in a slightly different form by C,
and are part of the other C-based languages, as
well as Perl, JavaScript, Python, and Ruby
• For example, sum += value;

• is equivalent to

sum = sum + value;

Theory Of Programming Languages


Fall 2024 7
~~~ Dr. Khalid Iqbal Khattak ~~~
Unary Assignment Operators

A Unary Operator in
Computer Science In the assignment
statement This operation could
is an operator that also be stated as
acts on a single sum = ++ count;
operand.

Examples of unary
operators include The value of count is count = count + 1;
• unary minus (-x) incremented by 1 and sum = count;
• unary plus (+x) then assigned to sum.
• prefix decrement (--x)
• postfix increment (x++)

Theory Of Programming Languages


Fall 2024 8
~~~ Dr. Khalid Iqbal Khattak ~~~
Unary Assignment Operators

When two unary count is first


operators apply incremented
to the same For example, and then
rather than
operand, the in negated. So, it
association is is equivalent
right to left. to

- (count ++) (- count) ++


- count ++

Theory Of Programming Languages


Fall 2024 9
~~~ Dr. Khalid Iqbal Khattak ~~~
Assignment as an Expression
In most expression-oriented programming languages (Like, C), the assignment
statement returns the assigned value
• while ((ch = getchar()) != EOF) { ... }
• getchar() takes an input character from keyboard and assigned to ch.
• The result, or value assigned, is then compared with the constant EOF.
• If ch is not equal to EOF, the compound statement {...} is executed

The precedence of the assignment operator is lower than that of the


relational operators

Assignment must be parenthesized.

Without the (), the new character would be compared with EOF first. Then,
the result of that comparison, either 0 or 1, would be assigned to ch.

Theory Of Programming Languages


Fall 2024 10
~~~ Dr. Khalid Iqbal Khattak ~~~
Assignment as an Expression
• a = b + (c = d / b) - 1

For
Expression
example,
side effects
the
without (),
expression

denotes the
instructions

• Assign d / b to c
Assign b + c to temp
Assign temp - 1 to a

Theory Of Programming Languages


Fall 2024 11
~~~ Dr. Khalid Iqbal Khattak ~~~
Assignment as an Expression
Loss of error detection
if (x = y) ... instead of if
in the C design of the
(x == y) ...
assignment operation

sum = count = 0;
Multiple-target
assignments,
such as
legal in Python

Theory Of Programming Languages


Fall 2024 12
~~~ Dr. Khalid Iqbal Khattak ~~~
Multiple Assignments
Assignments can be • Consider the following
example:
made to multiple
• A, X = B + C; The value
variables in a single of B + C is assigned to
assignment statement. both A and X

• In Perl we can write


($first, $second, $third) =
(20, 40, 60);
Several recent PLs Perl
and Ruby provide ($first, $second) =
multiple-target, ($second, $first);
multiple-source correctly interchanges the
assignment statements values of $first and
$second, without the use
of a temporary variable
Theory Of Programming Languages
Fall 2024 13
~~~ Dr. Khalid Iqbal Khattak ~~~
Assignment in Functional Programming Languages

Functional programs do not


This eliminates any chances
have assignment statements,
of side effects because any
i.e., the value of a variable in
variable can be replaced with
a functional program never
its actual value at any point of
changes once defined. E.g. val
execution.
cost = quantity * price;

Some of the popular


functional programming
languages
include: Lisp(Impure FL),
Python, Erlang, Haskell(Pure
FLs), Clojure
Theory Of Programming Languages
Fall 2024 14
~~~ Dr. Khalid Iqbal Khattak ~~~
Assignment in Functional Programming Languages

• F# has a somewhat similar declaration that uses the let


reserved
let val word.
radius = 2.7 val pi = 3.14 in pi * radius * radius end;

• F#’s let and ML’s val is that let creates a new scope,
whereas val does not
• val declarations are often nested in let constructs in ML
(e.g. val t = 10 : int val m = 20.0 : real fun square(x) = x * x;
fun square(x : real) = x * x; fun square(x) = (x : real) * x; fun
square(x) = x * (x : real);)
• F# Examples: let x = 42 let myName = "Kalle"
– /// Do some arithmetic starting with the first integer
let sampleInteger2 = (sampleInteger/4 + 5 - 7) * 4
– /// A list of the numbers from 0 to 99
let sampleNumbers = [ 0 .. 99 ]
– /// A list of all tuples containing all the numbers from 0 to 99
and their squares
let sampleTableOfSquares = [ for i in 0 .. 99 -> (i, i*i) ]
https://www.fincher.org/tips/Languages/fsharp.shtml
Theory Of Programming Languages
Fall 2024 15
~~~ Dr. Khalid Iqbal Khattak ~~~
Mixed-Mode Assignment
Mixed mode is when an expression contains both reals and integers.
• If ANY of the operands are real then result of the operation will be real

C, C++, and Perl use coercion rules for mixed-mode assignment

Python and Ruby, types are associated with objects, not variables, so no
mixed-mode assignment
In FORTRAN, C, and C++, any numeric value can be assigned to any numeric
scalar variable; whatever conversion is necessary is done
In Pascal, integers can be assigned to reals, but reals cannot be assigned to
integers (the programmer must specify whether the conversion from real to
integer is truncated or rounded)

In Java, only widening assignment coercions are done

In Ada, there is no assignment coercion


Theory Of Programming Languages
Fall 2024 16
~~~ Dr. Khalid Iqbal Khattak ~~~
Mixed-Mode Assignment

The Narrow conversion: The type int,


For example: Mixed-mode
expression b+i is assigned to
int value is coerced to assignments is a
is mixed- a char, byte,
the type of the simple and an
mode or short
variable in a effective way to
int i, j; narrowing conversion increase the
double x, y; reliability of Java
and C#, relative
complex a, b; to C and C++
a = sin((b+i)/y) + x/j;

Theory Of Programming Languages


Fall 2024 17
~~~ Dr. Khalid Iqbal Khattak ~~~
Control Structures – Introduction
• A control structure is a control statement and the
collection of statements whose execution it controls.
• Sequence of statements (go in order, line-by-line),
repetition (repeat code), Boolean (true/false), and switch
statements (a way to replace multiple if/else statements)
• Control structures / Control statements enable a
programmer to determine the order in which program
statements are executed. These control structures allow
you to do two things:
– skip certain statements while executing others
– repeat one or more statements while certain condition is true.

Theory Of Programming Languages


Fall 2024 18
~~~ Dr. Khalid Iqbal Khattak ~~~
Control Structures – Introduction
• A control structure is a control statement and the
collection of statements whose execution it controls.
• Sequence of statements (go in order, line-by-line),
repetition (repeat code), Boolean (true/false), and switch
statements (a way to replace multiple if/else statements)
• Control structures / Control statements enable a
programmer to determine the order in which program
statements are executed. These control structures allow
you to do two things:
– skip certain statements while executing others
– repeat one or more statements while certain condition is true.

Theory Of Programming Languages


Fall 2024 19
~~~ Dr. Khalid Iqbal Khattak ~~~
Control Structures – Selection Statements

Selection statements
Selection statements
(sometimes called
A selection statement fall into two general
conditional statements) can
provides the means of categories:
be defined as code
choosing between two • two-way
(statements) that is or more execution
executed only when a • n-way, or multiple
paths in a program. selection.
certain condition is
satisfied.

Theory Of Programming Languages


Fall 2024 20
~~~ Dr. Khalid Iqbal Khattak ~~~
Selection Statements: Two way design
issues

The form and type of the expression that controls the


selection?

The then and else clauses specification

The meaning of nested selectors be specified

Theory Of Programming Languages


Fall 2024 21
~~~ Dr. Khalid Iqbal Khattak ~~~
Selection Statements: The Control
Expression

Control expressions are specified in ()


• if the then reserved word (or some other syntactic marker) is not used to introduce the then
clause.
• In cases where the then reserved word (or alternative marker) is used.

In C89, did not have a Boolean data type, arithmetic expressions were used as control
expressions.

Boolean and arithmetic expressions can also be done in Python, C99, and C++

Theory Of Programming Languages


Fall 2024 22
~~~ Dr. Khalid Iqbal Khattak ~~~
Selection Statements: The Control
Expression

Control expressions are specified in ()


• if the then reserved word (or some other syntactic marker) is not used to introduce the then
clause.
• In cases where the then reserved word (or alternative marker) is used.

In C89, did not have a Boolean data type, arithmetic expressions were used as control
expressions.

Boolean and arithmetic expressions can also be done in Python, C99, and C++

Theory Of Programming Languages


Fall 2024 23
~~~ Dr. Khalid Iqbal Khattak ~~~
Selection Statements: Clause Form
In many languages, the then
and else clauses appear as
either single statements or
compound statements Note: A colon is used
to introduce the then
clause in Python.
Perl
• all then and else clauses must be
compound statements, even if
have only one statement

Python uses indentation to if x > y :


specify compound x=y
statements (equally
indented ) print "case 1“
Theory Of Programming Languages
Fall 2024 24
~~~ Dr. Khalid Iqbal Khattak ~~~
Selection Statements: Nesting Selectors
A straightforward grammar for a two-way selector statement

Ambiguous grammar was as follows:

• <if_stmt> → if <logic_expr> then <stmt>


| if <logic_expr> then <stmt> else <stmt>

The issue is that when a selection statement is nested in the then clause
of a selection statement, it is not clear with which if an else clause
should be associated.

Reflected in the semantics of selection statements.

Theory Of Programming Languages


Fall 2024 25
~~~ Dr. Khalid Iqbal Khattak ~~~
Selection Statements: Nesting Selectors
if (sum == 0)
if (count == 0)
result = 0;
else
result = 1;

Consider the The else clause is matched with the first


then clause or the second

following
Java-like
The indentation seems to indicate that the
else clause belongs with the first then clause

code:

Theory Of Programming Languages


Fall 2024 26
~~~ Dr. Khalid Iqbal Khattak ~~~
Selection Statements: Nesting Selectors
• C, C++, and C# have the same problem as Java
with selection statement nesting.
In Java: the inner if is put in a compound
if (sum == 0) {
if (count == 0)
result = 0;
} In Perl, the code can be written as follows:
else if (sum == 0) {
result = 1; if (count == 0) {
result = 0; }
} If the alternative semantics were needed, it
else would be
{ result = 1; if (sum == 0) {
} if (count == 0) {
result = 0;
}
else {
result = 1;
Fall 2024 } }Of Programming Languages
Theory
27
~~~ Dr. Khalid Iqbal Khattak ~~~
Selection Statements: Nesting Selectors
• In Ruby. no syntactic entity to mark the end of the whole selection
statement
• The use of a special word for this purpose resolves the question of the
semantics of nested selectors and also adds to the readability of the
statement
In Ruby : the the use of end
if a > b then sum = sum + a
acount = acount + 1
else sum = sum + b
bcount = bcount + 1
end

Nested if can be written in Ruby like this


if sum == 0 then
if count == 0 then result = 0
else
result = 1
end
Fall 2024 end Theory Of Programming Languages
28
~~~ Dr. Khalid Iqbal Khattak ~~~
Selection Statements: Nesting Selectors
• ML does not have a problem with nested selectors
because it does not allow else-less if statements.
if sum == 0 then
if count == 0 then
result = 0
end
else
result = 1
end
• In Python, is semantically equivalent to the last Ruby
statement above:
if sum == 0 :
if count == 0 :
result = 0
else:
result = 1 Theory Of Programming Languages
Fall 2024 29
~~~ Dr. Khalid Iqbal Khattak ~~~
Selection Statements: Selector Expressions
• In the functional languages ML, F#, and LISP, the
selector is not a statement; it is an expression that
results in a value
• Return value type should be same for if and else
construct
F#: creates the name y and sets it
to either x or 2 * x
let y =
if x > 0 then x
else 2 * x;;

Theory Of Programming Languages


Fall 2024 30
~~~ Dr. Khalid Iqbal Khattak ~~~
_______________________________

END

_______________________________

You might also like