Access over 35 million academic & study documents

A) Discuss when do you use a break statement Provide an example

Content type
User Generated
Showing Page:
1/3
A) Discuss when do you use a break statement? Provide
an example?
Solution
ANS:
Break Statement:
Break statement is one of the several control statements
Java provide to control the flow of the program. As the
name says, Break Statement is generally used to break the
loop of switch statement.
Break statement has two forms labeled and unlabeled.
Unlabeled Break statement
This form of break statement is used to jump out of the
loop when specific condition occurs. This form of break
statement is also used in switch statement.
For example,
for(int var =0; var < 5 ; var++)
{
System.out.println(Var is : + var);
if(var == 3)

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/3
break;
}
In above break statement example, control will jump out of
loop when var becomes 3.
Labeled Break Statement
The unlabeled version of the break statement is used when
we want to jump out of a single loop or single case in
switch statement. Labeled version of the break statement
is used when we want to jump out of nested or multiple
loops.
For example,
Outer:
for(int var1=0; var1 < 5 ; var1++)
{
for(int var2 = 1; var2 < 5; var2++)
{
System.out.println(var1: + var1 + , var2: + var2);
if(var1 == 3)
break Outer;
}
}

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/3

Sign up to view the full document!

lock_open Sign Up
Unformatted Attachment Preview
A) Discuss when do you use a break statement? Provide an example? Solution ANS: Break Statement: Break statement is one of the several control statements Java provide to control the flow of the program. As the name says, Break Statement is generally used to break the loop of switch statement. Break statement has two forms labeled and unlabeled. Unlabeled Break statement This form of break statement is used to jump out of the loop when specific condition occurs. This form of break statement is also used in switch statement. For example, for(int var =0; var < 5 ; var++) { System.out.println(“Var is : “ + var); if(var == 3) break; } In above break statement example, control will jump out of loop when var becomes 3. Labeled Break Statement The unlabeled version of the break statement is used when we want to jump out of a single loop or single case i n switch statement. Labeled versi ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4