JAVA For loop: introduction

java for loop tutorial for beginners

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

What distinguishes the JAVA For loop from the other looping statements is that the number of repetitions is known in advance.

This means that we know the number of times we will repeat that processing.

JAVA For loop: syntax

for ( <init_expr>; <test_expr>; <alter_expr> ){

<statement_or_block>

}

This is the recommended version of the JAVA loop statement.

But, we can omit the curli-braces like what we did with the flow control statements.

The syntax of the for loop becomes:

for ( <init_expr>; <test_expr>; <alter_expr> )

<statement_or_block>

JAVA for loop examples:

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

The i variable is incremented in each iteration.

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

We will present the code with and without curli-braces:

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

The version without curli-braces is the following:

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

Remark: the version of the for loop statement without curli-braces may cause bugs in your program if you misuse it.

You should bare in mind that when you omit the curli-braces, the for loop repeats the execution of only one instruction.

This instruction comes directly after the for loop.

In the case of the for loop version with curli-braces, this problem doesn’t exist because the instructions to repeat are delimited by the opening and the closing curli-braces.

We will present an example which shows this problem.

public class TestSand {
public static void main(String[] args) {
for (int i = 0; i<4;i++)
System.out.println("This is the line number " + i);
System.out.println("We will show the next line number");  
}
}

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

This is the line number 0

This is the line number 1

This is the line number 2

This is the line number 3

We will show the next line number

The execution of the code above shows that when we omit the curli-braces, only one instruction gets repeated.

This instruction comes directly after the loop statement.

Special Loop Flow Control

Java loop and the break keyword

The break keyword finished the loop statement.

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

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

The output of this code is the following:

This is the line number 0

Java loop and the continue keyword

The JAVA continue keyword is used to skip one iteration.

public class TestFor {
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
if (i == 2) {
continue;
}
System.out.println("This is the line number " + 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 loop statement arrives at its end.

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

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

Post a Comment

Previous Post Next Post