Describe loops in java.
What are loops in java?
Loops in java
In Java, a loop is a control flow statement that allows you to repeat a block of code a certain number of times or until a certain condition is met. There are several types of loops in Java:
for loop:
This is used to iterate a block of code a fixed number of times. The syntax is as follows:
for (initialization; termination; increment) {
// statement(s);
}
while loop:
This is used to execute a block of code repeatedly as long as a certain condition is true. The syntax is as follows:
while (condition) {
// statement(s);
}
do-while loop:
This is similar to a while loop, but the block of code is always executed at least once before the condition is checked. The syntax is as follows:
do {
//statement(s);
} while (condition);
for-each loop:
for-each loop is used to iterate through the elements of an array or a collection. The syntax is as follows:
for (type var : array)
{
statements using var;
}
Comments
Post a Comment