Presentation is loading. Please wait.

Presentation is loading. Please wait.

While and Do While Syntax, semantics and examples

Similar presentations


Presentation on theme: "While and Do While Syntax, semantics and examples"— Presentation transcript:

1 While and Do While Syntax, semantics and examples
Copyright © , Curt Hill

2 The two simple loops Either the while or do while is simpler than the for However, all loops need all the pieces: Initialization Test Body of the loop Advancement From a programming perspective very similar Copyright © , Curt Hill

3 Syntax of while The form is: while(condition) one statement
While is a reserved word Condition is a boolean of the same type seen elsewhere The one statement is any statement Most often a compound statement Unlike the for the while almost never has an empty statement here Copyright © , Curt Hill

4 Syntax of do while The form is: do one statement while(condition);
Do is also a reserved word Condition is a boolean of the same type seen elsewhere Evaluated at end of loop One statement is most often a compound statement Copyright © , Curt Hill

5 Flowchart Representation
Leading decision loop such as a while Trailing decision loop such as a do while Copyright © , Curt Hill

6 Why Both? The while is used instead of a for when the advancement issue is fuzzy The do is used when the initialization becomes part of the loop body The do is the least used loop Never the less, the experienced programmer uses all three in their code Let’s consider some examples Copyright © , Curt Hill

7 While Revisited Suppose a cash register machine program
We take in the purchase price and the amount the customer has given us Compute the change Make the change in terms of numbers of bills and coins the teller has to give Try first part of this with a while Copyright © , Curt Hill

8 Cash Register Program double charge, tender; … // get charge, tender
double change = tender-charge; int twenties = 0; while(change >= 20.00){ change -= 20; twenties++; } String s; if(twenties>0){ s = “Give them “ +twenties+ “ twenties.”; Copyright © , Curt Hill

9 Example Revisited The loop would be duplicated for tens, fives, etc.
Both the while and for are leading decision loops They may execute the body of the loop zero times, if the condition is false This is most common form of loop This is desirable here, we may not give them one of some bill denomination Leading decision loops must initialize before the first test Copyright © , Curt Hill

10 Another Situation Sometimes we end up in duplication in initialization and body of loop Consider the case of reading in the age of a hospital patient, that we attempt to verify int age; age = scan.nextInt(); while(age > 120){ System.out.println( “Bad age entered”); age = scan.nextInt(); } Copyright © , Curt Hill

11 Another Loop This type of situation may be best handled with a trailing decision loop This checks the condition at the end of the loop rather than the beginning Particularly handy for when the initialization is part of the loop processing Consider this with a do while Copyright © , Curt Hill

12 Consider Example Again
Get a Hospital patient’s age: int age; do { System.out.println( “Enter age”); age = scan.nextInt(); } while(age > 120); The loop reads and verifies Copyright © , Curt Hill

13 One last thought Some loops are predictable and others not
For example: for(int i = 0;i<100;i++)… for(Pixel ap: p.getPixels()) … Prior to the first execution of these two we can say precisely how many times they will execute These two are deterministic Copyright © , Curt Hill

14 In contrast The do while: do { System.out.println( “Enter age”); age = scan.nextInt(); } while(age > 120); Is unpredictable The cash register while is also non-deterministic Copyright © , Curt Hill

15 Determinism Counting loops usually deterministic, use a for
Most often use whiles or do whiles for non-deterministic loops There are several other things we think about with loop choice as well However the while and for are equivalent in that any for can be converted to a while and the reverse Copyright © , Curt Hill

16 Conclusion There is more to learn about the three loops
There are also more presentations: Logical loop types Using break and continue Copyright © , Curt Hill


Download ppt "While and Do While Syntax, semantics and examples"

Similar presentations


Ads by Google