Home » Java Basics » while loop » Simple while loop Java Example (with video)

About Shaik Ashish

Avatar photo
He completed his Bachelors Degree in Computer Applications. He is a freelancer, writer, Microsoft Certified in Python and Java. He enjoys Writing, Teaching, Coding in Java and Python, Learning New Technologies and Music.

Simple while loop Java Example (with video)

1. Introduction

With this example we are going to demonstrate how to use a simple while loop Java statement. The while statement continually executes a block of statements while a particular condition is true. In short, to use a simple while loop you should:

  • Create a while statement that evaluates an expression, which must return a boolean value and has a statement.
  • If the expression evaluates to true, the while statement executes the statement in the while block.
  • The while statement continues testing the expression and executing its block until the expression evaluates to false.

You can also check Loops in Java in the following video:

For loop, while loop and do while loop in Java – Video

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.basics;
public class SimpleWhileLoopExample {
    public static void main(String[] args) {
        int i = 0;
        int max = 5;
        System.out.println("Counting to " + max);
        while (i < max) {
            System.out.println("i is : " + i);
Counting to 5
i is : 1
i is : 2
i is : 3
i is : 4
i is : 5

2. Control Flow Diagram

In this flow given below, as part of while loop execution, at first, the test expression which evaluates to boolean value is executed. If the boolean condition evaluates to True, then the statements inside the body of the loop will be executed. Once the execution of statement(s) inside the loop gets over, then the test expression is again evaluated. Only when the condition evaluates to False, then the execution of the loop terminates.

while loop Java - Control Flow
Fig:1 While Loop Control Flow

3. While loop with assignment

In this section let us get to know a program which utilizes the while loop with assignment

public class WhileWithAssignment
	public static void main(String[] args) {
    boolean var=true;
    	System.out.println("Inside while");
    System.out.println("Outside while, value of var is: "+var);

    public static boolean getNext()
    	return false;
Outside while, value of var is: false

First, the variable var is initialized with true. In the parenthesis of while, getNext() method is called. It returns false and assigns false to the variable var. Finally, the statement after while loop is executed.

5. Download the Source Code

This is an example of Simple While Loop in Java.

You can download the full source code of this example here: Simple while loop Java Example

Last updated on Apr. 29th, 2021

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

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

Inline Feedbacks
View all comments