|
Reading Loops Versus Counting Loops
Typcially programmers use one of two general types of loops -- counting loops and reading loops. A counting loop loops a specific number of times. For example, the loop below always executes exactly ten times:
int i = 1;
while (i <= 10)
{
// ... do something here ...
i++;
}
A reading loop is a loop that is reading input from an outside source, such as from a file, from the keyboard, or from a random number generator. Usually, these loops keep going until the end of the input data is reached, or until a certain value is reached. For example, the following loop reads integers from the user until the value 100 is entered:
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
while (i != 100)
{
// ... do something here ...
i = scan.nextInt();
}
Most problems that need a loop will use one or the other of these two general foorms. Just start with the above code and fill in the "body" of the loop (i.e., the part labeled "// ... do something here ... " in the above examples). The body might be quite simple, such as a print statement, or fairly complex with if-statements, method calls, etc.
With the idea that people learn better by solving problems, the next several pages contain a series of looping questions. For each of these questions, always use a loop where a loop would be appropriate (i.e., don't write five nearly identical print statements if one print statment in a loop could have generated the same output). The following problems involve either a single loop, or two or more SEPERATE loops (i.e., one loop after another, but no loop-inside-a-loop solutions are required in the problems below).
Click here for details on when to use a counting loop versus a reading loop.
When asked to solve a programming problem, it is extremely useful for novice programmers to follow
these steps:
-
Get a complete description of the problem.
-
Get a sample run of the program that shows the exact expected output.
-
Make an educated guess at the variables you will need to solve the problem -- give each variable a name and draw a picture of each variable. Fill in values from the sample run.
-
Describe your solution to the problem in a few simple English statements. Look at what you have written to try to determine the structure of the problem (look for works like "loop" and "if").
-
Write the Java code for the problem, solving one simple piece at a time (compile and run each simple part of the solution before moving on to the next piece of code).
The first two steps are already supplied in the problems below (the description and a sample output). Perform steps 3, 4 and 5 for each of the problems (at the bottom of the page is a link to solutions to some of the problems shown below):
-
Write a Java application to print the following to the console:
4
6
8
10
12
14
-
Write a Java application to print the following to the console:
100
150
200
250
300
350
-
Write a Java application to print the following to the console:
1
2
3
4
5
1000
1010
1020
1030
1040
-
Read a series of positive integers from the keyboard. Keep reading until the user enters a negative number. Report the sum of the numbers.
For example, if the user enters 10, 33, 6, and -1, then the output would be:
10
33
6
-1
Sum is: 49
-
Read a series of positive integers from the keyboard. Keep going until the user enters a negative number. For each number read, print both the number and twice the number.
For example, if the user enters 10, 33, 6, and -1, then the output would be:
10
10 and 20
33
33 and 66
6
6 and 12
-1
-
Read a series of positive integers from the keyboard. Keep reading until the user enters a negative number. Report the total number of integers read in:
10
33
6
-1
There were 3 numbers.
-
Read a series of positive integers from the keyboard. Keep reading until the user enters a negative number. Report the number of odd values entered (use %2 to determine if a value is odd or even).
10
33
6
47
90
21
88
-1
There were 3 odd numbers.
-
Read exactly seven integers from the user. Report the sum of the numbers and the number of even numbers. Also report the sum of the odd numbers and the sum of the even numbers.
12
30
60
31
90
11
15
Even numbers: 4
Even sum: 192
Odd numbers: 3
Odd sum: 57
-
Generate ten random numbers in the range 1 to 100. Report the number of values that were 50 or greater (print them to the console).
76
52
95
80
-
Generate random numbers in the range 1 to 10. Keep generating the numbers (and printing them out) until the value seven is generated. Stop the program once a seven is generated.
4
2
9
2
8
-
Generate random numbers in the range 1 to 10. Keep generating the numbers (and printing them out) until the value seven is generated. Stop the program once a seven is generated. Report the number of numbers generated and rint the sum of those numbers.
9
3
There were 2 numbers, for a total of 12.
Solutions to selected exercises.
Some Extra Problems
If you are eager to try a few more looping problems, then here are a few more.
The for Loop
The number of iterations of a counting loop can be determined from its starting value, ending value, and the amount size of the increment. For example, this loop starts at one, ends at ten, and increments by one -- it will have exactly ten iterations:
int i = 1; // start at 1
while (i <= 10) // end at 10
{
// ... do something here ...
i++; // increment by 1
}
A common shorthand for a counting loop is the for loop. The for loop pulls the starting, ending, and increment values onto one line:
for (int i = 1; i <= 10; i++)
{
// ... do something here ...
}
Even though these three features appear on one line, they take their affect exactly in the places shown in the while loop at the start of this section. This for loop behaves exactly like the while loop shown above. Experienced programmers often use the for loop because it compactly describes the core features of a counting loop.
|