Looping Statements

Looping statements are a fundamental part of programming, allowing us to execute a block of code repeatedly. In Java, there are three types of looping statements: for, while, and do-while. Each of these statements has its own purpose and syntax. Let's dive into each one in more detail.
For Loop
The for loop is commonly used for iterating over arrays, collections, and numerical ranges. Its syntax consists of an initialization, a condition, and an alteration.
for (initialization; condition; alteration) {
// code block to be executed
}
The initialization expression is usually used to initialize the loop variable, while the condition is a boolean expression that tests whether the loop should continue or not. The alteration expression is used to increment or decrement the loop variable.
Here's an example of a for loop that prints the first 10 integers:
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
While Loop
The while loop is used for looping until a certain condition is met. Its syntax consists of a boolean expression that is tested at the beginning of the loop.
while (condition) {
// code block to be executed
// incerement/decrement
}
Here's an example of a while loop that prints the first 10 integers:
int i = 1;
while (i <= 10) {
System.out.println(i);
i++;
}
Copy
Do-While Loop
The do-while loop is similar to the while loop, but its condition is checked at the end of the loop. This means that the code block inside the loop will always execute at least once.
do {
// code block to be executed
// increment/decrement
} while (condition);
Here's an example of a do-while loop that prints the first 10 integers:
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 10);
Breaking and Continuing in Loops
Within a loop, we can use the break and continue statements to modify the flow of execution.
The break statement is used to terminate a loop premanently. When used inside a loop, it immediately exits the loop and continues with the next statement outside of the loop.
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // terminates the loop early
}
System.out.println(i);
}
The continue statement is used to skip over the remaining statements in the loop and continue with the next iteration. In the example below, the loop will only print the odd numbers.
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // skips even numbers
}
System.out.println(i);
}
Conclusion
In conclusion, looping statements are essential constructs in Java that allow us to execute a block of code repeatedly. They can be used to iterate over collections, numerical ranges, and many other use cases. Understanding the differences between for, while, and do-while loops will help you write efficient and effective code. For more blogs and posts on Java programming language stay tuned....
About the author
I am currently an undergraduate in Computer Science and Engineering . I am currently working on my skills in java language and I also having knowledge of C language. Looking forward to improve my skills in android development and making some interesting projects .
Comments
Join the discussionNo comments yet. Be the first to comment.




