Computer Science 111:

Introduction to Computer Science I

Chapter Six

Gregory M. Kapfhammer


creative commons licensed ( BY-NC-SA ) flickr photo shared by Ed Yourdon

Switch Statements

An alternative to using if statements

Cannot encode complex boolean expressions

Sometimes simpler for a programmer to understand

See Section 6.1 for more details!

Do statements

Comparing while and do-while loops

What is the key difference?

How do pick between these options?

Can you draw a picture to visualize their execution?

For Statements

When is it best to use a while loop?

What if we know the number of iterations?

We should try to use the for loop!

What is the structure of a for loop?

Finding Multiples

Run the git pull command!

Compile the Multiples.java program

Run this program like it was on page 282

Try five different inputs for this program

How does this program use for loops?

Printing Stars

Find the Stars.java program

Can you compile and run this program?

What output does it produce?

Change the MAX_ROWS variable

Recompile the program

How does the output change?

Iteration Constructs

Three basic loop statements are functionally equivalent

What if we want to run the loop body at least one time?

What if we know the number of times iteration will occur?

What if we want to use an Iterator?

Make sure that you understand the trade-offs!