Skip to main content

Section 6.1 Worked Example: Writing Loops

Subgoals for Writing a Loop.

  1. Determine purpose of loop

    1. Pick a loop structure (while, for, do_while)

  2. Define and initialize variables

  3. Determine termination condition

    1. Invert termination condition to continuation condition

  4. Write the loop body

    1. Update Loop Control Variable to reach termination

You can watch this video or read through the content below it.

Problem 1: Write a loop that will prompt the user to continue to enter numbers until a sentinel value is entered and find the maximum value entered.

Subsection 6.1.1 SG1: Determine purpose of loop

Generally, while-loops are a good choice for sentinel loops.

Subsection 6.1.2 SG2: Define and initialize variables

We will need variables to store the most recent input value, as well as the maximum value so far.

We will also need a Scanner to accept user input, as well as a priming read.

Scanner get = new Scanner(System.in);
int value;
int maxSoFar;
System.out.println("Enter a value, enter -1 to end");
value = get.nextInt();
while (                )
{

}

Subsection 6.1.3 SG3: Define termination condition invert to continuation condition

Scanner get = new Scanner(System.in);
int value;
int maxSoFar;
System.out.println("Enter a value, enter -1 to end");
value = get.nextInt();
while ( value != -1  )
{

}

Subsection 6.1.4 SG4a: Update condition (LCV) to reach termination

Scanner get = new Scanner(System.in);
int value;
int maxSoFar;
System.out.println("Enter a value, enter -1 to end");
value = get.nextInt();

while ( value != -1  )
{
   System.out.println("Enter a value, enter -1 to end");
   value = get.nextInt();
}

Subsection 6.1.5 SG4: Write loop body

Scanner get = new Scanner(System.in);
int value;
int maxSoFar = -1;
System.out.println("Enter a value, enter -1 to end");
value = get.nextInt();

while ( value != -1  )
{
    if (value > maxSoFar)
        maxSoFar = value;
    System.out.println("Enter a value, enter -1 to end");
    value = get.nextInt();
}
if (maxSoFar > -1)
    System.out.println("The maximum is " + maxSoFar);
else
    System.out.println("No values entered");

Problem 2: Write a loop that will add up 1 + 1/2 + 1/4 + 1/8 + 1/16 + … + 1/1024

Subsection 6.1.6 SG1: Determine purpose of loop

Because we know the number of iterations, we could choose a for-loop, but for this exercise we will go with the general purpose while-loop.

Subsection 6.1.7 SG2: Define and initialize variables

double sum = 0;
double term = 1.0;
while (        )
{

}
System.out.println(sum);

Subsection 6.1.8 SG3: Define termination condition invert to continuation condition

The denominators are increasing powers of 2. The first denominator, if you think of 1 as 1/1, is 2 raised to the power of 0. The final denominator, 1024, is 2 raised to the power of 10, so we will iterate from zero to ten.

double sum = 0;
double term = 1.0;
int i = 0;
while ( i <= 10 )
{

}
System.out.println(sum);

Subsection 6.1.9 SG4: Write loop body

double sum = 0;
double term = 1.0;
int i = 0;

while ( i <= 10 )
{
   sum = sum + term;
   term = 0.5 * term; //same as divide term in half
   i++;
}
System.out.println(sum);

Practice Pages.

You have attempted of activities on this page.