Learn how to use while loop and do while loop in java with this tutorial. Java while loop and do while loop can be used in programming to perform the execution of codes or statements repeatedly until the given condition is true.

Let’s start the tutorial and learn each type of while loop with examples.

Java While Loop

The while loop contains only one condition which can be true or false. If the condition is true, the codes inside the while loop get executed. However, if the condition is false, the code inside the loop will not get executed and the control jumps to the next code after while loop.

Below is the syntax that contains the while loop with a single condition argument which you have to replace your condition.

Syntax

Inside the while loop, you can put many statements or codes to execute only when the condition is true.

Java While Loop Examples

You can use while loop to create a simple java program, infinite loop condition and iterate through array elements. Learn each section of the programming using the while loop with useful examples and the results given in the output.

Simple Java While Loop Examples

A simple while loop example given below contains the while loop with the condition. The condition given in the while loop will execute the code inside the while loop for 10 times.

I am using the while loop here to print the numbers from 0 to 9. For this, I have initialized the variable i with 0. The condition taken in the while loop is less than 10. An increment operator is taken inside the while
loop to increase the value of the variable by 1 in each iteration.

Example1

Output

0
1
2
3
4
5
6
7
8
9

Print the numbers from 0 to 9 using the java while loop with the above-given example.

Iterate Through an Array Using Java While Loop Examples

If you want to print each element of the array, you can use while loop and iterate through each element. In the below example, the array contains the five elements and you can find the length of the array using the.arr.length

initialize any variable with 0 and put the condition i less than arr.length. This will run the while loop for five times similar to the number of elements inside the array.

Example2

Output

21
13
3
12
5

Infinite Java While Loop

In addition to above examples, you can also create an infinite loop using the while loop. The below example contains the condition i greater than 0. Which can never be false and the loop will execute the statement repeatedly for the infinite number of times.

Example3

If you run the above example, the loop will execute for infinite and print the number repeatedly with an increment of the value.

Java Do While Loop

The do while loop also contains one condition which can true or false. The statement is given in the do while loop, the statement execute for one time after that it only gets executed when the condition is true.

If the condition is false in the while loop, the statement will execute for only one single time.

Syntax

The statement to execute will comes under the do. Put a semicolon after while loop in the do while loop syntax.

Simple Java Do While Loop Examples

The loop will run for 10 times in the example given below. The statement will print the number according to the programming you have done in the code. The below example print numbers from 0 to 9 and after that the loop come to the next line given after the while loop.

Example4

Output

0
1
2
3
4
5
6
7
8
9

In addition to this simple example, if the condition is false as given in the example below. The statement will execute for one single time only and the control will move to the next line given after the while loop.

Example5

Output

1

The above example executes the statement for one time and prints the single output.

Iterate Through an Array Using Java Do While Loop Examples

You can also iterate through each element of an array using the java do while loop. The same method you have to follow as you have used above. The only difference is the while condition you have to use at the end of the code. It first executes statement then check the condition given in the while loop.

Example6

Output

21
13
3
12
5

Infinite Java Do While Loop

An infinite loop can be created using the do while loop. The statement will execute first without checking the condition of the infinite loop. After that, it will check the condition and the infinite loop starts working.

The loop runs and prints the statement output for the repeated number of times.
Example7

You must also read.

Reference.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.