GFG Day 5: Java Loops

sri parthusri parthu
3 min read
  • Loops in Java allow you to execute a block of code multiple times, either a fixed number of times or while a condition is true. In Java, there are four types of loops, each with its own syntax and method of checking the condition, but the use case is the same.

Loops in Java

  • In Java there are four types of loops listed below:

    • for loop

    • for each loop

    • while loop

    • do-while loop

  1. for loop

  • for loop is used when you know exactly how many times you want to execute the block of code.

Syntax

for (initialization; condition; increment/decrement) {

// code to be executed

}

Forloop

  • Initialization: Typically, the variable will be initialized, and the statement is executed once before the loop starts.

  • Condition: This expression is evaluated before each iteration, and if the condition is true, the loop executes; if it is false, the loop stops.

  • Increment/Decrement: It is usually to increment or decrement the variable when the statement is executed after each iteration.

The below Java program demonstrates a for loop that prints numbers from 0 to 10 in a single line.

// file name: forloop.java
public class forloop{
    public static void main (String[] args) {
        for(int num = 0; num <=10; num++){
            System.out.println("num: " + num);
        }
    }
}

Output

num: 0
num: 1
num: 2
num: 3
num: 4
num: 5
num: 6
num: 7
num: 8
num: 9
num: 10
  1. For each loop

  • For each loop is the updated version of the for loop, and it is mainly used for arrays and collections.

Syntax

for (dataType variable : arrayOrCollection) {

// code to be executed

}

The below Java program demonstrates an enhanced for loop (for each loop) to iterate through an array and print names.

// file name: foreach.java

public class foreach{
    public static void main (String[] args) {
        int[] numbers = {3, 9, 5, -5};
        for (int number : numbers) {
        System.out.println(number);
        }

    }
}

Output

3
9
5
-5
  1. while loop

  • While loop is used when the number of iterations isn't known in advance. It repeats the block as long as the condition is true.

Syntax

while (condition) {

// code to be executed

}

While-loop

The below Java program demonstrates a while loop that prints numbers from 0 to 10 in a single line.

// file name: whileloop.java

public class whileloop{
    public static void main (String[] args) {
        int i = 0;
        while (i <= 10) {
            System.out.print(i + " ");
            i++;
        }

    }
}

Output

0 1 2 3 4 5 6 7 8 9 10
  1. do-while loop

  • The do-while loop ensures that the code block executes at least once before checking the condition.

Syntax

do {

// code to be executed

} while (condition);

Do-while-loop

The below Java program demonstrates a do-while loop that prints numbers from 0 to 10 in a single line.

// file name: dowhileloop.java

public class dowhileloop{
    public static void main (String[] args) {
        int i = 0;
        do {
            System.out.print(i + " ");
            i++;
        } while (i <= 10);

    }
}

Output

0 1 2 3 4 5 6 7 8 9 10
  • The difference between a while loop and a do-while loop is that a while loop checks the condition first and only executes the code if the condition is true. In contrast, a do-while loop executes the code once before checking the condition, and if the condition is true, it will proceed to the next iteration.

Happy Learning

Thanks For Reading! :)

SriParthu ๐Ÿ’๐Ÿ’ฅ

0
Subscribe to my newsletter

Read articles from sri parthu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

sri parthu
sri parthu

Hello! I'm Sri Parthu! ๐ŸŒŸ I'm aiming to be a DevOps & Cloud enthusiast ๐Ÿš€ Currently, I'm studying for a BA at Dr. Ambedkar Open University ๐Ÿ“š I really love changing how IT works to make it better ๐Ÿ’ก Let's connect and learn together! ๐ŸŒˆ๐Ÿ‘‹