The code below demonstrate how to use the while
loop statement. The while
statement will check if its expression evaluates to true
and execute the while
statement block.
The program below will execute while the countDown
value is bigger or equals to zero.
package org.kodejava.basic;
public class WhileDemo {
public static void main(String[] args) {
// Start the count-down from 10
int countDown = 10;
// Do the count-down process while the value of
// countDown is bigger or equals to zero.
while (countDown >= 0) {
System.out.println(countDown);
countDown--;
try {
// Adds one-second delay.
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Latest posts by Wayan (see all)
- How do I create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023