break Statement
The break statement is a termination statement and its used to terminate the innermost while, do-while, for, or switch statement even though the expression is valid (i.e. true).
Java break statement is a branching statement in a control flow statement and it will terminate the iteration of a loop even though the looping expression is valid (i.e., true). There are two types of Break Statement.
The break statement is a termination statement and its used to terminate the innermost while, do-while, for, or switch statement even though the expression is valid (i.e. true).
The Labeled break statement is a termination statement and it's used to terminate the outermost while, do-while, for, or switch statement even though the expression is valid (i.e. true). Moreover the labeled break statement will be used commonly in the nested loops to break the outermost loop.
Find the value 4 in 1-dimensional array and terminate the loop using simple break statement only if the value exist.
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");
}
}
}
Find the value 10 in 2-dimensional array and terminate the loop using simple break statement only if the value exist.
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");
}
}
}
Find the value 10 in 2-dimensional array and terminate the loop using labeled break statement only if the value exist.
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");
}
}
}
Find the value 4 in 1-dimensional array and terminate the loop using simple break statement only if the value exist.
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");
}
}
}
Find the value 10 in 2-dimensional array and terminate the loop using simple break statement only if the value exist.
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");
}
}
}
Find the value 10 in 2-dimensional array and terminate the loop using labeled break statement only if the value exist.
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");
}
}
}
Find the value 4 in 1-dimensional array and terminate the loop using simple break statement only if the value exist.
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");
}
}
}
Find the value 10 in 2-dimensional array and terminate the loop using simple break statement only if the value exist.
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");
}
}
}
Find the value 10 in 2-dimensional array and terminate the loop using labeled break statement only if the value exist.
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");
}
}
}
Print the greeting message for the language Python using switch statement with break statement.
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");
}
}
}
Print the greeting message for the language Java for the basic subscription using nested switch statement with break statement.
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");
}
}
}
Print the greeting message for the language Java for the basic subscription using nested switch statement with labeled break statement without a 'Thanks' greeting message.
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");
}
}
}