Using Continue statement in Java
Sometimes we want to force an early iteration of the loop.That is we might want to continue running the loop but escaping the execution for few conditions.To skip some iteration of the loop we can use continue statement.Its opposite of break.break lets us come out of loop and switch statements whereas continue keeps us inside with leaving some conditional iterations.
For example if we want to print the odd numbers only then we may skip the iterations of even numbers.For this purpose we can use continue statement.
Let us consider below mentioned example:-
For example if we want to print the odd numbers only then we may skip the iterations of even numbers.For this purpose we can use continue statement.
Let us consider below mentioned example:-
public class TestContinue{
public static void main(String a[]){
for(int i=0;i<10;i++){
if(i%2==0)
continue;
System.out.println("The Odd Number is :- "+i);
}
}
}
NOTE:-continue statement can only be used inside a loop.It cannot be used inside switch or other blocks.
Couple of things about continue to Remember:-
1.In while and do-while loop a continue statement causes control to be transferred immediately to the conditional expression that controls the loop.
2.In case of for loop ,continue statement takes the control first to the loop iteration and then to the conditional expression.All the statements written after continue statement are bypassed and loop continues the iteration.
Couple of things about continue to Remember:-
1.In while and do-while loop a continue statement causes control to be transferred immediately to the conditional expression that controls the loop.
2.In case of for loop ,continue statement takes the control first to the loop iteration and then to the conditional expression.All the statements written after continue statement are bypassed and loop continues the iteration.
Related Links:-
Java Finalize method
Java Break statement
How to use break as GOTO
How to use command line arguments


















0 comments:
Post a Comment
Post a Comment