Iteration Statements in Java

Iteration statement in java is the compound statement that needs to execute multiple times zero to more. Loops are the Iteration statements in Java.

What is the Iteration statement in Java?

A statement in Java is a single command that is executed by the Java interpreter. When we need to execute a statement or collection of statements multiple time then its called Iteration or Iterative statement. Loops are used to perform the Iteration statement in Java.

Let’s understand what is loops and how can we achieve the Iteration statement in java using loops.

What are loops in Java?

As I discuss loops are used to execute a statement multiple times. Mainly there are four types of loops support by Java.

  • for loop
  • while loop
  • do while loop
  • for each loop

for loop in Java

for loops in Java is used to iterate a statement multiple times. Mostly it’s used when we have a fixed number of iterations.

All its loop control elements are gathered in one place (on the top of the loop), while in the other loop construction of the java, they (top -control elements) are scattered about the program.

Syntax of for loop in Java

for (initialization expression(s); test-expression; Increment/decrement(s))

{
Body of the loop;

}

for loop in java is the easiest to understand and to implement. Just need to start point and endpoint. Let’s understand. Let’s try to understand with an example.

Suppose, We need to write a java program to print numbers from 1-5.

Initialization is the starting point for the loop (int i=0)

Expression/Condition is the endpoint for the loop (int i<6)

Increment/decrement value of the conditions (i++)

for(int i=1; i<6; i++){

System.out.println(i+", ");

}

It will print the output 1, 2, 3, 4, 5 but what are the execution steps of this program Let’s deep dive into this.

At very first i = 1; now controller will check the condition(i<6) its true because 1<6. so in the first iteration, it will print the current value of (i) that is 1. Now controller  will move to Increment/decrement(i++). and i++ will increase the value of (i) by one.

Now the value of i=2 again checks the condition(2<6) it is true so it will print the current value of i=2 and controller move to (i++). the process will repeat until the condition is true. The loop will terminate once the condition(6<6) is false.

while loop in Java

while loop in java is another form of iteration statement in java. In simple words, Another way to iterate a statement multiple times. The only difference in the execution.

It will check the condition initially. It means if the condition is false in the first iteration it will not print initialize the variable.

The while loop is an entry controlled loop. Where the loop body may contain a single statement, a compound statement or any empty statement.

The loop iterates while the expression evaluates to true. When the expression becomes false, the program control passes to the line after the loop -body code.

Syntax of while loop

while(condition){  
//loop-body
//update expression(s)

}

Let’s understand while loop with the same problem. Write a java program to print numbers from 1-5.

int i =1;

while(i<6){

System.out.println(i+", ");

i++;

}

The Initial value of i=1; Now Controller will enter in the loop and at very first it will check the condition(1<6) that is true. then the next statement will execute and print the current value of i. then the controller will come to i++.

Now the i=2 and the condition is (2<6) again true so it will print 2. similarly the same process will repeat until the condition reaches to (6<6) false.

Program to find factorial of a number using while loop in java

class example
{
public static void main(String args[])
{
long i=0, fact=1;
i= num;
while(num != 0)
{
fact= fact* num ;
--num;
}
System.out.println(“the factorial of “ + i+” is “ + fact);
}
}

Output:
The factorial of 5 is 120.

do while loop in Java

The do-while is an exit- controlled loop I.e., it evaluates its test-expression at the bottom of the loop after executing its loop-body statement. This means that a do-while loop always executes at least once.

Syntax of the do-while loop in java

initialization expression(s)
do
{
loop body
update expression(s)
}
while(test- expression);

Let’s understand while loop with the same problem. Write a java program to print numbers from 1-5 using do while loop.

public class Loops {
  public static void main(String[] args) {
    int i =1;
    do {
      System.out.println(i+", ");
      i++;
    }while(i<6);
  }

}

for each loop in Java

Java introduce for each loop from JDK 1.5. It’s really cool and my favorite one. It solved the problem of fixed interaction.   for each executes dynamically not need to define the condition.

for each loop is really helpful. when we need to retrieve elements from an array list or array. 

Syntax of for each loop in java

for (data type var_name : array) 
{ 
    statements using var_name;
}

Example: Write a java program to print numbers from 1-5 using for each loop.

public class Loops {
  public static void main(String[] args) {
    int a[] = {1,2,3,4,5};
    for (int i : a) {
      System.out.print(i+", ");
    }
  }

}

Output: 1, 2, 3, 4, 5