continue and break statement
continue :
Continue statement is used to skip the execution of code in loop . Continue statement is used only inside the loop control(while,do-while,for) in java . continue is a keyword in java.
break :
break statement is used to terminate the execution of code. break is a keyword in java.
Let's see an example of continue and break statement.
Without continue and break statement (Fig-1):
public class ContinueBreakExample
{
public static void main(String[] args)
{
for(int i=0;i<3;i++){
System.out.println(i);
}
}
}
Output :
0
1
2
With continue statement (Fig-2):
public class ContinueBreakExample
{
public static void main(String[] args)
{
for(int i=0;i<3;i++){
if(i==1)
{
continue;
}
System.out.println(i);
}
}
}
Output :
0
2
With break statement (Fig-3) :
public class ContinueBreakExample
{
public static void main(String[] args)
{
for(int i=0;i<3;i++){
if(i==1)
{
break;
}
System.out.println(i);
}
}
}
Output :
0
In the above three example i have used same class with some little change . In Fig-1 example , for loop prints all value because there are no continue or break statement has been used . In Fig-2 example, i have been used continue statement . so first print "0" and then "1" is skip and control goes to next part that means print "2" . But last one (Fig-3) only "0" prints because of break statement . In break statement , control does not go to next part it terminates the execution.
Find us :
Facebook : @apnaandroid
Google+ : Apna Java
Youtube : Android & Java Tutorial
Continue statement is used to skip the execution of code in loop . Continue statement is used only inside the loop control(while,do-while,for) in java . continue is a keyword in java.
break :
break statement is used to terminate the execution of code. break is a keyword in java.
Let's see an example of continue and break statement.
Without continue and break statement (Fig-1):
public class ContinueBreakExample
{
public static void main(String[] args)
{
for(int i=0;i<3;i++){
System.out.println(i);
}
}
}
Output :
0
1
2
With continue statement (Fig-2):
public class ContinueBreakExample
{
public static void main(String[] args)
{
for(int i=0;i<3;i++){
if(i==1)
{
continue;
}
System.out.println(i);
}
}
}
Output :
0
2
With break statement (Fig-3) :
public class ContinueBreakExample
{
public static void main(String[] args)
{
for(int i=0;i<3;i++){
if(i==1)
{
break;
}
System.out.println(i);
}
}
}
Output :
0
In the above three example i have used same class with some little change . In Fig-1 example , for loop prints all value because there are no continue or break statement has been used . In Fig-2 example, i have been used continue statement . so first print "0" and then "1" is skip and control goes to next part that means print "2" . But last one (Fig-3) only "0" prints because of break statement . In break statement , control does not go to next part it terminates the execution.
Find us :
Facebook : @apnaandroid
Google+ : Apna Java
Youtube : Android & Java Tutorial
Comments
Post a Comment