Cps 150- loops provide a mechanism for repeating a block of


Lab Project: The while Loop

Lab 1

Loops provide a mechanism for repeating a block of code called the loop body. We begin this lab by experimenting with while loops, the simplest form of loop code. Many loops are controlled with a single variable, which we will refer to as the loop control variable or the loop index.

Consider the code below. What is the output the program produces? Try changing the assigned value (6) of the limit variable to various integer values (e.g., 10, 100, 0, -10). What happens?

/**
A simple program that prints a loop control variable.
*/
public class SimpleLoop
{
public static void main(String[] args)
{
inti = 0;
int limit = 6;
while (i< limit)
{
System.out.println("i = " + i);
i++;
}
}
}

Lab 2

Consider the code below again. What happens if you comment out the line that increments i (line 13)? Will the program ever stop looping?

/**
A simple program that prints a loop control variable.
*/
public class SimpleLoop
{
public static void main(String[] args)
{
inti = 0;
int limit = 6;
while (i< limit)
{
System.out.println("i = " + i);
i++; // line 13
}
}
}

Lab 3

Manipulating the loop control variable is a critical skill in learning to write code with loops. Modify the program in Lab 1 so that it produces the following output:

i = 6
i = 8
i = 10
i = 12
i = 14
i = 16
i = 18
...
i = 98

What Do I Hand In?

Once you are done, submit your answers for Lab 1 and Lab 2, and upload the completed source code file (i.e., .java file) for Lab 3.

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Cps 150- loops provide a mechanism for repeating a block of
Reference No:- TGS02240318

Now Priced at $35 (50% Discount)

Recommended (96%)

Rated (4.8/5)