
CSIS 110 - Lecture 13
Announcements
Program due Wednesday.
Quiz next Monday.
Next Program?
Last time: foreach loop
Use classroom presenter to ask students to write for each loops for some ArrayLists.
Limitations of foreach loop:
- Can only go through collection in one direction
- Can't modify the collection inside the loop, because you don't have an index
- Have to go through every element in the collection โ not easy to stop in the
middle.
While loop
Java provides another variety of loop statement โ the while statement.
while ( boolean-expr ) {
statements;
}
The while statement works this way:
1. Evaluate the boolean expression. If it is true go to step 2. Otherwise, go to step 4.
2. Execute the statements in the while-loop body.
3. Go to step 1
4. Execute the statement following the while loop.
[Draw a flow chart]
Add a listAllNotes method to the notebook project โ it needs this while loop
int index = 0;
while (index < notes.size() ) {
String nextNote = notes.get( index );
System.out.println( nextNote );
index++;
}
Discuss practical parts of this loop: