Professional Documents
Culture Documents
13 - While Loops
13 - While Loops
A while loop continues repeating it’s body while the <condition> is true. Another way of saying
this is it continues looping until the <condition> is no longer met.
while (<condition>) {
//body
}
//WhileLoops.java
public class WhileLoops {
public static void main (String args[]) {
//example 1: counts from 0 to 100
int count = 0; //declares and initializes the counter variable
while (count<=100) {
System.out.println(count);
count++; //count=count+1
}
//example 3: Get a minimum from the user. Show all values from
the minimum up to 100.
Scanner keyb = new Scanner(System.in);
System.out.print("Enter the minimum: ");
int count = keyb.nextInt();
while (count<101) {
System.out.println(count);
count++;
}
}
}