{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Conditionals and Loops(2) - Chapter 5 Conditionals and...

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

View Full Document Right Arrow Icon
Chapter 5: Conditionals and Loops Lab Exercises Topics Lab Exercises The while statement PreLab Exercises Counting and Looping (submit) A Guessing Game (submit) The for statement Finding Maximum and Minimum Values (extra credit) Using the Coin Class (submit) Chapter 5: Conditionals and Loops 1
Background image of page 1

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

View Full Document Right Arrow Icon
Prelab Exercises Section 5.5 In a while loop, execution of a set of statements (the body of the loop) continues until the boolean expression controlling the loop (the condition ) becomes false. As for an if statement, the condition must be enclosed in parentheses. For example, the loop below prints the numbers from 1 to to LIMIT: final int LIMIT = 100; // setup int count = 1; while (count <= LIMIT) // condition { // body System.out.println(count); // -- perform task count = count + 1; // -- update condition } There are three parts to a loop: T The setup , or initialization . This comes before the actual loop, and is where variables are initialized in preparation for the first time through the loop. T The condition , which is the boolean expression that controls the loop. This expression is evaluated each time through the loop. If it evaluates to true, the body of the loop is executed, and then the condition is evaluated again; if it evaluates to false, the loop terminates. T The body of the loop. The body typically needs to do two things: T Do some work toward the task that the loop is trying to accomplish. This might involve printing, calculation, input and output, method calls—this code can be arbitrarily complex. T Update the condition. Something has to happen inside the loop so that the condition will eventually be false— otherwise the loop will go on forever (an infinite loop). This code can also be complex, but often it simply involves incrementing a counter or reading in a new value. Sometimes doing the work and updating the condition are related. For example, in the loop above, the print statement is doing work, while the statement that increments count is both doing work (since the loop's task is to print the values of count) and updating the condition (since the loop stops when count hits a certain value). The loop above 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. 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.
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.

{[ snackBarMessage ]}