DOC PREVIEW
FIU COP 2210 - The while Statement

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

The while StatementComputer Programming I Instructor: Greg ShawCOP 2210 The while Statement I. Java LoopsJava has three different statements that implement repetition(aka: "looping" or "iteration"). Of these, the while statementis the most important because it is the most general-purpose.It can be used in any situation where a loop is needed.In fact, the other types of loops (i.e., the for statement andthe do-while statement) are merely special cases of the while.II. Syntax of the while Statement while (boolean expression){statement(s)} where, boolean expression = anything that evaluates to true orfalse. statements = any number of Java statements1. the parentheses are required, and2. there is no semi-colon after the closing parenthesisIII. Execution1. The boolean expression is evaluated.2. If it is true, then the loop body is executed and the processis repeated from step 1 (i.e., the boolean expression isevaluated ; if true, then the loop body is executed andthe process is repeated, etc.)Otherwise (if it is false), the loop is terminated. I.e.,the loop body is skipped and control passes to thestatement following the closing brace. The loop body will be skipped entirely (i.e., executed zerotimes) if the boolean expression is false the first timeit is tested. IV. The Loop Control Variable (LCV)Well-constructed loops test the value of a variable in theboolean expression. This variable is commonly known as theloop control variable (lcv), because its value determineswhether or not another iteration will be doneV. The Three Loop NecessitiesEvery self-respecting loop -- whether while, for, or do-while-- will meet these three requirements:1.The LCV must be initialized before it is tested.2.The LCV must be tested in the boolean expression.3.The value of the LCV must be changed in the loop body(otherwise, you get an "infinite loop"). VI. Example// print all positive powers of 2 that are <= 1000int limit = 1000 ;int power = 1 ; // start with 2^0 = 1while (power <= limit){System.out.print(" " + power) ;power = power * 2 ; // get next power} // question: what is the loop control variable in this// example?VII. Another ExampleA common use of the while loop is to force (or “coerce”) validinput. We have all seen one of these loops in action, any timewe enter invalid data – such as an incorrect password – into anonline form.Here’s the algorithm:get the data from userwhile (data are unacceptable){print error messagemake user enter data again}Sample code that implements this algo is on the next page...// get a int from 1 to 10String input = JOptionPane.showInputDialog( "Enter a number from 1 to 10") ;int number = Integer.parseInt(input) ;boolean goodData = number >= 1 && number <= 10 ;while ( !goodData ){JOptionPane.showMessageDialog(null, "Sorry, I need a number " + "between 1 and 10!\nYou entered: " + number) ;input = JOptionPane.showInputDialog( "Enter a number from 1 to 10") ;number = Integer.parseInt(input) ; goodData = number >= 1 && number <= 10 ;}// here when goodData is true (i.e. number is between 1 and 10)JOptionPane.showMessageDialog(null, "You entered " + number + "\n\nThanks, genius!") ;Questions:1. What is the loop control variable in this example?2. What happens if the user enters a “good” value the


View Full Document
Download The while Statement
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view The while Statement and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view The while Statement 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?