break Statement
An unlabeled break transfers control to the statement after the innermost enclosing loop or switch.
In Java, break exits the nearest enclosing loop or switch statement. A labeled break can exit a named enclosing statement.
Stop before printing 3
class Main {
public static void main(String[] args) throws Exception {
for (int n = 1; n <= 4; n++) {
if (n == 3) {
break;
}
System.out.println(n);
}
}
}
1 2
A print queue has ten credits and must serve jobs in arrival order. Jobs costing four and three leave three credits. The next job costs eight, so the queue stops before the smaller job behind it.
An unlabeled break transfers control to the statement after the innermost enclosing loop or switch.
A labeled break exits the statement carrying that label. This is useful when a match should end a search across nested loops.
Search the array for 4 and stop at the first match.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 1-dim integer array
int[] arr = {2,4,6};
// Declare and Initialize the row index variable
int index = 0;
int findValue = 4;
boolean result = false;
// The 1-dim array is 1x3 matrix so the array's index should be 0,1,2
while(index < 3) {
System.out.println("Index: " + index);
// Once matching found update the result flag to true
// and terminate the current execution
if(arr[index] == findValue) {
result = true;
System.out.println("Break statement reached");
break;
}
// Increment the index by 1 using Post Increment
index++;
}
if(result == true) {
System.out.println("The number " + findValue + " found");
} else {
System.out.println("The number " + findValue + " not found");
}
}
}
Index: 0 Index: 1 Break statement reached The number 4 found
Search the rows for 10. An unlabeled break exits only the inner loop.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 2-dim integer array
int[][] arr = {
{2,4,6},
{5,10,15},
{9,18,27}
};
// Declare and Initialize the row index variable
int row = 0;
int findValue = 10;
boolean result = false;
// The 2-dim array is 3x3 matrix so the array's row index should be 0,1,2
while(row < 3) {
System.out.println("Outer Iteration - Row: " + row);
// Declare and Initialize the column index variable
int column = 0;
// The 2-dim array is 3x3 matrix so the array's column index should be 0,1,2
while(column < 3) {
System.out.println("Inner Iteration - Column: " + column);
// Once matching found update the result flag to true
// and terminate the current execution
if(arr[row][column] == findValue) {
result = true;
System.out.println("Break statement reached");
break;
}
// Increment the column index by 1 using Post Increment
column++;
}
// Increment the row index by 1 using Post Increment
row++;
}
if(result == true) {
System.out.println("The number " + findValue + " found");
} else {
System.out.println("The number " + findValue + " not found");
}
}
}
Outer Iteration - Row: 0 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Inner Iteration - Column: 2 Outer Iteration - Row: 1 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Break statement reached Outer Iteration - Row: 2 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Inner Iteration - Column: 2 The number 10 found
Use a labeled break to stop searching all rows after finding 10.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 2-dim integer array
int[][] arr = {
{2,4,6},
{5,10,15},
{9,18,27}
};
// Declare and Initialize the row index variable
int row = 0;
int findValue = 10;
boolean result = false;
// Label
outer:
// The 2-dim array is 3x3 matrix so the array's row index should be 0,1,2
while(row < 3) {
System.out.println("Outer Iteration - Row: " + row);
// Declare and Initialize the column index variable
int column = 0;
// The 2-dim array is 3x3 matrix so the array's column index should be 0,1,2
while(column < 3) {
System.out.println("Inner Iteration - Column: " + column);
// Once matching found update the result flag to true
// and terminate the current execution
if(arr[row][column] == findValue) {
result = true;
System.out.println("Labeled Break statement reached");
break outer;
}
// Increment the column index by 1 using Post Increment
column++;
}
// Increment the row index by 1 using Post Increment
row++;
}
if(result == true) {
System.out.println("The number " + findValue + " found");
} else {
System.out.println("The number " + findValue + " not found");
}
}
}
Outer Iteration - Row: 0 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Inner Iteration - Column: 2 Outer Iteration - Row: 1 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Labeled Break statement reached The number 10 found
Search the array for 4 and stop at the first match.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 1-dim integer array
int[] arr = {2,4,6};
// Declare and Initialize the row index variable
int index = 0;
int findValue = 4;
boolean result = false;
// The 1-dim array is 1x3 matrix so the array's index should be 0,1,2
do {
System.out.println("Index: " + index);
// Once matching found update the result flag to true
// and terminate the current execution
if(arr[index] == findValue) {
result = true;
System.out.println("Break statement reached");
break;
}
// Increment the index by 1 using Post Increment
index++;
} while(index < 3);
if(result == true) {
System.out.println("The number " + findValue + " found");
} else {
System.out.println("The number " + findValue + " not found");
}
}
}
Index: 0 Index: 1 Break statement reached The number 4 found
Search the rows for 10. An unlabeled break exits only the inner loop.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 2-dim integer array
int[][] arr = {
{2,4,6},
{5,10,15},
{9,18,27}
};
// Declare and Initialize the row index variable
int row = 0;
int findValue = 10;
boolean result = false;
// The 2-dim array is 3x3 matrix so the array's row index should be 0,1,2
do {
System.out.println("Outer Iteration - Row: " + row);
// Declare and Initialize the column index variable
int column = 0;
// The 2-dim array is 3x3 matrix so the array's column index should be 0,1,2
do {
System.out.println("Inner Iteration - Column: " + column);
// Once matching found update the result flag to true
// and terminate the current execution
if(arr[row][column] == findValue) {
result = true;
System.out.println("Break statement reached");
break;
}
// Increment the column index by 1 using Post Increment
column++;
} while(column < 3);
// Increment the row index by 1 using Post Increment
row++;
} while(row < 3);
if(result == true) {
System.out.println("The number " + findValue + " found");
} else {
System.out.println("The number " + findValue + " not found");
}
}
}
Outer Iteration - Row: 0 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Inner Iteration - Column: 2 Outer Iteration - Row: 1 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Break statement reached Outer Iteration - Row: 2 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Inner Iteration - Column: 2 The number 10 found
Use a labeled break to stop searching all rows after finding 10.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 2-dim integer array
int[][] arr = {
{2,4,6},
{5,10,15},
{9,18,27}
};
// Declare and Initialize the row index variable
int row = 0;
int findValue = 10;
boolean result = false;
// Label
outer:
// The 2-dim array is 3x3 matrix so the array's row index should be 0,1,2
do {
System.out.println("Outer Iteration - Row: " + row);
// Declare and Initialize the column index variable
int column = 0;
// The 2-dim array is 3x3 matrix so the array's column index should be 0,1,2
do {
System.out.println("Inner Iteration - Column: " + column);
// Once matching found update the result flag to true
// and terminate the current execution
if(arr[row][column] == findValue) {
result = true;
System.out.println("Labeled Break statement reached");
break outer;
}
// Increment the column index by 1 using Post Increment
column++;
} while(column < 3);
// Increment the row index by 1 using Post Increment
row++;
} while(row < 3);
if(result == true) {
System.out.println("The number " + findValue + " found");
} else {
System.out.println("The number " + findValue + " not found");
}
}
}
Outer Iteration - Row: 0 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Inner Iteration - Column: 2 Outer Iteration - Row: 1 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Labeled Break statement reached The number 10 found
Search the array for 4 and stop at the first match.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 1-dim integer array
int[] arr = {2,4,6};
int findValue = 4;
boolean result = false;
// The 1-dim array is 1x3 matrix so the array's index should be 0,1,2
for(int index = 0; index < 3; index++) {
System.out.println("Index: " + index);
// Once matching found update the result flag to true
// and terminate the current execution
if(arr[index] == findValue) {
result = true;
System.out.println("Break statement reached");
break;
}
}
if(result == true) {
System.out.println("The number " + findValue + " found");
} else {
System.out.println("The number " + findValue + " not found");
}
}
}
Index: 0 Index: 1 Break statement reached The number 4 found
Search the rows for 10. An unlabeled break exits only the inner loop.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 2-dim integer array
int[][] arr = {
{2,4,6},
{5,10,15},
{9,18,27}
};
int findValue = 10;
boolean result = false;
// The 2-dim array is 3x3 matrix so the array's row index should be 0,1,2
for(int row = 0; row < 3; row++) {
System.out.println("Outer Iteration - Row: " + row);
// The 2-dim array is 3x3 matrix so the array's column index should be 0,1,2
for(int column = 0; column < 3; column++) {
System.out.println("Inner Iteration - Column: " + column);
// Once matching found update the result flag to true
// and terminate the current execution
if(arr[row][column] == findValue) {
result = true;
System.out.println("Break statement reached");
break;
}
}
}
if(result == true) {
System.out.println("The number " + findValue + " found");
} else {
System.out.println("The number " + findValue + " not found");
}
}
}
Outer Iteration - Row: 0 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Inner Iteration - Column: 2 Outer Iteration - Row: 1 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Break statement reached Outer Iteration - Row: 2 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Inner Iteration - Column: 2 The number 10 found
Use a labeled break to stop searching all rows after finding 10.
class ControlFlowStatement {
public static void main(String[] args) {
// Declare and Initialize the 2-dim integer array
int[][] arr = {
{2,4,6},
{5,10,15},
{9,18,27}
};
int findValue = 10;
boolean result = false;
// Label
outer:
// The 2-dim array is 3x3 matrix so the array's row index should be 0,1,2
for(int row = 0; row < 3; row++) {
System.out.println("Outer Iteration - Row: " + row);
// The 2-dim array is 3x3 matrix so the array's column index should be 0,1,2
for(int column = 0; column < 3; column++) {
System.out.println("Inner Iteration - Column: " + column);
// Once matching found update the result flag to true
// and terminate the current execution
if(arr[row][column] == findValue) {
result = true;
System.out.println("Labeled Break statement reached");
break outer;
}
}
}
if(result == true) {
System.out.println("The number " + findValue + " found");
} else {
System.out.println("The number " + findValue + " not found");
}
}
}
Outer Iteration - Row: 0 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Inner Iteration - Column: 2 Outer Iteration - Row: 1 Inner Iteration - Column: 0 Inner Iteration - Column: 1 Labeled Break statement reached The number 10 found
Select a greeting for Python and break after the matching case.
class ConditionalStatement {
public static void main(String[] args) {
String language = "Java";
switch(language) {
case "C":
System.out.println("Welcome to the world of 'C'");
break;
case "Java":
System.out.println("Welcome to the world of 'Java'");
break;
case "Kotlin":
System.out.println("Welcome to the world of 'Kotlin'");
break;
default:
System.out.println("None of the language selected");
}
}
}
Welcome to the world of 'Java'
Select the Java greeting for a basic subscription using nested switch statements.
class ConditionalStatement {
public static void main(String[] args) {
String language = "Java";
String subscription = "basic";
switch(language) {
case "C":
switch(subscription) {
case "basic":
System.out.println("Subscription for basics of 'C' language is $15");
break;
case "advanced":
System.out.println("Subscription for advanced concepts in 'C' language is $30");
break;
default:
System.out.println("None of the subscription selected on 'C' language");
}
System.out.println("Thanks for choosen the 'C' language");
break;
case "Java":
switch(subscription) {
case "basic":
System.out.println("Subscription for basics of 'Java' language is $20");
break;
case "advanced":
System.out.println("Subscription for advanced concepts in 'Java' language is $35");
break;
default:
System.out.println("None of the subscription selected on 'Java' language");
}
System.out.println("Thanks for choosen the 'Java' language");
break;
case "Kotlin":
switch(subscription) {
case "basic":
System.out.println("Subscription for basics of 'Kotlin' language is $20");
break;
case "advanced":
System.out.println("Subscription for advanced concepts in 'Kotlin' language is $40");
break;
default:
System.out.println("None of the subscription selected on 'Kotlin' language");
}
System.out.println("Thanks for choosen the 'Kotlin' language");
break;
default:
System.out.println("None of the language selected");
}
}
}
Subscription for basics of 'Java' language is $20 Thanks for choosen the 'Java' language
Use a labeled break to leave the enclosing switch after selecting the greeting.
class ConditionalStatement {
public static void main(String[] args) {
String language = "Java";
String subscription = "basic";
// Label
lang:
switch(language) {
case "C":
switch(subscription) {
case "basic":
System.out.println("Subscription for basics of 'C' language is $15");
break lang;
case "advanced":
System.out.println("Subscription for advanced concepts in 'C' language is $30");
break lang;
default:
System.out.println("None of the subscription selected on 'C' language");
}
System.out.println("Thanks for choosen the 'C' language");
break;
case "Java":
switch(subscription) {
case "basic":
System.out.println("Subscription for basics of 'Java' language is $20");
break lang;
case "advanced":
System.out.println("Subscription for advanced concepts in 'Java' language is $35");
break lang;
default:
System.out.println("None of the subscription selected on 'Java' language");
}
System.out.println("Thanks for choosen the 'Java' language");
break;
case "Kotlin":
switch(subscription) {
case "basic":
System.out.println("Subscription for basics of 'Kotlin' language is $20");
break lang;
case "advanced":
System.out.println("Subscription for advanced concepts in 'Kotlin' language is $40");
break lang;
default:
System.out.println("None of the subscription selected on 'Kotlin' language");
}
System.out.println("Thanks for choosen the 'Kotlin' language");
break;
default:
System.out.println("None of the language selected");
}
}
}
Subscription for basics of 'Java' language is $20
Reference: Java language reference