[an error occurred while processing this directive]
[an error occurred while processing this directive]
University of Washington, AP/CS A
Lab 5: while loops
Except where otherwise noted, the contents of this document are
Copyright 2013 Stuart Reges and Marty Stepp.
lab document created by Marty Stepp, Stuart Reges and Whitaker Brand
Today's lab
Goals for this lab:
- use
while
loops for indefinite repetition
- Where you see this icon, you can click it to check the problem in Practice-It!
while
Loops
for
loops are fantastic for when we know how many times we want to repeat something. But sometimes we won't know how many times we'll want to repeat something in advance! while
loops repeat indefinitely while a given condition is true.
while (test) {
statement(s);
}
Example:
int num = 1;
while (num < 5) {
System.out.print(num + " ");
num++;
}
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]
[an error occurred while processing this directive]