Chapter 3 - Java for Beginners Course

While and Do-While Statements

The while and do-while are looping statements that execute a given block of code while a given boolean expression is true.

while statement

The while statement has the following syntax:

while(boolean expression is true) {
	// execute this block of code

If the boolean expression, or loop condition, evaluates to true, then the block of code will be executed. Once the execution reaches the last line of the block of code, the execution will loop back to the starting point of the while loop and will evaluate the boolean expression once again, repeating the same process.

If the boolean expression is false, then the loop stops and the execution continues after the block of code of the while loop.

With the while statement, the block of code might not be executed at all if the loop condition evaluates to false the first time. This means, the block of code will be executed 0 or more times.

As an example, the following code sums all the numbers from 1 to the value given in the n variable:

int n = 3;
int sum = 0;
int count = 1;
while (count <= n) {
    sum = sum + count;

System.out.println("The sum from 1 to " + n + " is: " + sum);


The sum from 1 to 3 is: 6
Try running the example application with different values of n. What happens if you give n a value of 0 or a negative value?

Analysing the while loop example

The following table represents what happens in the execution of the example above. The first row of the table illustrates what happens the first time the while loop is executed (first iteration), the 2nd row the 2nd time (second iteration), etc.

Iteration Loop Condition (count <= n) Is Block Of Code Executed? New value of sum New value of count


(1 <= 3) = true


0 + 1 = 1



(2 <= 3) = true


1 + 2 = 3



(3 <= 3) = true


3 + 3 = 6



(4 <= 3) = false




To give more details about the table, in the first iteration the loop condition checks if the count variable is less than or equal to n, which is true, 1 is less than 3. As the condition is true, the block of code is executed.

Inside of the loop, we update our sum variable, by adding the current value of sum (in this case 0) to the value of count (1 in this iteration), giving a result of 1.

Finally, our count variable is incremented with a new value of 2.

The same process is followed for the next iterations until we reach the point where count is assigned a value of 4. This causes the loop condition to be false (4 is not less than or equal to n that has a value of 3)

do-while statement

The do-while statement has the following syntax:

do {
	// execute this block of code
} while(boolean expression is true);

It performs the same job as the while loop, the difference relies on when the loop condition is evaluated. In the case of the do-while the loop condition is evaluated at the end of the block of code. As a result, the block of code will be executed at least once.

For example, the following code calculates the factorial of the m variable. The factorial of a number (m!) equals 1 * 2 * …​ * m.

int m = 4;
int factorial = 1;
int current = 1;
do {
    factorial = factorial * current;
} while (current <= m);

System.out.println("The factorial of " + m + " is: " + factorial);


The factorial of 4 is: 24
Try running the example application with different values for m and see what results you get. What happens if you assign a negative value?
Code in GitHub

Get the code for this tutorial using the links below.

Project Repo
Download code for this step
Main class for this step

This is a list of recommended tutorials or courses that might be useful before starting this one.

Welcome to the Course!
Course Introduction
Chapter 1 - Building Blocks
Quick introduction to Java Variables Classes And Objects Class Example - Defining a class Object Examples - Creating instances Java Application Example - Running our first app Accessing class members - The dot operator Packages - Organizing the code
Chapter 2 - Primitives and Operators
Primitives Arithmetic Operators Assignment Operator Unary Operators Equality and Relational Operators Conditional Operators
Chapter 3 - Statements and Control Flow
Expressions Statements If-Then Statement If-Then-Else Statement More If Statements Switch Statement While and Do-While Statements For Statement Branching Statements Exception Handling
Chapter 4 - Code Example
Example Project - A Simple Vending Machine Adding money Delivering Items Giving Change
Chapter 5 - Classes and Interfaces
Introduction Access Level Modifiers Class Declaration - Class, Methods and Fields Class Declaration - Constructors Inheritance Basics Inheritance - Constructors Inheritance - Methods and Fields Polymorphism Abstract Classes and Methods Interfaces Static Class Members Class Composition Final Classes and Class Members Generic Classes
Chapter 6 - Base Object Behaviors
Introduction Type Comparison Type Casting Object Equality - The Contract Object Equality - Common Pitfalls Object String Representation Garbage Collection Object Comparison Primitive Wrappers and Autoboxing
Chapter 7 - Data Structures
Introduction Arrays - Declaration and Creation Arrays - Basic Operations Core Collection Interfaces List and ArrayList - Basic Operations ArrayList Internals Introduction to Hash Tables Map and HashMap - Basic Operations Set and HashSet - Basic Operations
Chapter 8 - Anonymous classes and lambdas
Introduction Filtering a List Anonymous Classes Lambdas Built-in Functional Interfaces
Chapter 9 - Streams
Introduction Creating Streams Intermediate Operations Terminal Operations