You are on page 1of 20

LOOP Statement

Java
Three basic loops:

• for
• while
• do-while
Syntax of for loop:

for(initialization; condition ; increment/decrement)


{
statement(s);
}
Example of Simple for loop
class ForLoopExample
{
public static void main(String args[]){
for(int i=10; i>1; i--)
{
System.out.println("The value of i is: "+i);
}
}}
The output of this program is:
The value of i is: 10
The value of i is: 9
The value of i is: 8
The value of i is: 7
The value of i is: 6
The value of i is: 5
The value of i is: 4
The value of i is: 3
The value of i is: 2
Syntax of while loop

while(condition)
{
statement(s);
}
Simple while loop example
class WhileLoopExample {
public static void main(String args[]){
int i=10;
while(i>1)
{
System.out.println(i);
i--;
}
}}
The output of this program is:
10
9
8
7
6
5
4
3
2
Syntax of do-while loop:
do
{
statement(s);
}
while(condition);
do-while loop example
class DoWhileLoopExample {
public static void main(String args[]){
int i=10;
do {
System.out.println(i); i--;
}
while(i>1);
}
}
Output:
10
9
8
7
6
5
4
3
2
Title and Content Layout with Chart
Two Content Layout with Table
• First bullet point here
Group 1 Group 2
• Second bullet point here
Class 1 82 95
• Third bullet point here
Class 2 76 88

Class 3 84 90
Two Content Layout with SmartArt
• First bullet point here

• Second bullet point here Task


• Third bullet point here
1

Group
A
Task Task
3 2

You might also like