-
The following is a basic loop that prints the
numbers from 1 to LIMIT and finds their sum:
final int LIMIT = 100; // setup
int count = 1; // - initialize the loop control variable
int sum = 0; // - and the summing variable
while (count <= LIMIT) // loop control condition
{ // body
System.out.println(count); // -- perform task
sum = sum + count; // (print and sum)
count = count + 1; // -- update condition
}
System.out.println ("The sum of the integers from 1 to " +
LIMIT + " is " + sum);
This loop is an example of a count-controlled loop,
that is, a loop that
contains a counter (a variable that increases or decreases by
a fixed value -- usually 1 -- each time through the
loop) and that stops when the counter reaches a certain value.
- In this loop the println statement and the statement
to update the sum comes before the value
of count is incremented.
What would happen if you changed the order of
these statements so that count was incremented before the other
two statements? Specifically (fill in the blanks):
The loop would print and sum the numbers from _____________ to _____________.
- Assuming the order of statements is changed as in part (a) (with
the count incremented first), you can make two small changes
in the loop setup (in the initialization and/or the loop control, not
the body of the loop) to make the loop behave exactly as it did originially.
What are those changes?
- Not all
loops with counters are count-controlled; consider the example below,
which determines how many even numbers must be added together, starting at 2,
to reach or exceed a given limit.
final int LIMIT = 16; TRACE
int count = 1; sum nextVal count
int sum = 0; --- ------- -----
int nextVal = 2;
while (sum < LIMIT)
{
sum = sum + nextVal;
nextVal = nextVal + 2;
count = count + 1;
}
System.out.println("Had to add together " + (count-1) + " even numbers " +
"to reach value " + LIMIT + ". Sum is " + sum);
Note that although this loop counts how many times the body is executed,
the condition does not depend on the value of count.
- Trace this loop. That is, in the table next to the code
show values for variables
nextVal, sum and count
at each iteration (also show the initial values before the loop starts).
- Show what the code prints.
- Note that when the loop terminates, the number of even numbers
added together
before reaching the limit is count-1, not count.
How could you modify the code so that when the loop terminates, the
number of things added together is simply count?
-
Not all loops have counters. For example, if the task in the loop above
were simply to add together even numbers until the sum reached a certain
limit and then print the sum (as opposed to printing the number of things added
together), there would be no need for the counter.
Similarly, the loop below determines whether or not integers entered by
the user are divisible by 37; it contains no counter. The loop is controlled
by the user's answer to a "keep going?" question.
String keepGoing = "Y";
int nextVal;
while (keepGoing.equals("y") || keepGoing.equals("Y"))
{
System.out.print("Enter the next integer: "); //do work
nextVal = scan.nextInt();
if (nextVal % 37 == 0)
{
System.out.println (nextVal + " is divisible by 37.");
}
else
{
System.out.println (nextVal + " is NOT divisible by 37.");
}
System.out.print("Type y or Y to keep going: "); //update condition
keepGoing = scan.next();
}
System.out.println("Bye, hope this program was helpful!");
Not all counters are counting the number of times a loop
executes. Modify this loop so that it counts the
integers that are divisible by 37 and those that are not. Write your
changes on the code above as follows:
- Declare and initialize two counting variables -
one named count37 that
will count the number of integers divisible by 37 and the other
named not37 that will count the number not
divisible by 37.
- Update each variable in the appropriate place in the code.
- After the loop, print the number
of integers entered that were divisible by 37 and the number that weren't.
- This last loop continues as long as the user types in a Y or y in
response to the keep going question. It stops on any other input so
if the user accidentally hits a different key the loop will stop. An
alternate way to control the loop would be to stop only when the user
enters N or n. In this case the loop would continue if the user
accidentally entered something other than n or N.
Fill in a loop control condition that would cause the loop to STOP when
N or n is entered but continue on any other input.
while (______________________________________________________________________)
{
..... // Same code as before
System.out.print("Type n or N to stop: "); //update condition
keepGoing = scan.next();
}
- Write a while loop that will print "I love computer science!!"
100 times.
- Is the loop you just wrote count-controlled? _____________
- The code below is supposed to print the integers from 10 to 1 backwards.
What is wrong with it? (Hint: there are two problems!) Correct the code
so it does the right thing.
count = 10;
while (count >= 0)
{
System.out.println(count);
count = count + 1;
}