Android Course - How to use Loop in Java

Android Tutorial – How to Use Loop in Java

Articles & Tutorials Programming

Android Tutorial – How to Use Loop in Java

 

What is a loop in Java? How to use them? In this article, I’ll explain to you the different loop that exists in Java and how to use them.

 

What is the loop in Java

In programming languages, looping is a feature which makes easy the execution of part of code for a set of instruction while the condition is true
In Java we have 3 different ways to implement a loop, about the functionality there are same but the syntax is different between them. I’ll show you one by one how to write them

 

1) While Loop:

With a while loop, we can run repeatedly some part of codes (or set of instructions) until the condition isn’t respected.

The syntax of while loop is like this :

while (boolean conditions)
{
   // set of instruction
}

How this code run :

  1. The compiler checks the conditions, if it returns true, it executes the set of instructions inside the while loop
  2. The compiler retur to first step and make the same thing until the conditions returns false.

 

So, let’s write a simple example of how to implement this concept :

class LoopsExample
{ 
    public static void main(String args[]) 
    { 
        int number = 3; 
  
        // Exit when number becomes equal or greater than 8 
        while (number < 8) 
        { 
            System.out.println("The next number is: " + number); 
            number++; 
        } 
    } 
}

 

When you execute this code, you’ll get this result :

The next number is:3

The next number is:4

The next number is:5

The next number is:6

The next number is:7

As you can see, the instruction System.out.println(“The next number is: ” + number); has been repeated 5 times

 

2) For Loop:

A for loop is another way to write a loop structure in Java. The difference between the for and the while loop, the for loop make the initialization, the conditions and the incrementation (or decrementation) in the same instruction. So the syntax is like this :

for (initialization; condition; increment/decrement)
{
    instructions
}

Let’s see now how this code works

 

  1. The first time, the compiler makes the initialization
  2. After this, the compiler test the condition, if the condition isn’t respected, it’s the end of the loop
  3. If the condition returns true, the set of instructions inside the for will be executed.
  4. Increment or decrement the variable and check again the condition
  5. Exist of the loop when the condition returns false

 

Let’s make a simple example for implementing for loop :

class LoopsExample { 
    public static void main(String args[]) 
    { 
        // for loop begins when number=3 
        // and runs till number < 8
        // and increment number by 2
        for (int number = 3; number < 8; number = number+2) 
            System.out.println("The next number is:" + number); 
    } 
}

The output will be :

The next number is:3

The next number is:5

The next number is:7

We don’t get the number 9 because, when we have number 7, we increment the number by 2, so the number will be 9 and 9 is less than 8.

 

For Loop with Array:

In the previous article, I’ve shown you how to use Array, List, and Map in Java. The For loop is often used with this type of variable. I‘ll show you how to use it with an array and it’s the same thing with two others

To use For Loop in Array, we have two different ways (2 different syntaxes) :

for(init index; condition; increment/decrement index) {
 // instruction with arrayVariable[index]
}

or

for (Type element: arrayVariable)
{
    // Instructions with element
}

The second syntax has been introduced In Java since version 5 of JDK, I recommend you to use this syntax. Let’s see an example :

public class LoopsExample{ 
    public static void main(String args[]) 
    { 
        String countries[] = {"Algeria", "Morocco", "Tunisia", "Spain", "Senegal"}; 
  
        //Type is String 
        for (String x:countries) 
        { 
            System.out.println("The country is:"+x); 
        } 
  
        // The second syntax 
        for (int i = 0; i < countries.length; i++) 
        { 
            System.out.println("The country is:"+countries[i]); 
        } 
        
    } 
}

The output for the 2 syntaxes will be :

The country is :Algeria

The country is :Morroco

The country is :Tunisia

The country is :Spain

The country is :Senegal

The difference is very clear, the first one is more readable than the second.

 

3) Do While Loop:

The Do while loop is the same loop with while, the only difference is the instruction is executed at least one time. The syntax is like this :

init;
do
{
    instructions..
    increment / decrement
}
while (condition);

Example :

class LoopsExample { 
      public static void main(String args[]) { 
         int number = 3; 
         do {
              System.out.println("The next number is: " + number); 
              number++;
           } while (number < 8);
      }
}

 

The video where I explain these different loops:

Leave a Reply

Your email address will not be published. Required fields are marked *