JAVA while loop: introduction

JAVA while loop tutorial for beginners

The JAVA while loop statement is used to repeat a set of instructions multiple times.

In the JAVA while loop, the number of repetitions is not known in advance.

JAVA while loop: syntax

while ( <test_expr> ) {

<statement_or_block>

}

The body of the JAVA while loop is executed only if the test_expr is true.

Otherwise, it is never executed.

The body of this iterative structure can be executed 0 to several times.

In fact, the body of the JAVA while loop is repeated until the test_expr condition becomes false.

If the test_expr condition never becomes false, we fall in an infinite loop and our program will fail.

We can omit the curli-braces like what we did with the flow control statements and the for loop.

The syntax of the while loop becomes:

while ( <test_expr> )

<statement_or_block>

Java while loop examples

public class TestWhile {
public static void main(String[] args) {
int i = 0;
while (i < 4) {
System.out.println("This is the line number " + i);
 i++;
}
}
}

This code displays the message: “This is the line number i” 4 times in the console.

The i variable is incremented in each iteration (the i++ instruction does the incrementation).

The result of the execution of this program is the following:

This is the line number 0

This is the line number 1

This is the line number 2

This is the line number 3

Remark: you should use the version of the while loop statement without curli-braces carefully.

In this case, the while loop repeats the execution of only one instruction.

This instruction comes directly after the while loop.

In the case of the while loop version with curli-braces, the instructions to repeat are delimited by the two curli-braces.

We will present an example which shows this problem.

public class TestWhile {
public static void main(String[] args) {
int i = 0;
while (i < 4)
i++;
System.out.println("This is a new line");
}
}

When we run this code, we will get this output:

This is a new line

When we omitted the curli-braces only the incrementation of the variable i is repeated.

The program above is fixed by adding the curli-braces:

public class TestWhile {
public static void main(String[] args) {
int i = 0;
while (i < 4){
i++;
System.out.println("This is a new line");
}
}
}

Special Loop Flow Control

Java while loop and the break keyword

The break keyword finishes the loop statement.

It is generally used to stop the execution of the while loop if some condition is true.

public class
TestWhile {
public static void main(String[] args) {
int i = 0;
while (i < 4) {
if (i == 2) {
break;
}
System.out.println("This is a new line number " + i);
i++;
}
}
}

The output of this code is the following:

This is the line number 0

This is the line number 1

Java while loop and the continue keyword

The JAVA continue keyword is used to skip one iteration.

public class
TestWhile {
public static void main(String[] args) {
int i = 0;
while (i < 4) {
if (i == 2) {
continue;
}
System.out.println("This is a new line number " + i);
i++;
}
}
}

The output of this code is the following:

This is the line number 0

This is the line number 1

This is the line number 3

We notice that we skipped the processing of the line number 2.

In fact, we skipped the display of the message for the line number 2.

This core JAVA tutorial which deals with java while loop statement arrives at its end.

To check the other JAVA loop statements, please check this tutorial.

Please, don’t forget to follow us on Facebook to get our next JAVA core tutorial.

Post a Comment

Previous Post Next Post