Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Java While-Loop Examples

This Java example set uses the while-loop. This loop helps when looping until an unknown end condition is reached.

While. All things have endpoints. With a while-loop we proceed forward.

This loop helps when the end condition (such as the number 10) is not yet known.

Infinite loops. Here is a warning about the while-loop. It can lead to infinite loops, ones that freeze programs and give headaches. We must carefully test ends.

A program. In this example, we control a loop based on both "i" and "z." This could be expressed with a for-loop, but it might be less clear.

And: We increment "i" upwards and decrement "z" downwards. When "i" is no longer smaller than "z," the loop ceases.

Thus: In programs, for-loops are more often useful. But in cases where the loop termination is unknown, a while-loop shines.

Based on:

Java 7

Java program that uses while-loop

public class Program {
    public static void main(String[] args) {

	int i = 0;
	int z = 10;

	// Loop with two variables.
	while (i < z) {

	    // Display the values.
	    System.out.println(i + "/" + z);



Break, while-true. Break is often useful in while-loops. But it can be used in any kind of loop. Here we show a while-true loop—it would be infinite if there were no break statement.


However: The break statement terminates the loop. No further iterations are run.

Here: This program will continue looping in the while-loop until it reaches a random number greater than 0.8.


Java program that uses break, while-true

import java.lang.Math;

public class Program {
    public static void main(String[] args) {

	// Loop infinitely.
	while (true) {

	    // Get random number between 0 and 1.
	    double value = Math.random();

	    // Break if greater than 0.8.
	    if (value >= 0.8) {



Do-while. This loop is just like a while-loop, except its condition is not checked before the first iteration is reached. So we must ensure the loop works with its initial value.


Sometimes: A do-while loop can make programs faster by reducing the number of checks done.

Java program that uses do-while

public class Program {
    public static void main(String[] args) {

	int i = 0;
	// Loop while the variable is less than 3.
	// ... It is not checked on the first iteration.
	do {
	} while (i < 3);



Post-increment, continue. This while-loop uses several syntax forms. It uses a post-increment in the while-loop condition. This means the < test is done before the increment is done.

Modulo: We also use a modulo test to see if a number is evenly divisible by 2. We use the continue statement to stop the current iteration.


Java program that uses while, post increment, continue

public class Program {
    public static void main(String[] args) {

	int index = 0;
	// Use post increment in while-loop expression.
	while (index++ < 10) {

	    // Continue if even number.
	    if ((index % 2) == 0) {
	    System.out.println("Element: " + index);


Element: 1
Element: 3
Element: 5
Element: 7
Element: 9

A choice. In most looping contexts, a for-loop is a better choice. But an important skill is to detect when a while-loop is a superior option. Rarely even a do-while is better.

On the surface, loops are simple. But I find the most complex algorithms are based mainly on loops. So loops are simple for simple things, but become rapidly complex.

Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf