Java Language / Java Looping Statements

It is a part of the program (sequence of instructions / part of code) which is repeated to perform a particular task
till a certain condition is meets. It is used save time, reduce errors and make code more readable.There are
3 looping Statements in java.They were

1. While Loop.
2. Do While Loop.
3. For Loop.

While Loop
It is used to repeat a Block / Lines of code to be executed till it meets a certain condition.But this loop first checks the
condition of the loop and then it enters into the code block.It the condition is false it comes out of the loop.

Syntax
while (condition)
{
// Block / Lines of code to be executed
}

Sample Program to print numbers Output
public class numbersprint
{
public static void main(String[] args)
{
int i = 0;
while (i < 5)
{
System.out.println(i);
i++;
}
}
}
0
1
2
3
4


Do While Loop
It is used to repeat a Block / Lines of code to be executed till it meets a certain condition. But in this loop first it
enter into the code block and then checks the condition of the loop till the condition is false it comes out of the loop.

Syntax
do
{
// Block / Lines of code to be executed
}
while (condition);


Sample Program to print numbers Output
public class numbersprint
{
public static void main(String[] args)
{
int i = 0;
do
{
System.out.println(i);
i++;
}
while (i < 5);
}
}
0
1
2
3
4


For Loop
It is used to repeat a Block / Lines of code to be executed till it meets a certain condition. But in this loop first it enter into the code block and then icrement / decrements the counters and thenchecks the condition of the loop till the condition is false it comes out of the loop.

Syntax
for (Initialization; Condition; Increment / Decrement)
{
// Block / Lines of code to be executed
}

Sample Program to print numbers Output
public class numbersprint
{
public static void main(String[] args)
{
for (int i = 0; i < 5; i++)
{
System.out.println(i);
}
}
}
0
1
2
3
4


Home     Back