Chapter 07 Expressions and Assignments
Chapter 07 Expressions and Assignments
EXPRESSIONS AND
ASSIGNMENT
STATEMENTS
Spring 2022
Where are
we ?
We have studied Names and Types
2
a* ( c mod 3 )
CSC 461 May 1, 2025
Conditional
10
Expressions
Conditional Expressions
C-based languages (e.g., C, C++)
An example:
average = (count == 0)? 0 : sum / count
narrowing conversion:
Converts an object to a type that cannot include all of the
values of the original type
e.g. float to int
widening conversion
Converts an object to a type that can include at least
approximations to all of the values of the original type
e.g. int to float
Relational Expressions
Use relational operators and operands of
various types
Evaluate to some Boolean representation
Operator symbols used vary somewhat
among languages (!=, /=, .NE., <>, #)
Boolean Expressions
Operands are Boolean and the result is
Boolean
Example operators
Reason:
a && b if a is false, then result is
false
a || b if a is true, then result is true
Example
( a>2) || ( b/13+c*23+d >1024)
Which is equivalent to
if (flag)
total = 0
else
subtotal = 0
a = a + b
is written as
a += b
CSC 461 May 1, 2025
Assignment
Statements:
28
Unary Assignment
Operators
Unary assignment operators in C-based
languages combine increment and
decrement operations with assignment
Examples
sum = ++count (count incremented, added to
sum)
sum = count++ (count incremented, added to
sum)
count++ (count incremented)
-count++ (count incremented then negated)
CSC 461 May 1, 2025
Assignment as an
29
Expression
In C, C++, and Java, the assignment
statement produces a result and can be
used as operands
An example:
while ((ch = getchar())!= EOF){…}