Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 1

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 2: counts downwards from 500 to 50 by 25


count = 500 //initializes count
while (count >= 50) {
System.out.println(count);
count-=25; //count = count - 25
}

//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++;
}
}
}

You might also like