loops - Loops Why a Loop This section will discuss three...

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

View Full Document Right Arrow Icon
Loops Why a Loop? This section will discuss three different kinds of loops in Java. Loops are structures that repeat the same set of actions over and over until a specified condition becomes false. To see why loops are useful, suppose we want to print the sum of 100 numbers entered by the user. With what we’ve seen so far, we’d have to write 100 separate prompts and input statements – this would be a big mess. Really, for each number we want to do exactly the same thing – print a prompt and read in the number. With a loop, we can repeat these two steps a certain number of times without having to write each one out separately. While Loops The easiest kind of loop is a while loop . This loop executes a set of instructions repeatedly until a given condition becomes false. Syntax Here is the syntax for a while loop: while (condition) { //statements } The condition is evaluated before anything inside the loop is executed. If the condition is false, we immediately skip to the code after the loop. If the condition is true, we execute the statements inside the loop, and then check the condition again. If the condition is false, we leave the loop. If it is still true, we execute the loop again. We repeat this process until the condition becomes false. Examples Here is how we can use a while loop to print the sum of 100 numbers entered by the user: //keep track of the sum of the elements we’ve seen so far int sum = 0; //keep track of how many elements we’ve asked for int count = 0; //keep looping while we haven’t asked for 100 elements Scanner s = new Scanner(System.in); while (count < 100) { //ask for the next number System.out.print(“Enter a number: “);
Background image of page 1

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

View Full DocumentRight Arrow Icon
int num = Integer.parseInt(s.nextLine()); //add the number to the sum we have so far sum = sum + num; //add one to our count (we’ve asked for one more number) count = count + 1; } //the loop is over – sum now holds the sum of 100 values //print the sum System.out.println(“The sum is: “ + sum); We can also put if-statements inside of loops (or loops inside of if-statements). In this example, we want to print the sum of 100 positive numbers. If the user enters a negative number, we want to print an error and not add the number to our total. int sum = 0; int count = 0; Scanner s = new Scanner(System.in); while (count < 100) { System.out.print(“Enter a number: “); int num = Integer.parseInt(s.nextLine()); //only add the number if it is positive if (num > 0) { sum = sum + num; count = count + 1; } //otherwise, print an error else { System.out.println(num + “ is not positive”); } } //the loop is over – sum now holds the sum of 100 values //print the sum System.out.println(“The sum is: “ + sum); Do-While Loops A do-while loop is similar to a while loop, but its condition is evaluated at the end of the loop instead of at the beginning. This means that a do-while loop will always execute at least once, but a while loop might not (because the condition might be false in the beginning).
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/02/2010 for the course CIS 200 taught by Professor Staff during the Spring '08 term at Kansas State University.

Page1 / 11

loops - Loops Why a Loop This section will discuss three...

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

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