Lecture #10: Operators, Expressions, and
Statements
Bob
Pitha
equality operators
== !=
Test
whether operands are equal or not equal
Return
1 or 0
precedence and associativity
equality operator issues
Dont count on testing floating-point types with ==
why?
real numbers
in the computer can differ by an insignificant amount
== tests for exactly equal
so instead,
test the difference between them

important!
Dont
confuse == with =

the worst
part is, this is legal C code; it just doesnt do what you (probably) want
the
expression i = 0
it sets the
value of i to 0
its value is
0
the
expression i == 0
doesnt set
the value of anything
its value is
1 or 0 depending on whether i is 0
This can
cause bugs that are very hard to find!
logical operators
&&
||
Logical
AND and OR
(a && b)
is true if both a
and b
are true
(a || b) is true
if either a
or b are
true, or both.
Evaluation
stops when truth value is known
((a != b) && (c <= d))
precedence and associativity

expressions
what is an expression?
Nobody
has a good, clear definition.
A
sequence of operators and operands that has a value and/or does something
interesting
There
are a bunch of types
how are expressions used?
As
operands in larger expressions

As
parts of statements

side effects in expressions
Some
operators have side effects
change the
value of their operands
You
must be careful not to combine side effects
no
expression should modify the same object twice

weve finally finished chapter 2!
2.1
Variable Names
2.2
Data types
2.3
Constants
2.4
Declarations
2.5
Arithmetic operators
2.6
Relational operators
2.7
Type conversions
2.8
Increment/decrement operators
2.10
Assignment operators
2.12
Precedence
statements
whats a statement?
A
specification of some action to be performed
Executed
sequentially
what kinds of statements do we have?
Expression
statements
Compound
statements
Selection
statements
Iteration
statements
Jump
statements
what kinds of statements do we have?
Expression statements
Compound
statements
Selection
statements
Iteration
statements
Jump
statements
expression statements
Any
expression can be a statement
just stick a
semicolon (;) on the end
but theyre
only evaluated for their side effects
the
expressions value is ignored

expression statements
Since
the value is ignored, expressions that only have a value are not meaningful
statements
theyre not
illegal, just useless
some
compilers will warn you about them
the
assumption is that you forgot something

Special
case: the null statement
an empty
statement just a semicolon
used when
the syntax requires a statement, but you dont want to do anything.

what kinds of statements do we have?
Expression
statements
Compound statements
Selection
statements
Iteration
statements
Jump
statements
compound statements
Also
called statement blocks
Groups
one or more statements into a unit
syntactically
equivalent to a single statement
that means
that wherever its legal to have a single statement, you can have a compound
statement.
Can
have declarations and initializations
but only at
the beginning of a block
compound statement syntax
Statements
surrounded by braces { }
no semicolon
at the end
although the
statements and declarations in the block use semicolons as normal
use of compound statements
Impossible
to use C without using compound statements
Weve
already seen them a lot
