While and Do While Syntax, semantics and examples

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Chapter 5: Control Structures II (Repetition)
Loops – While, Do, For Repetition Statements Introduction to Arrays
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 5 Loops.
Copyright Curt Hill A Quick Introduction to Looping Breadth not Depth.
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Copyright © Curt Hill The C++ IF Statement The most important decision statement Part 1.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © Curt Hill Flow of Control A Quick Overview.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Repetition Statements b Repetition statements allow us to execute a statement multiple times repetitively b They are often simply referred to as loops.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
The for Statement A most versatile loop
Repetition Statements
Slides by Evan Gallagher
Slides by Evan Gallagher
Lecture 4b Repeating With Loops
Chapter 4 – C Program Control
Flow of Control An Overview
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Unit 3 Lesson 9 Repetition Statements (Loops)
Chapter 5: Control Structures II (Repetition)
More important details More fun Part 3
EGR 2261 Unit 4 Control Structures I: Selection
The for-each or for-all loop introduced in Java 5
Loop Structures.
Control Structures II (Repetition)
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Simplifying Flow of Control
The C++ IF Statement Part 2 Copyright © Curt Hill
Outline Altering flow of control Boolean expressions
Concepts From Alice Switching to Java Copyright © Curt Hill.
Introduction to Object-Oriented Programming with Java--Wu
The most important decision statement
Arrays in Java What, why and how Copyright Curt Hill.
Throwing and catching exceptions
Compound Statements A Quick Overview
Selection Control Structure
Chapter 4 Selection.
Chapter 6: Repetition Statements
Chapter 4: Control Structures I (Selection)
The Java switch Statement
CprE 185: Intro to Problem Solving (using C)
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
Loops and Iteration CS 21a: Introduction to Computing I
CprE 185: Intro to Problem Solving (using C)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

While and Do While Syntax, semantics and examples Copyright © 1998-2011, Curt Hill

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 © 1998-2011, Curt Hill

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 © 1998-2011, Curt Hill

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 © 1998-2011, Curt Hill

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

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 © 1998-2011, Curt Hill

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 © 1998-2011, Curt Hill

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 © 1998-2011, Curt Hill

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 © 1998-2011, Curt Hill

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 © 1998-2011, Curt Hill

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 © 1998-2011, Curt Hill

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 © 1998-2011, Curt Hill

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 © 1998-2011, Curt Hill

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 © 1998-2011, Curt Hill

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 © 1998-2011, Curt Hill

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