[an error occurred while processing this directive] [an error occurred while processing this directive] AP/CS A Lab 5: while loops

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:

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 + " ");     // output: 1 2 3 4
    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]