Jumping Statements
1)Break
- The one use of break in switch statement we have seen.
- It can be used to break other loops also.
- When you use break in a loop it terminates the program.
Example
For(int i=1;i<100;i++)
{
System.out.println(“ i is “+i);
If(i==5)
{
System.out.println(“ i cannot continue”);
Break;
}
}
2)Continue
- The continue statement, skips the current iteration in the loop and continues the loop to the next iteration.
- So, when we want to skip some statements based on a condition, continue statement can be used.
Example
For(int i=1;i<100;i++)
{
System.out.println(“ i is “+i);
If(i%3==0||i%5==0)
{
Continue;
System.out.println(“no is “+i”);
}
}
3)Return
- The return statement causes the control to be transferred back from a method to the caller of the method.
Example
Int i=1;
While(i<10)
{
If(i==3)
Return;
System.out.println(“I = “+i);
I++;
}
No comments:
Post a Comment