Key Difference Break and Continue N Java-2
Key Difference Break and Continue N Java-2
The switch statement is java’s multiway branch statement. It provides an easy way to dispatch execution
to different parts of your code based on the value of an expression.
Example 1
package kiruba1;
switch(grade) {
case 'A' :
System.out.println("Excellent!");
break;
case 'B' :
case 'C' :
System.out.println("Well done");
break;
case 'D' :
System.out.println("You passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
}
Output
Better try again
Your grade is F
Example 2
package kiruba1;
case 0:
System.out.println("i is Zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is Two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than three.");
}
Output
i is Zero.
i is one.
i is Two.
i is three.
i is greater than three.
i is greater than three.
Example 3
package kiruba1;
case 0:
case 1:
case 2:
case 3:
case 4:
System.out.println( "i is less than 5");
break;
case 5:
case 6:
case 7:
case 8:
case 9:
System.out.println( "i is less than 10");
break;
default:
System.out.println( "i is 10 or more");
}
}
Output
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is 10 or more
i is 10 or more
Example 4
package kiruba1;
String season;
switch(month) {
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
case 11:
season = "Autum";
break;
default:
season = "Bogus Month";
}
Output
Jump statements
Java support three jump statements break, continue and return. These statements transfer
control to another part of your program.
Break
Example 1
package kiruba1;
// Using break to exit a loop
public class breakone {
}
// Although the for loop is designed to run from 0 to 99, the break statement
causes it to terminate early, when i equals 10
Output
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
loop complete
Example 2
package kiruba1;
int i = 0;
System.out.println("loop complete");
Output
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
loop complete
Example 3
package kiruba1;
//break statement will only break out of the innermost loop in inside a set
of nested loops
public class breakthree {
}
System.out.println("Loops complete.");
}
Output
Pass 0: 0 1 2 3 4
Pass 1: 0 1 2 3 4
Pass 2: 0 1 2 3 4
Loops complete.
Example 4
package kiruba1;
// Using break to exit from nested loops
public class breakfour {
}
System.out.println("Loops complete.");
}
Output
Pass 0: 0 1 2 3 4 Loops complete.
Continue
Example 1
package kiruba1;
// continue
public class continueone {
Output
1 3 5 7 9
Example 2
package kiruba1;
System.out.println();
}
}
Output
0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81
Return
Example
package kiruba1;
boolean t = true;
System.out.println("Before the return.");
if (t) return; // return to caller
//if (!t) return; // return to caller
System.out.println ("This won't execute.");
Output
Before the return.
{If (not t)
Output
According to the above program, the for loop iterates from 1 to 10. When the i value
becomes 6, the test expression becomes true. So, the break statement executes, and the
loop terminates. So, the value after 6 will not print. Only the value from 1 to 5 prints.
Main Purpose