Java Loop Example (while,for,do while)

Java program to show use of loops

Java Allow Three Loops ..
  • while
  • for
  • do while

[java]
class ForLoopDemo
{
public static void main(String args[])
{
int i;
for(i=1;i<=10;i++)
{
System.out.println("Value of loop i = " + i);
}
}
}
[/java]

[java]
class DoWhile
{
public static void main(String args[]){
int i = 1;
do
{
System.out.println("Value = " + i);
i+=1;
}while(i>=10);
}
}
[/java]

Java program to show use do while loop (exit control loop)

[java]

class DoWhileLoopDemo
{
public static void main(String args[])
{
int i = 25;
do
{
System.out.println("Loop = " + i);
i++;
}
while(i<=100);
}
}
[/java]

Loop Decrement

[java]

class DecForLoopDemo
{
public static void main(String args[])
{
int i;
for(i=10;i>=1;i–)
{
System.out.println("Value of loop i = " + i);
}
}
}

[/java]

[java]

class LoopDemo
{
public static void main(String args[]){
int i;
for(i=1;i<=10;i +=2)
{
System.out.println("Loop = " + i);
}

for(i = 10;i>=1;i–)
{
System.out.println("Loop = " + i);
}

}
}

[/java]

Share

About the Author

Akash Padhiyar

Visit Website

There are no comments yet, add one below.

Leave a Comment

Your email address will not be published. Required fields are marked *

*

Time limit is exhausted. Please reload CAPTCHA.