Lecture 19 & 20
Lecture 19 & 20
Wah Campus
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.
• is equivalent to
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++)
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.
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
sum = count = 0;
Multiple-target
assignments,
such as
legal in Python
• 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
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)
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.
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++
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++
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.
following
Java-like
The indentation seems to indicate that the
else clause belongs with the first then clause
code:
END
_______________________________