Lecture08 - Loops While, Do, For Repetition Statements...

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

View Full Document Right Arrow Icon
1 Loops – While, Do, For Repetition Statements While Do For Break and Continue in Loops Iterators Iterable Objects and For-each Reading for this Lecture, L&L, 5.5 - 5.8
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Repetition Statements Repetition statements allow us to execute a statement or a block of statements multiple times Often they are referred to as loops Like conditional statements, they are controlled by boolean expressions Java has three kinds of repetition statements: the while loop the do loop the for loop The programmer should choose the right kind of loop for the situation
Background image of page 2
3 The while Statement A while statement has the following syntax: If the condition is true, the statement is executed Then the condition is evaluated again, and if it is still true, the statement is executed again The statement is executed repeatedly until the condition becomes false while ( condition ) statement ;
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 The while Statement An example of a while statement: If the condition of a while loop is false initially, the statement is never executed Therefore, the body of a while loop will execute zero or more times boolean done = false; while (!done) { body of loop statements; if (some condition) done = true; }
Background image of page 4
5 The while Statement Let's look at some examples of loop processing A loop can be used to maintain a running sum A sentinel value is a special input value that represents the end of input See Average.java (page 229) A loop can also be used for input validation , making a program more robust See WinPercentage.java (page 231)
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/25/2010 for the course CS 110 taught by Professor Wilson during the Spring '10 term at University of Massachusetts Boston.

Page1 / 22

Lecture08 - Loops While, Do, For Repetition Statements...

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

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