Outline for January 23, 2012
Reading:
§5
Decision structures
If statement
Executes once, based on condition
Syntax
Conditions
Resolves to boolean value
Literal booleans:
True
,
False
Testable as true or false
Relational operators
Use two arithmetic expressions to create a boolean
>
,
>=
,
<
,
<=
,
==
,
!=
Resolved after arithmetic operators
6 > 2
;
"UCD" == "Sac State"
Two-way decisions
if-else statements
One condition, two possible code blocks
Syntax
else very powerful when the positive condition is easy to describe but not the negative
String comparison example
Multi-way decisions
Can execute code based on several conditions
elif (else if) introduced
Syntax
else only reached if all previous conditions false
Nested if statements
A PDF version is available here.
ECS 10, Basic Concepts of Computing
Winter Quarter 2012