MIME-Version: 1.0
Server: CERN/3.0
Date: Sunday, 24-Nov-96 22:48:14 GMT
Content-Type: text/html
Content-Length: 10237
Last-Modified: Wednesday, 16-Oct-96 13:58:58 GMT
rdz10
CS100B
Lecture 10
CONCEPTS --- last lecture
Compound statements and scoping rules; blocks and
declarations; fun with else;
iteration with while
and break; fencepost
errors
CONCEPTS --- this lecture
Additional C statements: for,
switch; type coercion and its perils
HANDOUT: Prelim #1 answers
Prelim #1 notes
- Review session is Sunday at 3:00
- Prelim is Monday 7:30-9:00 PM
- Coverage is: everything (but there will be no
detailed questions on the stack model)
- Closed book, closed notes, mostly for your benefit.
Bring a writing implement.
- We don't stress memorization
- We will forgive minor syntactic errors
- Room assignments: A-L Upson B17, M-Z Phillips
101
- Don't miss the prelim!
- Make up exams are oral, and guaranteed to be
no easier than the real one
Compound statements
- Another kind of C statement (we've actually seen
this already, but hadn't given it a name)
- This is what the curly braces {}
do, at least so far
- Sort of like begin and end in PASCAL
- Syntax: an open curly brace {,
then 0 or more statements, then a closed curly brace }{x
= 3;y = square(x);}
- A compound expression can include declaration
statements, but they must all appear at the beginning (i.e., before
any statement which is not a declaration statement).
- Example with declaration:{int
y;y = square(x);}
Scoping rules
- Nasty example (don't write code like this)void
main(){int x;x = 3;{int x;x =4;printf("%d",x);}}
- The rule is simple: to figure out which x
is intended, look outwards from the line you are on until you
find x.
- You can add this to the stack model, but it's
a little ugly and we won't do it
- We won't use this feature in anything very complicated
in CS100B
Where have we seen compound statements?
- Conditional statements often involve compound
statementsif (test)
thenstatement else elsestatement
- test
is an expression, but thenstatement
and elsestatement
can be any statement
- It is good practice to use compound statements
with conditional statements - it makes your code much easier to
read
- Where else have we seen compound statements?
- Hint: in this case, they usually involve declarations
Fun with else
- You don't actually need to use else
with ifif
(x != 0) y = 1/x;printf("%d",42);
- This can lead to totally unreadable code, especially
if you nest your if
statements. Don't do this!if
(x != 0) if (y != 0) z = x/y;else z = -1;
- There is a reasonable way of doing multiway conditional
statementsif (test1)
statement1else if (test2) statement2else if (test3) statement3
else
defaultstatement
- I don't personally find this incredibly readable,
but sometimes it's necessary
Iteration
- Today we'll mostly cover the syntax of iteration
in C
- We'll do some examples of its use in section
- Iteration involves executing some part of your
program repeatedly (as does recursion!)
- The easiest way to do iteration is with whilewhile
(expression) statement
- Typically, statement
is a compound statement
- semantics of while:
- evaluate expression.
- If it is false (zero), then we're done with the
while statement.
- If it is true (non-zero), then evaluate statement,
then evaluate the while statement again
Example
void countdown(int count){
while (count > 0) {printf("%d
",count);
count = count - 1;} printf("Blastoff!");}
void main(){ start = 10; countdown(start); printf("%d",start);}
- This example also shows off a subtle but important
property of the stack model
- Note that if the test expression is never 0,
your while statement will run forever
- How do we get out of a while
statement early?
while (count > 0) {printf("%d
",count);if
(NASA_goofed()) return; count = count - 1;}
Break statements
- Suppose that instead of getting out of the entire
countdown function we merely want to get out of the while statement
- NASA didn't goof, the astronauts got impatient
and decided to blast off before the countdown finished
- There is a good way and a bad way. We're only
going to teach you the good way.
- The break
statement terminates a while
statementvoid countdown(int
count){ while (count > 0) {printf("%d
",count);
count = count - 1;if (astronauts_bored(count)) break;} printf("Blastoff!");}
What about nested while statements?
- Again, this allows unreadable code to be created
- The world has enough of this without you folks
contributing!
- Small example:while(x
> 0){ x = x - 1; while (y < 0)
{ if (abs(x-y) < 10) break; } if (test(z)) break; }
- The rule is that a break
statement terminates the smallest while
statement that encloses it
Iteration and the stack model
- As far as the stack model is concerned, iteration
primitives aren't much different than conditional statements.
- They simply manipulate the program counter
- These primitives are sometimes called Control
Flow primitives
Fencepost errors
- With iteration (and also, with recursion) it
becomes very easy to make fencepost errors
- Suppose you want to build a 10' fence, and you
need to install posts which are 1' apart
- How many fenceposts do you need?void
nastybug(){int count;count = 10;while
(count > 0) { count = count - 1;something(1/count); }}
- This kind of bug is hard to find, in general