Branching Statements
Branching Statements
break statement:
terminates a statement sequence in a switch statement
used to exit a loop
Example 1:
class Example
{
public static void main(String[] args)
{
for(int i=0; i<100; i++)
{
if(i == 10)
break; System.out.println("i: " + i);
}
System.out.println("Loop completed");
}
}
Labeled break Statement
And, the control goes to the first statement right after the loop.
Concept
class Example Output:
{
public static void main(String[] args)
{ Outer 0
outer:
for(int i=0; i<3; i++)
Inner 0
{ Bye
System.out.println("Outer "+ i);
inner:
Inner 1
for(int j=0; j<3; j++) Bye
{
System.out.println("Inner "+j);
Inner 2
if(i== j+1) Bye
break outer;
System.out.println("Bye");
Outer 1
} Inner 0
}
}
}
NOTE
}
}
}
Ans:A
Output??
public class Main
{ A. 1
public static void main (String[]args)
{ B. 2
int i=1,j=1;
label1: C. 1 2
while(i<=2)
{ D. 2 2
i++;
while(j<=2)
{
j++;
System.out.println(i);
if(i==j)
break label1;
}
}
}
}
Ans:B