Lecture 06 07
Lecture 06 07
--number; 4
System.out.println("Now, number is " + number);
The while Loop
• Java provides three different looping structures.
• The while loop has the form:
while(condition){
statements;
}
• While the condition is true, the statements will
execute repeatedly.
• The while loop is a pretest loop, which means
that it will test the value of the condition prior to
executing the loop.
The while Loop
• Care must be taken to set the condition to
false somewhere in the loop so the loop will
end.
• Loops that do not end are called infinite loops.
• A while loop executes 0 or more times since if
the condition is false, the loop will not
execute.
Example: WhileLoop.java
int number = 1; number Output
1 Hello
while (number <= 5)
2 Hello
{
3 Hello
System.out.println("Hello");
4 Hello
number++;
5 Hello
}
6
System.out.println("That's all!");
Infinite Loops
• In order for a while loop to end, the condition
must become false.
int x = 20;
while(x > 0){
System.out.println(“x is greater than
0”);
}