Intro to Java Web-Notes_Part30

Intro to Java Web-Notes_Part30 - Lo Home - About Us Reading...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the 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 specific 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 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.nextlnt(); while (i I: 100) i // ... 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 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: 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 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): 1. Write a Java application to print the following to the console: 4 6 8 10 12 14 2. Write a Java application to print the following to the console: 100 150 200 250 300 350 3. Write a Java application to print the following to the console: ubme 1000 1010 1020 1030 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 33 6 —1 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

Page1 / 2

Intro to Java Web-Notes_Part30 - Lo Home - About Us Reading...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online