Looping Statements in Java – Explained in Simple Words with Examples


Looping Statements in Java

Explained in Simple Words with Examples

Introduction

When we want to do something again and again in a program, we use loops. For example, if you want to print "Hello" 100 times, you don't have to write it 100 times. You can just use a loop.

In Java, looping statements help us repeat a block of code multiple times. It saves time and makes the code clean, simple, and powerful.

Let's learn the different types of loops in Java, how they work, and where to use them.

Types of Loops in Java

Java mainly has three types of loops:

  • for loop
  • while loop
  • do-while loop

Let's understand each one step by step.

for loop

Syntax of for loop

for(initialization; condition; update) {
  // code to be executed
}
  • Initialization: Start the loop (like setting a counter)
  • Condition: Check if we should keep looping
  • Update: Change the counter (increase/decrease)

Example of for loop

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

Output:

Hello 1
Hello 2
Hello 3
Hello 4
Hello 5

Easy, right?

while loop

Syntax of while loop

while(condition) {
  // code to be executed
}

The while loop checks the condition first, then runs the code.

Example of while loop

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

do-while loop

Syntax of do-while loop

do {
  // code to be executed
} while(condition);

In do-while, the loop runs at least once even if the condition is false.

Example of do-while loop

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

Loop Control Statements

These are special keywords that help control the loop flow.

break statement

What is break statement?
break is used to exit the loop immediately, even if the condition is still true.

Example with break

for(int i = 1; i <= 10; i++) {
  if(i == 5)
    break;
  System.out.println(i);
}

Output:

1
2
3
4

continue statement

What is continue statement?
continue skips the current iteration and jumps to the next.

Example with continue

for(int i = 1; i <= 5; i++) {
  if(i == 3)
    continue;
  System.out.println(i);
}

Output:

1
2
4
5

(3 is skipped!)

return statement

What is return statement?
return ends the method and returns to the caller.

Example with return

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

Output:

1
2

Once return is hit, the program exits the main method.

Difference between for, while, and do-while loops

Loop Type
Entry/Exit Check
Executes At Least Once
Best Use Case
for
Entry
No
Known number of times
while
Entry
No
Unknown iterations
do-while
Exit
Yes
One-time execution

Common Mistakes in Loops

  1. Infinite Loop: Forgetting to update the loop variable
  2. Wrong Condition: Using = instead of ==
  3. Incorrect Update: Increasing when you should decrease

Nested Loops

A loop inside another loop is called a nested loop.

Example

for(int i = 1; i <= 3; i++) {
  for(int j = 1; j <= 2; j++) {
    System.out.println("i=" + i + ", j=" + j);
  }
}

Loops with Arrays

We often use loops to access array elements.

Example

int[] numbers = {10, 20, 30, 40};
for(int i = 0; i < numbers.length; i++) {
  System.out.println(numbers[i]);
}

Real-Life Use Cases of Loops

  • Printing Patterns: Stars, number pyramids, etc.
  • Searching: Finding an item in a list
  • Repeating Process: Like retrying a login or showing a menu again

Conclusion

Loops in Java are like magic tools. They help reduce repetitive code and make your program smarter. Whether you're printing a message, working with arrays, or creating complex patterns — loops are there to help.

Once you understand for, while, do-while, and control statements like break, continue, and return, you're ready to write cleaner and more powerful Java programs.

FAQs

1. What is the main purpose of a loop in Java?
To repeat a block of code multiple times based on a condition.

2. Can we use break in all types of loops?
Yes, break works in for, while, and do-while.

3. Is do-while better than while?
Not always. Use do-while when you want the code to run at least once.

4. Can we use multiple loops inside one another?
Yes, it's called nested loops and it's very common.

5. Why is return used in loops?
To stop the method and go back to where it was called — useful in many cases.


Post a Comment

0 Comments