1. Introduction: Why Do We Need Loops?
Have you ever needed to do something over and over in your code—like print “Hello World” 100 times?
Would you really sit there and write this line 100 times?
java
System.out.println("Hello World");
Of course not! That’s where loops come to your rescue. Loops help you repeat tasks quickly and efficiently, saving time and effort.
2. The for
Loop: The Workhorse of Repetition
Concept:
Use a for
loop when you know exactly how many times you want something to repeat.
Syntax Breakdown:
java
for (initialization; condition; increment) {
*// code block*
}
-
Initialization: Start a counter—usually
int i = 0;
-
Condition: Keep going while
condition
is true—likei < 5
-
Increment: Move to the next step—like
i++
Example: Print numbers from 1 to 5
java
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
*// Output: 1 2 3 4 5*
Easy, right? No matter how many times you need, just adjust the numbers!
3. The while
Loop: Condition-Based Repetition
Concept:
Use a while
loop when you don’t know beforehand how many times you'll need to repeat something, but you have a condition to check.
Syntax Breakdown:
java
while (condition) {
*// code block*
}
Example: Keep asking a user for the number 5
`java
Scanner scanner = new Scanner(System.in);
int number;
do {
System.out.print("Enter the number 5 to continue: ");
number = scanner.nextInt();
} while (number != 5);
System.out.println("Thank you!");`
The loop repeats until the user finally enters 5.
4. The do-while
Loop: Guaranteed to Run Once
Concept:
A do-while
loop always runs at least once, because it checks the condition after running the code block.
Syntax Breakdown:
java
do {
*// code block*
} while (condition);
Example: Menu Program
`java
int choice;
do {
System.out.println("1. Say Hello");
System.out.println("2. Exit");
Scanner scanner = new Scanner(System.in);
choice = scanner.nextInt();
if (choice == 1) {
System.out.println("Hello!");
}
} while (choice != 2);
System.out.println("Goodbye!");`
Even if the user wants to exit, they see the menu once first!
5. The Enhanced for-each
Loop
Concept:
When you want to visit every element of an array or collection, use the for-each
loop.
No more worrying about indexes!
Syntax:
java
for (Type element : collection) {
*// use element*
}
Example: Print all elements of an array
java
int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
System.out.println(num);
}
*// Output: 1 2 3 4 5*
So clean and easy!
6. Keywords that Control Loops: break
and continue
break: Instantly stops the entire loop.
Example: Looking for a value in an array—leave as soon as you find it!
java
int[] arr = {2, 4, 6, 8, 10};
for (int num : arr) {
if (num == 6) {
System.out.println("Found 6!");
break;
}
}
*// Output: Found 6!*
continue: Jumps to the next round, skipping the rest of the loop’s body this time.
Example: Print only even numbers from 1-5
java
for (int i = 1; i <= 5; i++) {
if (i % 2 != 0) {
continue; *// Skip odd numbers!*
}
System.out.println(i);
}
*// Output: 2 4*
7. Conclusion: Choose the Right Loop
- Use a for loop when you know how many times you want to repeat.
- Use a while loop when it depends on a condition (don’t know how many times).
- Use a do-while loop when you always want the code inside to run at least once.
- Use for-each for collections and arrays.
- Use
break
andcontinue
to control what’s happening in your loops (but don’t over-use them—they can make code harder to read if used everywhere).
Try this out in your code! Experiment, make mistakes, see what happens—that’s the fastest way to learn and grow. And if you ever get confused, don’t worry: everyone did at some point, including me. I’m always here to help you untangle any loop, big or small! 😊🌻
Top comments (0)