Principles of Programming Language - Chapter 7
Principles of Programming Language - Chapter 7
7.1 – Introduction
(Paraphrased clearly for beginners in Google Docs–ready format)
Expressions
Assignment Statements
(Example: a+b*c )
Chapter 7 1
3. Side Effects
4. Overloaded Operators
5. Mixed-Mode Expressions
What happens when you mix data types (like int + float )?
6. Type Conversions
How does the program handle changing one data type into another?
8. Short-Circuit Evaluation
9. Assignment Statements
Chapter 7 2
7.2 Arithmetic Expressions
What Is an Arithmetic Expression?
An arithmetic expression is a combination of:
✅ Examples:
a+b*c
(x + y) / 2
7.2.1 Operators
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
** Exponentiation (in some languages like Python)
% Modulus (remainder, in C-like languages)
Chapter 7 3
Precedence determines the order in which operations are performed when
there are multiple operators.
✅ What Is Associativity?
Associativity decides the direction of evaluation when operators have the
same precedence.
✅ Example:
a+b*c
(a + b) * c
Chapter 7 4
A side effect happens when evaluating an operand changes something
else in the program (like modifying a variable or doing input/output).
Example:
x = 1;
a = (x++) + (x++);
✅ Simple Example:
int a = 5, b = 3;
float x = 2.5, y = 1.5;
Chapter 7 5
7.3.1 Operator Overloading in Programming
Languages
🔹 Java:
System.out.println(3 + 4); // 7
System.out.println("hi" + "bye"); // "hibye"
🔹 C++:
C++ also allows custom operator overloading, where programmers can
define how an operator works for user-defined types (like classes).
Chapter 7 6
class Complex {
public:
int real, imag;
Complex operator + (const Complex& other) {
Complex result;
result.real = real + other.real;
result.imag = imag + other.imag;
return result;
}
};
✅ Advantages:
Makes code simpler and more readable
You can use familiar operators for new types (like vectors or complex
numbers)
❌ Disadvantages:
Too much overloading can make code confusing
The same operator might do very different things, which can lead to
unexpected behavior
✅ Language Comparison:
Language Overloading Allowed?
Chapter 7 7
Language Overloading Allowed?
✅ Example:
int a = 5;
float b = 2.5;
float result = a + b; // a is automatically converted to float
7.4.1 Coercion
✅ What Is Coercion?
Coercion is an automatic (implicit) type conversion done by the compiler
or interpreter.
You don’t have to write extra code — the system figures it out.
✅ Example:
int x = 3;
float y = 3.5;
Chapter 7 8
float z = x + y; // x is coerced to float
✅ What Is Casting?
Casting is when the programmer manually converts one type into another.
You use special syntax to tell the compiler exactly what you want.
✅ Example in C:
int a = 5;
float b = (float)a; // a is explicitly cast to float
To control precision
✅ Java Example:
double x = 9.75;
int y = (int)x; // y becomes 9 (decimal part is lost)
Chapter 7 9
7.4.3 Mixed-Mode Expressions
int a = 4;
float b = 2.0;
float result = a + b;
In such cases:
✅ Order of Promotion
Type Promotion (Typical Order)
char
int
float
double
Chapter 7 10
✅ Best Practices
Avoid mixing too many types in one expression.
✅ Example:
int a = 10, b = 20;
bool result = (a < b); // true
or false .
Relational operators
Chapter 7 11
Logical (Boolean) operators
` ` Logical OR
! Logical NOT !true false
A B A && B
A B A || B
A !A
true false
false true
Chapter 7 12
Language Notes on Boolean and Relational
Expressions
✅ C and C++
The result of a relational expression is an integer:
0 → false
✅ Java
Has a true Boolean type ( boolean )
✅ Python
Uses True and False (capitalized)
✅ Example:
int x = 10, y = 20;
if (x < y && y < 30) {
// condition is true
}
Chapter 7 13
Short-circuit evaluation is a technique used in Boolean expressions.
How It Works
A && B
✅ Logical OR ( || )
In an expression like:
A || B
Chapter 7 14
This avoids a divide-by-zero error.
✅ Benefits:
1. Improves Efficiency
2. Prevents Errors
Avoids operations that would crash (like dividing by zero or null pointer
access).
Operator Meaning
or else Short-circuit OR
Chapter 7 15
What Is an Assignment Statement?
An assignment statement is used to give a value to a variable.
✅ General Format:
variable = expression
The expression is evaluated, and its result is stored into the variable.
✅ Example:
x = 3 + 4; // x gets the value 7
✅ C/C++/Java Example:
int x;
x = 5;
x = x + 1; // x becomes 6
Chapter 7 16
✅ Functional Language Behavior:
x=5
-- x cannot be reassigned to a different value
Multiple Assignments
✅ Chained Assignment
Some languages support assigning multiple variables at once:
a=b=c=0
✅ Parallel Assignment
In some languages (like Python), you can assign multiple values at the
same time:
a, b = 1, 2
a becomes 1 , b becomes 2 .
Assignment as an Expression
In languages like C and C++, assignment statements can also be used as
expressions that return a value.
✅ Example:
x = (y = 5); // y is set to 5, then x is set to 5
Chapter 7 17
⚠️ Risk:
Can make code hard to read or debug, especially if chaining assignments.
✅ Common Operators:
Operator Meaning Example Equivalent To
+= Add and assign x += 2 x=x+2
✅ Example:
int x;
float y = 3.5;
Chapter 7 18
x = y; // assigning a float to an int
❌ Potential Problems:
1. Loss of Precision
2. Unexpected Behavior
3. Language Differences
✅ Example in Java:
int a;
double b = 7.9;
a = (int) b; // manual casting: a gets 7
Here, the cast is explicit, so the programmer is clearly asking for a type
conversion.
Chapter 7 19
Type Conversion Rules Vary by Language
Best Practices
✅ Summary
Mixed-mode assignment involves storing a value of one type into a
variable of another type.
You should always be careful when mixing types to avoid logic errors.
Chapter 7 20
Type evaluation is the process of figuring out the type of an expression or
variable.
✅ Why Is It Important?
The correct type affects:
This allows:
This allows:
More flexibility
Chapter 7 21
✅ For Simple Variables
The type is either:
int x; // x is an integer
✅ For Expressions
The type depends on:
Example:
int a = 3;
float b = 2.5;
float c = a + b; // a is promoted to float; result is float
✅ Type Checking
Type evaluation is closely related to type checking, where the compiler
ensures that:
Type Evaluation
Language Notes
Style
Chapter 7 23