0% found this document useful (0 votes)
21 views34 pages

22 - 3014 - 033401 - Unit 2 Part 2 Expression and Assignment Statements

The document covers key concepts in programming languages, focusing on data structuring, expression evaluation, and assignment statements. It discusses various data types, operator overloading, type conversions, and control statements, as well as the importance of operator precedence and short-circuit evaluation. Additionally, it highlights the differences in assignment syntax and behavior across different programming languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views34 pages

22 - 3014 - 033401 - Unit 2 Part 2 Expression and Assignment Statements

The document covers key concepts in programming languages, focusing on data structuring, expression evaluation, and assignment statements. It discusses various data types, operator overloading, type conversions, and control statements, as well as the importance of operator precedence and short-circuit evaluation. Additionally, it highlights the differences in assignment syntax and behavior across different programming languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

SE Computer

Course Name :Principle of Programming Language


Course Code: 210256
Unit 2
Structuring the Data, Computations and Program
● Elementary Data Types : Primitive data Types, Character String types, User Defined Ordinal Types,
Array types, Associative Arrays, Record Types, Union Types, Pointer and reference Type.
● Expression and Assignment Statements: Arithmetic expression, Overloaded Operators, Type
conversions, Relational and Boolean Expressions, Short Circuit Evaluation, Assignment Statements,
Mixed mode Assignment.
● Statement level Control Statements: Selection Statements, Iterative Statements, Unconditional
Branching.
● Subprograms: Fundamentals of Sub Programs, Design Issues for Subprograms, Local referencing
Environments, Parameter passing methods. Abstract Data Types and Encapsulation Construct: Design
issues for Abstraction, Parameterized Abstract Data types, Encapsulation Constructs, Naming
Encapsulations

Department of Computer Engineering


Unit 2
Structuring the Data, Computations and Program
● Expression and Assignment Statements:
● Arithmetic expression,
● Overloaded Operators,
● Type conversions,
● Relational and Boolean Expressions,
● Short Circuit Evaluation, Assignment Statements,
● Mixed mode Assignment.

Department of Computer Engineering


Introduction

■ Expressions are the fundamental means of specifying


computations in a programming language.
■ To understand expression evaluation, need to be familiar
with the orders of operator and operand evaluation.
■ The purpose of assignment variable is to change the value of
variable.

4
Arithmetic Expressions
Airthmatic Expression
■ Arithmetic evaluation was one of the motivations for the development of
the first programming languages.

■ Arithmetic expressions consist of operators, operands, parentheses,


and function calls.

■ Operators can be:


⚪ A unary operator has one operand.
⚪ A binary operator has two operands.
⚪ A ternary operator has three operands.

5
Unary Arithmetic Operators in C/C++ and Java
■ Unary Plus ( +A ):
⚪ Examples: +num, +num/5, num1 * +num2
⚪ Has no effect on its operand in C/C++.
⚪ In Java, it causes an implicit conversion of a char, short, or
byte operand to int type; otherwise, it has no effect on its
operand.

■ Unary Minus ( -A ):
⚪ Examples: -num, -num/5, num1 * -num2
6
Unary Arithmetic Operators in C/C++ and Java (cont.)
■ The pre-increment is specified as follows: (++a)
⚪ It says to add 1 to the current value of the variable and then to use the new value (if
necessary).
■ The post-increment is specified as follows: (a++)
⚪ It says to use the current value of the variable (if necessary) and then add 1 to it.
■ The pre-decrement is specified as follows: (--a)
⚪ It says to subtract 1 from the current value of the variable and then to use the
new value (if necessary).
■ The post-decrement is specified as follows: (a--)
⚪ It says to use the current value of the variable (if necessary) and then subtract 1 from it.

7
Binary Arithmetic Operators in C/C++ and Java
Operation Operator Example Operand Result

Addition + A+B Both integers Integer


------------------- ------------------
One operand is not integer Double precision floating-point

Subtraction - A–B Both integers Integer


---------------------------- -----------------------
One operand is not integer Double precision floating-point

Multiplication * A*B Both integers Integer


------------------------------ -------------------------
One operand is not integer Double precision floating-point

Division / A/B Both integers Integer


------------------------------ -------------------------
One operand is not integer Double precision floating-point

Modulus % A%B Both integers Integer


(Remainder)

8
Ternary Operator in C/C++ and Java
A ternary operator “ ?: ” has three operands.

average = (count==0)? 0 : sum/count;

⚪ Evaluates as if written like:

if (count == 0) average = 0;
else average = sum/count;

9
Arithmetic Expressions: Operator Precedence Rules
■ The operator precedence rules for expression evaluation define the order in
which “adjacent” operators of different precedence levels are evaluated.
■ Typical precedence levels:
⚪ parentheses.
⚪ unary operators.
⚪ ** (the exponential operator, if the language supports it).
⚪ *, /, %
⚪ +, -

■ APL has a single level of precedence .


10
Arithmetic Expressions: Operator Associativity Rule
■ The operator associativity rules for expression evaluation define the
order in which adjacent operators with the same precedence level are
evaluated.

■ Typical associativity rules:


⚪ Left to right, except **, which is right to left.

■ APL is different; all operators have equal precedence and all operators
associate right to left.

■ Precedence and associativity rules can be overridden with parentheses.

11
Precedence Highest *, /, not

+, –, &, mod

– (unary)

=, /=, < , <=, >=, > a+b * c+d


and
((a + (b * c)1)2 + d)3
Lowest or, xor

Associativity Left to right


Precedence Highest *, /, not
a. a * b – 1 + c
+, –, &, mod
b. a * (b – 1) / c mod d
– (unary)
c. (a – b) / c & (d * e / a – 3)
=, /=, < , <=, >=, >
d. -a or c = d and e
and
e. a > b xor c or d <= 17
Lowest or, xor
f. -a + b
Associativity Left to right
int fun(int *i) {

*i += 5;

return 4; operands are evaluated left to right :

} operands are evaluated right to left.

void main() {

int x = 3;

x = x + fun(&x);

}
Arithmetic Expressions :Conditional Expressions
■ Conditional Expressions:
⚪ C-based languages (e.g., C, C++).
⚪ An example:
average = (count == 0)? 0 : sum/count;

⚪ Evaluates as if written like:


if (count == 0) average = 0;
else average = sum/count;

15
Operand Evaluation Order: Functional Side Effects
■ Functional side effects:
⚪ A side effect of a function occurs when a function changes either:
■ one of its parameters or
■ a global variable.

■ Assume fun(x) changes parameter x:


⚪ y = fun(x) + x;
⚪ y = x + fun(x);

16
Functional Side Effect: Example (2 of 2)
int a = 5; int a = 5;
int fun1() { int fun1() {
a = 17; a = 17;
return 3; return 3;
} }
void main() { void main() {
a = fun1() + a; a = a + fun1();
cout << a << endl; cout << a << endl;
} }

// 20 is printed // 8 is printed

17
Overloaded Operators
■ Operator Overloading is accomplished by defining functions that
have the same name as the operator being overloaded.
■ Some are common (e.g., +, -, *, … for int and float).

■ C++, FORTRAN 95 and Ada allow user-defined overloaded


operators.

■ Java does not permit operator overloading.

18
Overloaded Operators - division operator issue

■ avg= sum/count

■ Avg is floating point but sum and count are integer

■ The result gives integer value for Avg

19
Overloaded Operators - division operator solution
■ Pascal: two operators

○ div: integer solution

○ / : floating point solution

■ Javascript does not have integer airthmatic operators

○ Numbers in JavaScript are "double-precision 64-bit format IEEE 754


values".

■ In PHP , a floating point value is produced.

20
Overloaded Operators
■ It is harmful.

○ User defined operator overloading some one can define + for


multiplication.

■ C++ has a few operators that cannot be overloaded.

○ Structure member operator(.)

○ Scope resolution operator(::)

21
Type Conversions
■ Type conversions are either:
⚪ A narrowing conversion is one that converts an object to a type that
cannot include all of the values of the original type e.g., float to int.
■ May lose data.
⚪ A widening conversion is one in which an object is converted to a
type that can include at least approximations to all of the values of
the original type e.g., int to float.
■ Generally safe.

22
Type Conversions (cont.)
■ Type conversions may be:
⚪ Implicit (coercion).
⚪ Explicit (such as casting in C).
■ A mixed-mode expression is one that has operands of different types.
■ A coercion is an implicit type conversion.
■ In most languages, all numeric types are coerced in expressions, using
widening conversions.
■ In Java, byte and short are coerced to int whenever an operation is applied.

23
Explicit Type Conversions
■ Explicit Type Conversions:
⚪ Called casting in C-based language.
⚪ Examples:
■ C conversion (cast)

int a, b;
float x;
x = (float)a/(float)b;

24
Type Conversions:Errors in Expressions
■ Causes:
⚪ Errors resulting from narrowing conversions.
⚪ Errors resulting from limitations of computer arithmetic:
■ The result of an operation cannot be represented in the memory cell where it
must be stored.
■ Overflow Or Underflow: depending on result is too large or too small
⚪ Some languages (Ada, C++, Java) provide an exception handling mechanism that allows the
programmer to handle conditions such as divide by 0.
⚪ Often ignored by the run-time system.
⚪ Sometimes known as exceptions.

25
Relational and Boolean Expressions
■ Relational operators in various languages:
⚪ Ada: =, /=, >, <, >=, <=
⚪ Java: ==, !=, >, <, >=, <=
■ Relational operators always have lower precedence than arithmetic operators.
■ Relational Expressions:
⚪ Use relational operators and operands of various types.
⚪ Evaluate to some Boolean representation.
■ Boolean Expressions:
⚪ Operands are Boolean and the result is Boolean.
⚪ C/C++ operators: &&, ||, !

26
Relational and Boolean Expressions: No Boolean Type in C
■ C has no Boolean type, it uses int type with 0 for false and
nonzero for true.

27
Short-Circuit Evaluation
■ A short-circuit evaluation of an expression is one in which the result is
determined without evaluating all of the operands and/or operators.
■ Types:
1. In Arithmetic Expressions:
■ Ex.: (13*a) * (b/13–1)
If a is zero, there is no need to evaluate (b/13-1).
2. In logical Expressions:
■ Ex.: The value of (a >= 0) and (b < 10) is
independent of the second relational expression if a < 0.

28
Short-Circuit Evaluation (cont.)
■ Lack of short circuit evaluation can cause problems:
Index = 1;
while ((Index < listlen) && (list[Index] !=
key))
Index = Index + 1;

If the expression doesn’t have short-circuit, then both operands


to “&&” are evaluated.
■ C, C++, and Java: use short-circuit evaluation for the usual
Boolean operators (&& and ||).
29
Short-Circuit Evaluation (cont.)
■ Short-circuit evaluation exposes the problem of side effects in
expressions:
if ((a > b) || (b++/3))

If (a<=b), the second expression which increments b is not


evaluated.

■ Ada has short-circuit logical operators: and then and or else


if (index <= listlen) and then (list(index) /= key)

30
Assignment Statements
■ The general syntax:
<target_var> <assign_operator> <expression>
■ The assignment operator:
= FORTRAN, BASIC, PL/I, C, C++, Java.
:= ALGOLs, Pascal, Ada.
■ Simple assignment:
⚪ a = b;
⚪ a = b = c;
■ Suppose a, b, and c are integers.
■ In C, the integer value of c is assigned to b, which is in turn assigned to a
(multiple targets).

31
Assignment statements
■ Multiple targets:
Sum, Total = 0 (PL/I)
Sum = Total = 0 (C)
■ Conditional targets:
⚪ Perl allows it, for example:
flag ? count1 : count2 = 0;
⚪ Which is equivalent to:
if (flag) count1 = 0; else count2 = 0;
■ Compound assignment operators:
a += b; // a = a + b;

32
Assignment statements (cont.)
■ Unary assignment operators:
⚪ count++;
⚪ ++count;
⚪ sum = ++count; 🡺 count incremented then assigned to sum
⚪ sum = count++; 🡺 count assigned to sum then incremented

■ Assignment as an expression:
⚪ In C, C++, and Java the assignment statement produces a result and can
be used as operands.
while ((ch = getchar()) != EOF) { … }

ch = getchar() is carried out; the result (assigned to ch) is used as a


conditional value for the while statement.

33
Mixed-Mode Assignment
■ Assignment statements can also be mixed-mode, for example:
int a, b;
float c;
c = a / b;

■ In Pascal, integer variables can be assigned to real variables, but real


variables cannot be assigned to integers

■ In Java, only widening assignment coercions are done.

■ In Ada, there is no assignment coercion.

You might also like