Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
While Loop
-
If you do not know how many times a loop will execute, use a while or do-while
loop.
-
These loops execute until a condition becomes true. This condition test
is contained in parenthesis after the while statement. It
is follwed by a block of code contained in curly braces.
-
Ideal for a situation where you prompt users, or change the value of the
test variable within the code block.
-
Example:
var registeredStudents = 0;
while(registeredStudents <= 24) {
addToClass(student);
registeredStudents++;
}
-
The code in the block will execute until the number of registered students
exceeds 24.
Do-While Loop


Created by: Brandan
Jones January 4, 2002