Program to demonstrate the usage of 'break' statement in
'switch' selection statements
class SwitchBreak
{
public static void
main(String args[])
{
int a=1;
switch(a)
{
case 0:
System.out.println("Case 0");
break;
case 1:
System.out.println("Case 1");
break;
default:
System.out.println("Default Case");
}
}
}
Output of this program:
Case 1
Observe that after printing the 'Case 1' text, the program
tries to move to the next cases. But as we've mentioned break; statement, the
switch case will be terminated and instead of going to the next case i.e.
default
Using break statement to exit a 'for' loop
class ForBreak
{
public static void
main(String args[])
{
int a=3;
for(int
i=1;i<5;i++)
{
if(i==a)
break;
System.out.println("The value of i is "+i);
}
}
}
Output of this program:
The value of i is 1
The value of i is 2
i.e. when the value of i is equal to the value of a i.e. 3,
the program will exit the 'for' loop because of the break statement written.
But we've to understand the break was not designed to
provide a normal means by which a loop is terminated. Instead the break
statement should be used to cancel a loop only when some sort of special
situations occurs. If you go through this program, we have used break statement
to exit the 'for' loop when the special situation if(i==a) is occurred.
Using a break statement to exit a 'while' loop
class WhileLoopBreak
{
public static void
main(String args[])
{
int i=0;
while(i<100)
{
if(i==10)
break;
System.out.print(i+"
");
i++;
}
}
}
Output of this program:
0 1 2 3 4 5 6 7 8 9
Since the while loop got braked by the break; statement when
the value of i reached 10, so the program printed values of i from 0 to 9
,though it has the scope to print the values till 99.
Using a break statement to exit a do-while loop
class DoWhileLoopBreak
{
public static void
main(String args[])
{
int i=0;
do{
if(i==10)
break;
System.out.print(i+"
");
i++;
}while(i<100);
}
}
Output of this program:
0 1 2 3 4 5 6 7 8 9
Since the Do-while loop got braked by the break; statement
when the value of i reached 10, so the program printed values of i from 0 to 9
,though it has the scope to print the values till 99.
Using break to exit the nested loops
class BreakNestedLoops
{
public static void
main(String args[])
{
first: for(int
i=0;i<10;i++) //Labelled this
loop as 'first'
{
second: for(int
j=0;j<10;j++) //Labelled this
loop as 'second'
{
Third: for(int
k=0;k<10;k++) //Labelled this loop as 'third'
{
if((i+j+k)==1)
break first; //This will break
the entire nested loop if the condition specified in the 'if' statement is met,
since we've specified to break the loop labelled with 'first'
System.out.println("The value of i+j+k is "+(i+j+k));
}
}
}
}
}
Output of this program:
The value of i+j+k is 0
Break the inner loop
class BreakInnerLoop
{
public static void
main(String args[])
{
first: for(int
i=0;i<5;i++) //Labelled this
loop as 'first'
{
second: for(int
j=0;j<5;j++) //Labelled this
loop as 'second'
{
if(j>3)
break second; //This will break
the inner loop if the condition specified in the 'if' statement is met, since
we've specified to break the loop labelled with 'second'
System.out.print(j+" ");
}
System.out.println();
}
}
}
Output of this program:
0 1 2 3
0 1 2 3
0 1 2 3
0 1 2 3
0 1 2 3
If you observe clearly when the program execution enter the
inner loop, it breaks when the value of j is 4, hence it wont print the value 4
in the output. Each time it the value of j becomes 4, the program execution
breaks and the control goes to the outer loop labelled 'first' in this example
and start its execution by incrementing the 'i' value
No comments:
Post a Comment