This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Lo Home - About Us 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 speciﬁc number of times. For example, the loop below always executes exactly ten times: int i : 1;
while (i <: 10) i
// ... do something here ... i++;
l A reading loop is a loop that is reading input from an outside source, such as from a ﬁle, 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.nextlnt();
while (i I: 100)
// ... do something here ...
i = scan.nextlnt(); l Most problems that need a loop will use one or the other of these two general foorms. Just start with the above code
and ﬁll 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 ﬁve
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: 1. Get a complete description of the problem. 2. Get a sample run of the program that shows the exact expected output. 3. 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. 4. 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‘). 5. 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 ﬁrst 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): 1. Write a Java application to print the following to the console: 4
14 2. Write a Java application to print the following to the console: 100
350 3. Write a Java application to print the following to the console: ubme 1000
1040 4. 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
Sum is: 49 5. 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 6. 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: ...
View Full Document
- Summer '08