Loops in Java

In a programming language, a feature that helps you to execute the set of instructions/ functions to execute repeatedly when some conditions evaluate to be true is known as looping. In java programming, there are three types of loops available. In other words, we can also say that repetition of structures is known as loops.

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

1. For Loop.

In java programming, using for loop we can execute some specified block of code for a certain number of time and the number of iterations are fixed. A for loop has initialization, condition and increment/decrement in one line which provides a shorter and easier syntax to code and debug.

Syntax

for (initialization; condition; increment/decrement){
     //Block of code to be executed.
}

where,

  • In the initialization condition, variable initialization or declaration is done, marking the loop’s start point of the loop which is local to the loop only.
  • The second condition is executed every time to test the condition of the loop and continues to execute until the condition is false. It must return a boolean value .i.e either true or false.
  • Once the condition is true, the block of code inside the body of the loop is executed.
  • With increment/decrement, it performs increment or decrement for the value of a variable.

Example 1

You can find the code here

package com.qacaffe.examples.loops;

public class For1 {
    public static void main(String[] args) {
        int counter = 1;
        for (int i = 1; i <= 10; i++) {
            System.out.println("Hello this is block " + counter++);
        }
    }
}

/*
Output:
Hello this is block 1
Hello this is block 2
Hello this is block 3
Hello this is block 4
Hello this is block 5
Hello this is block 6
Hello this is block 7
Hello this is block 8
Hello this is block 9
Hello this is block 10
 */

Example 2

You can find the code here

package com.qacaffe.examples.loops;

import java.util.Scanner;

public class For2 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a number whose multiplication Table you want : ");
        int multiplication = scanner.nextInt();

        System.out.println("------------Multiplication Table of " + multiplication + "------------");

        for (int i = 1; i <= 10; i++) {
            System.out.printf("%d * %d = %d\n", multiplication, i, multiplication * i);
        }
        System.out.println("------------Thanks for using our Multiplication Table System!!!------------");
    }
}

/*
Output:
Enter a number whose multiplication Table you want : 5
------------Multiplication Table of 5------------
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
------------Thanks for using our Multiplication Table System!!!------------

 */

Enhanced Loop

Enhanced loop is another version of for loop in java which was introduce in java 5. Using enhanced loop, iteration through the  the elements of a collection or array becomes more simpler. It is not flexible and should be used only when there is a need to iterate through the elements in sequential manner without knowing the index of currently processed element.

Syntax

for (datatype variable_name : Collection obj/array)
{
   //body of enhanced for loop
}

Example

You can find the code here

package com.qacaffe.examples.loops;

public class EnhancedFor {
    public static void main(String[] args) {
        System.out.println("Example of enhanced for Loop.");
        String[] fruits = {"Mango", "Banana", "Kiwi", "Apples"};
        for (String elements : fruits) {
            System.out.println(elements);
        }
    }
}

/*
Output:
Example of enhanced for Loop.
Mango
Banana
Kiwi
Apples
 */

Rutu Shah