Loops
Loops let you repeat a block of code multiple times. They are essential for traversing arrays, processing data, and implementing algorithms.
for loop
Best when you know the number of iterations.
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
while loop
When condition is checked before execution.
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
do-while loop
Executes at least once.
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
Enhanced for loop (for-each)
For iterating over arrays/collections.
int[] numbers = {1,2,3};
for (int num : numbers) {
System.out.println(num);
}
break and continue
break exits the loop; continue skips the current iteration.Two Minute Drill
- for: when iteration count known.
- while: when condition based, may execute zero times.
- do-while: executes at least once.
- Enhanced for: simplifies array/collection traversal.
- break and continue control loop flow.
Need more clarification?
Drop us an email at career@quipoinfotech.com
