CS 100 Spring 2014
Lab 3 |
More Loops
|
Using the Processing language/environment answer the following four questions.
1. [4 pts] Consider the following program that uses a while
loop to print increasing numbers on the console and then do the same thing with
a for
.
println("counting up with a while loop"); int MAX = 5; // constant declaration used to terminate the loop int i = 1; // initialization of counter variable // i is the loop variable, incremented each time through the loop while (i <= MAX) { // continutation test: boolean expression // tested before continuing next iteration // print the counter followed by a space (no new line) print(i + " "); //counter i is updated i = i+1; } // change line with println println(""); println("done: out of the while loop"); println("---------------------------"); println("counting up with a for loop"); //Notice that the data type int is not in front of the variable i here //because i was declared above for(i=1; i<=MAX; i=i+1) { print(i + " "); } println(""); println("done: out of the for loop");
The synthax for a while
loop is
the following
initialization while (continuation test) { statements one statement updates the variable tested in the contintation test }where as the syntax of a
for
loop is
for (initialization; continuation test; update) { statements }Now that you understand
for
and while
loops, copy
the above code into a Processing program called loops
and do the following.
- Run the program and examine its output on the console.
- Change one line of the program to print from 1 to 10.
- Declare and use a variable to print starting at 3 for both the
while
andfor
loops. - Add two loops, one
while
loop and onefor
loop, below the provided code, such that each counts down from 12 to 0.
loops
should be the following
counting up with a while loop 3 4 5 6 7 8 9 10 done: out of the while loop --------------------------- counting up with a for loop 3 4 5 6 7 8 9 10 done: out of the for loop --------------------------- --------------------------- counting down with a while loop 12 11 10 9 8 7 6 5 4 3 2 1 0 done: out of the while loop --------------------------- counting down with for loop 12 11 10 9 8 7 6 5 4 3 2 1 0 done: out of the for loop
2. [4 pts] Using for
or while
, whichever you prefer,
write a program moreLoops
that produces on the console the output
below for Loop 1 and Loop 2. Write your code first on a piece
of paper, checking manually the loop iterations. (Loop 3 and Loop 4 are bonuses,
do them if you have time.)
Loop 1 0 3 6 9 12 15 18 Loop 2 20 15 10 5 0 Extra Loop 3 (power of two) 1 2 4 8 16 32 64 Extra Loop 4 (sum of i [starts at 1 and stops after 10 iterations]) 0 1 3 6 10 15 21 28 36 45
3. [4 pts] Copy the following code into a program called manyEllipses
and
run it.
size(400, 400); background(255); smooth(); fill(random(128, 255), random(128, 255), random(128, 255)); ellipse(random(width), random(height), 50, 50); fill(random(128, 255), random(128, 255), random(128, 255)); ellipse(random(width), random(height), 50, 50);
Add a loop to create many ellipses, the result is an image like the one below.
4. [4 pts] Write a program called dice
that creates
a 300 x 300
window and initializes the variables
int dotSize = 25; int dotX = 40; int dotY = 40; int incX = 50; int incY = 50;
which are used in a loop to produce the image below.
Changing the window size to 400 x 400
produces
this image.
Bonus
Create some graphical patterns using loop.
For example using the values of Loop 3 above for the rectangle width the following image is produced.