Monday, August 31, 2020

Learn For Loop in Java with example

for_loop_in_java
In the Java programming (as well as C derived languages such as C ++, PHP and JavaScript), there are 3 loop structures, namely the for loop, the while loop and the do while loop.

So, here we will discuss the for loop first.

Understanding Loop Structure For Java Language

Loop is a program code instruction that aims to repeat several lines of commands. In designing a for loop, we need to know at least 3 components:
  • The initial condition of the loop.
  • Condition at the time of looping.
  • The conditions that must be met for the loop to stop.
The basic format for the for loop structure in Java programming is as follows:

for (start; condition; increment) 
{
   // code here
   // code here
}

Start is a condition at the beginning of the loop. Usually this initial condition contains a command to assign a value to the counter variable. The counter variable itself is a variable that determines how many loops are performed. Most programmers use the variable i as a counter variable (this is not necessary, you can also use other variables).

As long as the conditions are met to running the loop , the Java language compiler will continue looping. For example, the condition contains the command i < 7, so as long as the "i" variable contains a number less than 7, then the program will keep looping.

Increment is the part that is used to process the counter variable so that it can fulfill the final loop condition. This section will always be executed on every loop.

It is called an increment because it usually contains increment operations such as i ++, which is actually the same as i = i + 1. That is, in each loop, the variable i will raise by 1 digit. But we can also give other values such as i = i + 2 so that the counter variable will increase by 2 digits per iteration

In addition, there is the term iteration (iteration), which means 1 iteration. This term is used quite often when discussing loop structures.

Example of looping program code for Java language.

As the first example, I want to display the text “Hello World” 5 times. Here's the program code:

class LearnJava {
  public static void main(String args[]){
     
    int i; 
    for (i = 1; i < 5; i++) { 
      System.out.println("Hello World "); 
    }
   
  }
}

In line 4, I create a variable "i" which is set as the integer data type. This variable will later be used as a counter variable, which is a variable that determines the final state of the loop.

The command at line 5, namely for (i = 1; i <5; i ++), can be read:

"Run the loop, starting from variable i = 1 to i < 5. In each iteration, increase the value of the i variable by 1 digit using the i++ command".

Here are the results:

Hello World
Hello World
Hello World
Hello World

The question is, why only 4 lines of “Hello World” appear? Even though we repeat from i = 1 to i < 5.

This has to do with the use of signs. The final condition of the loop is i < 5, which means that it will always be true if i is less than 5, but if it reaches 5 then the condition becomes false and the loop stop.

There are 2 alternatives to make the "Hello World" can come out 5 times. You can change the initial condition to i = 0, or change the final condition to i <= 5. The second option looks better:

class LearnJava {
  public static void main(String args[]){
     
    int i; 
    for (i = 1; i <= 5; i++) { 
      System.out.println("Hello World "); 
    }
  }
}

Program code results:

Hello World
Hello World
Hello World
Hello World
Hello World

Now the "Hello World" has appeared 5 times. Again, be careful using java comparisons, especially between "<" and "<=".

Inside the loop, we can also access the counter variable like this:

class LearnJava {
  public static void main(String args[]){
     
    int i; 
    for (i = 1; i <= 5; i++) { 
      System.out.println("Hello World "+i); 
    }
   
  }
}

Program code results:

Hello World 1
Hello World 2
Hello World 3
Hello World 4
Hello World 5

Now on the side "Hello World" text, there are the number from the variable value i. Because in each iteration the counter variable i will increase by 1 point (increment process), so when it is displayed it will also increase by 1 point for each iteration.

The counter variable i also doesn't have to be incremented, but it can also be decremented to make the loop decrease. Here's an example:

class LearnJava {
  public static void main(String args[]){
     
    int i; 
    for (i = 5; i >= 1; i--) { 
      System.out.println("Hello World "+i); 
    }
   
  }
}

Program code results:

Hello World 5
Hello World 4
Hello World 3
Hello World 2
Hello World 1

Our code is very similar as before, but pay attention to the for command on line 5: for (i = 5; i> = 1; i–). This can be read:

"Run the loop, starting from the variable i = 5 to i> = 1. In each iteration, decrease the value of the variable i by 1 digit using the command i--".

As a result, the value of the counter variable i will decrease by 1 digit in each iteration.

As a final example, can you make a loop to show the multiple of 3 10 times? The final result we want is as follows:

3 6 9 12 15 18 21 24 27 30

There are several ways to produce this sequence. First, change on the side of the command block to be executed where the counter variable i keeps increasing from 1 to 10:

class LearnJava {
  public static void main(String args[]){
     
    int i; 
    for (i = 1; i <= 10; i++) { 
      System.out.print(i*3 + " "); 
    }
   
  }
}

In order to produce a number that increases by a multiple of 3, the technique used is to multiply the value of the counter variable i by the number 3 for each iteration.

The second way is to modify the increment process of the counter variable:

class LearnJava {
  public static void main(String args[]){
     
    int i; 
    for (i = 3; i <= 30; i = i + 3) { 
      System.out.print(i + " "); 
    }
   
  }
}

Note the for loop command on line 5. The for command (i = 3; i <= 30; i = i + 3) can be read:

"Run the loop, starting from the variable i = 3 to i <= 30. In each iteration, increase the value of the variable i by 3 digits using the command i = i + 3".

This technique is rarely used, but it can be done.

In today's tutorial we have discussed the for loop. Later, these loops can be further combined, for example to create nested loops.

But for the next tutorial, we will continue with the second form of loop in Java, namely the WHILE loop.