
A Java loop is used to repeat a section of code several times. Sometimes we know how many iterations there are, and sometimes we don’t. For instance, if you want to display numbers from 1 to 10, you can use a loop, and if you want to print anything several times, you can also use a java loop.
The looping procedure is often divided into four steps: first, we set and initialize the counter, second we execute the statement in the loop, third the loop’s test condition, and fourth we increment the counter.
The Java loop not only reduces the number of similar statements that must be written, but it also minimizes the amount of code that must be written.
Table of Contents
Types of Loops in Java
There are mainly three basic types of loops in Java which are as follows:
- While loop
- Do-while loop
- for loop
While Loop in Java
The while loop runs a block of code until the specified condition is satisfied. If you use a boolean type parameter in a while loop, you will get a compile time error. You should use a while loop if you don’t know the number of iterations ahead of time.
Syntax of while loop in Java
while (expression) {
// Statements (Code to be executed)
}
While loop program in java
Here is an example of a while loop in which the number from 1 to 10 is displayed with a newline. We initialize the counter outside the body, then verify the condition inside the parenthesis, and update the counter inside the loop.
public class WhileLoopDemo {
public static void main(String[] args) {
int count = 1;
while (count <= 10) {
System.out.println(count);
count++;
}
}
}
Output: 1 2 3 4 5 6 7 8 9 10
Do-while Loop in Java
At the bottom of the loop, the do-while checks its expression. Use a do-while loop if the number of iterations isn’t fixed and you want to run the loop body at least once.
Even if the condition is false, the control flow will arrive to the loop body in do-while.
Do while loop syntax in java
do {
// Statements (Code to be executed)
} while (expression);
Do-while loop program in java
public class DoWhileLoopDemo {
public static void main(String[] args) {
int count = 1;
do {
System.out.println(count);
count++;
} while (count <= 10);
}
}
Output: 1 2 3 4 5 6 7 8 9 10
For Loop in Java
The for loop is used to go over a set of data. If you know how many times you want to iterate a section of code, loop is the way to go.
The nice thing about for loop is that you can use it to define numerous initialization variables, conditions, and counters.
Syntax of for loop in Java
for (initialization; condition; increment/decrement) {
// Statements (Code to be executed)
}
In the above syntax, you can see that for loop has four parts:
- initialization : The value we set to the counter at the start of the loop.
- condition : This part determines whether or not the counter meets the condition. If the condition is satisfied, the loop will continue; otherwise, control will exit the loop.
- increment/decrement : The counter rises or decreases by the value we provide in this section..
- Statements : This is the section of the code that is looped around.
For loop program in java
public class ForLoopDemo {
public static void main(String[] args) {
int count;
for (count = 1; count <= 10; count++) {
System.out.println(count);
}
}
}
Output: 1 2 3 4 5 6 7 8 9 10
Loop Control Statements in Java
The loop control statement changes the flow of the loop’s regular sequence of execution. For example, the loop may skip an iteration and control could leave the loop.
Following control statements are available in Java:
- break statement: It breaks the loop and control comes out of the loop.
- continue statement : It skips the current iteration of the loop.
In most situations, break and continue statements with an if condition are used in everyday programming. If we wish to break the loop after five iterations, we would use an if condition and a similar continue statement. In the switch case, we can also use the break statement to exit the switch block.
In Conclusion, I hope you gained a fundamental understanding of loops in Java via the use of syntax and examples. You may now tailor the loop to your own circumstance or demand.