How to write a program for Looping in core Java ?

Program for “while loop” in core java :

Hello Friends, 
 
Today I will give you an example for writing a program for use “while loop” in core java.
 
Code:

public class whiledemo {
public static void main(String args[])
{
int count=1,i=0;
while(count<=9)
{
i=i+1;
System.out.println(“the value of i=”+i);
count++;
}
}
}

output :
 
the value of i=1
the value of i=2
the value of i=3
the value of i=4
the value of i=5
the value of i=6
the value of i=7
the value of i=8
the value of i=9

Program for break statement in core java :

Hello Friends, 
 
Today I will give you an example for writing a program for use “break statement” in core java.
 
Code:
public class breakdemo {
public static void main(String args[])
{
for(int i=1;i<=20;i++)
{
if(i%10==0)
{
System.out.println(“Number”+i+”is divisible by 10”);
break;
}
else
{
System.out.println(“Number “+i+”is not divisible by 10”);
}
}
}
}
 
output :
 
Number 1is not divisible by 10
Number 2is not divisible by 10
Number 3is not divisible by 10
Number 4is not divisible by 10
Number 5is not divisible by 10
Number 6is not divisible by 10
Number 7is not divisible by 10
Number 8is not divisible by 10
Number 9is not divisible by 10
Number10is divisible by 10
 
 

Program for “for loop” in core java :

Hello Friends, 
 
Today I will give you an example for writing a program for use “for loop” in core java.


Code:

public class forloopdemo {

public static void main (String args[])
{
for(int i=0;i<=5;i++)
System.out.println(“The value of i=”+i);
 
}
}
output :
 
The value of i=0
The value of i=1
The value of i=2
The value of i=3
The value of i=4
The value of i=5

Leave a Reply

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