JAVA Tutorial



LOOPS IN JAVA


Loops in Java

Loops are used in Java to execute a block of code repeatedly. There are three primary types of loops in Java:

  • for loop
  • while loop
  • do-while loop

πŸ”΅ The for Loop

The for loop is used when you know in advance how many times you want the loop to run. It consists of three parts:

  • Initialization: Where the loop variable is initialized.
  • Condition: The condition that is checked before each iteration.
  • Increment/Decrement: How the loop variable changes after each iteration.

πŸ’‘ Example: For Loop

The following example demonstrates how to print numbers from 1 to 5 using a for loop:

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

πŸ”΅ The while Loop

The while loop is used when you don’t know how many times you need to loop. The condition is evaluated before each iteration. The loop continues as long as the condition is true.

πŸ’‘ Example: While Loop

Here's an example of using a while loop to print numbers from 1 to 5:

public class WhileLoopExample {
    public static void main(String[] args) {
        int i = 1;
        while (i <= 5) {
            System.out.println(i);
            i++;
        }
    }
}
  

πŸ”΅ The do-while Loop

The do-while loop is similar to the while loop, but the condition is checked after the code block has executed. This means the code inside the do block is always executed at least once.

πŸ’‘ Example: Do-While Loop

In this example, the loop will print the numbers from 1 to 5:

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

πŸ”΄ Key Differences Between Loops

- **For loop**: Best when you know how many times you need to iterate. - **While loop**: Best when the condition may change during execution and you don’t know how many iterations are needed. - **Do-While loop**: Ensures that the code block is executed at least once, even if the condition is false initially.

βš™οΈ Nested Loops

You can use loops inside other loops. This is called a "nested loop." Here’s an example of a nested for loop to print a multiplication table:

public class NestedLoopExample {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 5; j++) {
                System.out.print(i * j + " ");
            }
            System.out.println();
        }
    }
}
  

πŸ’‘ Infinite Loop

An infinite loop occurs when the condition of the loop is always true. Here’s an example:

public class InfiniteLoop {
    public static void main(String[] args) {
        while (true) {
            System.out.println("This is an infinite loop!");
        }
    }
}
  

Practice Challenge: Write a program using a loop to sum all the even numbers from 1 to 100.

πŸ”΄ Key Takeaways

  • The for loop is useful when the number of iterations is known in advance.
  • The while loop is better when the number of iterations is not known.
  • The do-while loop ensures that the block of code is executed at least once.
  • You can use nested loops for complex operations like printing multiplication tables.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review