Sitemap
Farel Alghazali

Farel Alghazali

Undergraduate students that very interested in information technology

Looping in Java with While loop

2 min readOct 24, 2025

--

Press enter or click to view image in full size
looping ilustration
Photo by Roman Synkevych on Unsplash

One of base concept in programming language is looping / iteration. Sometime you need to make a code repeatedly due to certain requirements, such as print users list or to print the sum of 1 to n. To make it more efficient, we can use looping, and now we will learn about loops with while-do & do-while.

While-do

When we want to use loops, we should know when the loops is start and when the loops is end, so that we need a condition to validate when loops start & end. In while statement, the condition is checked first before the code/action is executed. Example code:

int col = 1;
int max = 5;

while (col <= max) {
System.out.println("Column number " + col);
col++;}

In the code above, we first check the condition for the loops. If col ≤ max is true, execute the action; and if false don’t execute the action and stop the loops. So if the condition is false from start, the action will not be executed at all.

Do-while

If in while-do we check the condition first, in do-while the condition is checked last. Example code:

int col = 6;
int max = 5;

do{
System.out.println("Column number " + col);
col++;
} while ( col <= max );

In the code above, we will execute the action first. Then, we check the condition: if col ≤ max is true, continue the loops; but if false, stop the loops. In the do-while, the action will be executed at least once when the condition is false, cause the condition check is at the end, so the code will be executed first .

Get Farel Alghazali’s stories in your inbox

Join Medium for free to get updates from this writer.

So that's it looping in Java with while-do & do-while statements. Both methods have the same concept, which is to run a loop when a condition is met. The only difference is in the order of the checks: while-do is at the beginning, and do-while is at the end. So, I hope you understand this article, and see you next time!

--

--

Farel Alghazali
Farel Alghazali

Written by Farel Alghazali

Undergraduate students that very interested in information technology