Control Structures in Java — Loops

Nickson Joram
Javarevisited
Published in
4 min readJan 20, 2020

We all know what is defined as Control structures and we have seen about one of the Control Structure (Conditional Statements) here.

Today we are going to discuss another Control structure that is being used in Computer Programming, especially in Java.

You know that we can print a statement with a new line in Java using

System.out.println("Hello");

If you have to print the Hello for 5 times, we can use the entire line for 5 times as this

System.out.println("Hello"); //1st
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello");
System.out.println("Hello"); //5th

What if it is required 100 times? Going to include this in 100 lines as follows?

System.out.println("Hello"); //1st
System.out.println("Hello");
.
.
.
System.out.println("Hello"); //100th

Theoretically, it is fine and not wrong at all. But when we are building a system, and if we are including lines like this, we are just increasing the lines that can give some other costs like increase in size and become a nonreadable material. But sometimes it may result in less use of time as an advantage.

So now you’ve come to a conclusion that to execute a series of similar or progressive commands, we can use a repeating chain-like method.

Iteration statements or Loops are used to iterate through multiple values/objects and repeatedly run specific code blocks. The basic loop types in Java are

  1. for loop
  2. while loop
  3. do-while loop

Let's see them briefly one by one

For loop

Syntax

for(initialization; Boolean expression; update){
//Statements
}

Just see this simple example

for(int i=1;i<=10;i++){
System.out.println(i);
}

In this example, you can see that we have assigned a variable i with data type Integer with value 1. In the Boolean expression, i will be tested to see whether it is less than or equal to 10. Then the statement will be executed. After that, i will get an increment by 1 due to the update given as i++. So in this example, we will get integers 1 to 10 printed in consecutive newlines. That is;

1
2
3
.
.
10

Here also we can have nested for loop if needed. The syntax fr nested for loop is

for(for(initialization; Boolean expression; update){
for(for(initialization; Boolean expression; update){
//Statements
}
}

In for loop the iteration itself will give increments or decrements and no need to specify the increments or decrements inside the loop.

While loop

Syntax

while(Boolean expression){
//Statements
}

Not as for loop, this loop will continue as long as the expression result is true. To control the loop the Boolean expression assigned in the condition has to give false. In this loop, we can add the increments or decrements inside the loop.

Look at this simple example

int i=1;
while(i<=10){
System.out.println(i);
i++; //also can use ++i here
}

In this example, you can see that we have assigned a variable i with data type Integer with value 1. In the Boolean expression, i will be tested to see whether it is less than or equal to 10. Then the statement will be executed. After that, i will get an increment by 1 due to the increment given as i++. So in this example, we will get integers 1 to 10 printed in consecutive newlines. That is;

1
2
3
.
.
10

What are we comment/remove the increment part? Try it!

Obviously, the loop won’t end and 1 will be printed until you forcefully terminate the program.

Do while loop

Syntax

do{
//Statements
} while(Boolean expression);

In this control structure, the loop will first execute the statement, and then after it will check the condition. That means even if the condition is not satisfied the loop will execute the 1st iteration at the beginning.

Just see this simple example

int i=1; 
do{
System.out.println(i);
i++;
}while(i<=10);

In this example also you can see that we have assigned a variable i with data type Integer with value 1. As the only difference, the print statement will be executed 1st and then it will check the condition for the 1st execution. In this example, we will get integers 1 to 10 printed in consecutive newlines. That is;

1
2
3
.
.
10

What if you make the condition slightly like this?

int i=1; 
do{
System.out.println(i);
i++;
}while(i<1);

Yeah! You’ll get 1 as output and that is the end of the loop. This feature can’t be accessed in for or while loops. Because they start the iteration only after checking the condition. But Do while loop starts the 1st execution without considering the Boolean expression.

I hope you’ve understood the loops in Java and keep practicing for better understandings. Play with the loops!

--

--