JAVA LOOPS
FOR
LOOP
When you know exactly
how many times you want to loop through a block of code.
Syntax
for (statement
1; statement
2; statement
3)
{
// code block to be execute
}
// code block to be execute
}
EXAMPLE
FOR LOOP
Public class test
{
Public ctatic void main(string args[])
{
for (int i = 0; i < 5; i++)
{
System.out.println(i);
}
System.out.println(i);
}
}
}
Output
0
1
2
3
4
WHILE
LOOP
Loops can execute a
block of code as long as a specified condition is reached.
Syntax
while (condition)
{
// code block to be executed
}
// code block to be executed
}
WHILE
EXAMPLE
Public class test
{
public static
void main(String args [])
{
int i=0;
while(i==5)
{
System.out.println(“hai”);
i++;
}
}
}
DO
WHILE
This loop will
execute the code block once, before checking if the condition is true, then it
will repeat the loop as long as the condition is true.
Syntax
do {
// code block to be executed
}
while (condition);
// code block to be executed
}
while (condition);
DO
WHILE EXAMPLE
int i
= 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
do {
System.out.println(i);
i++;
}
while (i < 5);
Nice
ReplyDelete