In this reading you’ll examine a second type of loop, the while
loop. while
loops will give us more control over how many times the loop body executes than the “For-Each” loop. With this added control, we’ll be able to do things like modify only a subset of Pixels in a picture. We’ll need to be careful when using while
loops though, as they are prone to infinite looping.
Sections 4.3.2-4.3.4 (pp. 93-110) from the course textbook.
Each student will be responsible for learning and demonstrating proficiency in the following objectives PRIOR to the class meeting. The reading quiz will test these objectives.
while
loop, describe when it will stop executing.++
and --
operators do.The following objectives should be mastered by each student DURING and FOLLOWING the class session through active work and practice.
while
loop that modifies a Picture, drawing a memory model as you trace.These exercises are geared towards mastering the BASIC learning objectives listed above. You are expected to submit them before class and it is highly recommended that you complete them before attempting the reading quiz.
How many times will the loop body of the following loop execute?
int i = 0;
while (i < 10)
{
System.out.println("Howdy, partner!");
i++;
}
If we replace the i++
in the code above with “i = i + 2”, how many times would the loop body execute?
Part A: What is printed after executing the following code?
int n = 0;
// n = n + 2;
System.out.println(n);
n++;
/*
System.out.println(n);
*/
Part B: Explain why the code in Part A printed what it did.
Download this cupcake picture to your computer. Use the Picture Explorer in DrJava to determine the RGB values at coordinate (300, 360).