Looping in programming languages is a feature which
facilitates the execution of a set of instructions/functions repeatedly while
some condition evaluates to true. Java provides three ways for executing the loops. While all the ways provide
similar basic functionality, they differ in their syntax and condition checking
time
while
loop: A
while loop is a control flow statement that allows code to be executed
repeatedly based on a given Boolean condition. The while loop can be thought of
as a repeating if statement.
Syntax :
while (boolean condition)
{
loop statements...
}
for
loop: for
loop provides a concise way of writing the loop structure. Unlike a while loop,
a for statement consumes the initialization, condition and increment/decrement
in one line thereby providing a shorter, easy to debug structure of looping.
Syntax :
for (initialization condition; testing condition;increment/decrement)
{
statement(s)
}
do while: do
while loop is similar to while loop with only difference that it checks for
condition after executing the statements.
Syntax :
do
{
statements..
}
while (condition);