Professional Documents
Culture Documents
A550092402 - 25127 - 30 - 2020 - 06. Branching Statements
A550092402 - 25127 - 30 - 2020 - 06. Branching Statements
• break Statement
• continue Statement
• return Statement
break Statement
Example 1:
public void breakTest()
{
for(int i=0; i<100; i++)
{
if(i == 10) break;
System.out.println("i: " + i);
}
System.out.println("Loop completed");
}
Example
public void switchTest() {
for(int i=0; i<5; i++)
switch(i) {
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;
default:
System.out.println("i is greater than 2.");
}
}
Labeled break Statement
The data type of the returned value must match the type of
the method's declared return value.
}
Brainstorming 3
public class ContinueExample2 {
public static void main(String[] args) {
//outer loop
for(int i=1;i<=3;i++){
//inner loop
for(int j=1;j<=3;j++){
if(i==2&&j==2){
//using continue statement inside inner loop
continue;
}
System.out.println(i+" "+j);
}
} }
}
Brainstorming 4
class BreakTest{
public static void main(String [] rk)
{
hello:
for(int a=1; a<3; a++)
System.out.print("Hello");
int i = 1;
if(i==1)
break hello;
System.out.print("Not reachable");
}
}
Brainstorming 5
public static void main(String [] rk)
{ int n=5;
outer: for(int a=1; a<5; a++)
{ int i=0, j=0; System.out.println();
space: while(true) {
System.out.print(" "); i++;
if(i==n-a) break space; }
star: while(true) {
System.out.print(" * "); j++;
if(j==a) continue outer;
}
}
}