Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

In this article, we’ll look at a core aspect of the Java language – executing a statement or a group of statements repeatedly using a while loop.

2. While Loop

The while loop is Java’s most fundamental loop statement. It repeats a statement or a block of statements while its controlling Boolean-expression is true.

The syntax of the while loop is:

while (Boolean-expression) 
    statement;

The loop’s Boolean-expression is evaluated before the first iteration of the loop – which means that if the condition is evaluated to false, the loop might not run even once.

Let’s have a look at a simple example:

int i = 0;
while (i < 5) {
    System.out.println("While loop: i = " + i++);
}

3. Conclusion

In this quick tutorial, we explored Java’s while loop.

As always, examples can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.