Loop control in Java
Loops are used for execute a statement or a group of statement more than one times . Loops are
1. while loop :
Execute a statement or group of statement depend on condition .
If condition is true . Let's see an example
public class LoopControl
{
public void whileloop()
{
int i=0;
while(i<10){ // here check the condition , if value less
i++; // than 10 then statement will execute .
System.out.println(i);
}
}
public static void main(String[] args)
{
LoopControl obj = new LoopControl();
obj.whileloop();
}
}
2. do-while loop :
It's like a while loop except the condition shall check at the
end of loop body . Let's see an example
public class LoopControl
{
public void dowhileloop()
{
int i=0;
do{
i++;
System.out.println(i);
}while(i<10);
}
public static void main(String[] args)
{
LoopControl obj = new LoopControl();
obj.dowhileloop();
}
}
3. for loop :
For loop executes group of Java statements as long as the
boolean condition evaluates to true.
public class LoopControl
{
public void forloop()
{
for(int i=0;i<10;i++){
System.out.println(i);
}
}
public static void main(String[] args)
{
LoopControl obj = new LoopControl();
obj.forloop();
}
}
Find us :
Facebook : @apnaandroid
Google+ : Apna Java
YouTube : Android & Java Tutorial
1. while loop :
Execute a statement or group of statement depend on condition .
If condition is true . Let's see an example
public class LoopControl
{
public void whileloop()
{
int i=0;
while(i<10){ // here check the condition , if value less
i++; // than 10 then statement will execute .
System.out.println(i);
}
}
public static void main(String[] args)
{
LoopControl obj = new LoopControl();
obj.whileloop();
}
}
2. do-while loop :
It's like a while loop except the condition shall check at the
end of loop body . Let's see an example
public class LoopControl
{
public void dowhileloop()
{
int i=0;
do{
i++;
System.out.println(i);
}while(i<10);
}
public static void main(String[] args)
{
LoopControl obj = new LoopControl();
obj.dowhileloop();
}
}
3. for loop :
For loop executes group of Java statements as long as the
boolean condition evaluates to true.
public class LoopControl
{
public void forloop()
{
for(int i=0;i<10;i++){
System.out.println(i);
}
}
public static void main(String[] args)
{
LoopControl obj = new LoopControl();
obj.forloop();
}
}
Find us :
Facebook : @apnaandroid
Google+ : Apna Java
YouTube : Android & Java Tutorial
Comments
Post a Comment