Java中 Break 关键字的使用
Break关键字通常用于循环控制结构和 switch 语句中。它用于在Java中终止循环和 switch 语句。当在循环中遇到 break 关键字时,循环立即终止,程序控制转到循环后的下一条语句。当在嵌套循环中使用 break 关键字时,只有内部循环会被终止。甚至与 if 语句一起使用以在满足特定条件时终止。
break 关键字在 switch 语句中有特殊用途。 switch 语句中的每个 case 后跟一个 break 关键字,这样无论何时程序控制跳转到下一个 case,它都不会执行后续 case。
实景图解:
Consider a person climbing stairs. Now the red line here is depicting the stair on which his/her shoe slips and he/she rolls backs to the base. Remember if he/she slips, he/she will always be landing on the base.
Here in computers in programming language there is a keyword called ‘break’ which will abruptly stop the further execution of all the statements following it defined in that scope.
句法:
break 关键字和分号
break;
Break语句流程图
Break 关键字的使用主要出现在循环概念中:Types of loops Usage of ‘break’ keyword Simple loop While the goal of insertion, delete, or updation is completed and there itself wants the program to terminate Nested loop When the user wants to take command from innermost loop to outer loop Infinite loop When the program enters into an infinite looping state
案例 1:在 FOR 循环中使用 Break 关键字
为了展示如何在 For 循环中使用 break 关键字。考虑到当 'i' 的值变为 39 时,break 关键字将发挥其作用并终止 for 循环。 'i' 39 之前的所有值都显示在结果中。
Java
// Java Program to show use of break statement
// inside the For loop
public class GFG {
// Main driver code
public static void main(String[] args)
{
// For loop for iteration
for (int i = 3; i <= 50; i += 3) {
if (i == 39) {
// Using Break keyword to suspend
// loop when i become 39
break;
}
// Printing elements showcasing break usage
System.out.print(i + " ");
}
}
}
Java
// Java Program to illustrate the use of break statement
// inside the While loop
// Importing generic Classes/Files
import java.utl.*;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// While loop for iteration
// Creating and initializing variable
// outside loop
int i = 35;
// Condition check
while (i >= 10) {
if (i == 15) {
// Using Break keyword to suspend
// loop when i become 15
// Decrementing variable initialized above
i--;
// Break statement
break;
}
// Printing elements showcasing break
System.out.print(i + " ");
i--;
}
}
}
Java
// Java Program to illustrate the use of break statement
// Inside the do-While loop
// Importing all generic classes
import java.util.*;
// Importing specific class to take
// user input from the user
import java.util.Scanner;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Using do-While loop
// Creating and initializing a variable
// over which execution occurs
int i = 0;
// do loop consisting of executable statements
do {
if (i == 80) {
// Incrementing variable by 5
i += 5;
// Using Break keyword to
// suspend loop when i become 80
break;
}
System.out.print(i + " ");
// Printing elements to show break usage
i += 5;
}
// Condition check
while (i <= 100);
}
}
Java
// Java Program to illustrate the use of break statement
// inside the Switch Statement
// Importing generic Classes/Files
import java.util.*;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a variable for switch expression
int numb = 20;
// Switch expression
switch (numb) {
// Case statements
case 10:
System.out.println("TEN");
// Break keyword to suspend the switch case
// if given condition is fulfilled
break;
case 20:
System.out.println("TWENTY");
break;
case 30:
System.out.println("THIRTY");
break;
// Default case statement
default:
System.out.println("INFINITE");
}
}
}
输出:
3 6 9 12 15 18 21 24 27 30 33 36
案例 2:WHILE 循环中的 Break 关键字
为了展示如何在 While 循环中使用 break 关键字。考虑到 'i' 的值变为 15 时,break 关键字将发挥其作用并终止 for 循环。 'i' 大于 15 到 35 的所有值都显示在结果中。
Java
// Java Program to illustrate the use of break statement
// inside the While loop
// Importing generic Classes/Files
import java.utl.*;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// While loop for iteration
// Creating and initializing variable
// outside loop
int i = 35;
// Condition check
while (i >= 10) {
if (i == 15) {
// Using Break keyword to suspend
// loop when i become 15
// Decrementing variable initialized above
i--;
// Break statement
break;
}
// Printing elements showcasing break
System.out.print(i + " ");
i--;
}
}
}
输出:
35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
情况 3:在 DO-WHILE 循环中使用 Break 关键字
为了展示如何在 Do-while 循环中使用 break 关键字。考虑到当 'i' 的值变为 80 时,break 关键字就会发挥作用并终止 for 循环。 80 之前 i”的所有值都显示在结果中。
Java
// Java Program to illustrate the use of break statement
// Inside the do-While loop
// Importing all generic classes
import java.util.*;
// Importing specific class to take
// user input from the user
import java.util.Scanner;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Using do-While loop
// Creating and initializing a variable
// over which execution occurs
int i = 0;
// do loop consisting of executable statements
do {
if (i == 80) {
// Incrementing variable by 5
i += 5;
// Using Break keyword to
// suspend loop when i become 80
break;
}
System.out.print(i + " ");
// Printing elements to show break usage
i += 5;
}
// Condition check
while (i <= 100);
}
}
输出:
0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75
案例 4:Switch 语句中的 Break 关键字
为了展示如何在 For 循环中使用 break 关键字。在每个 switch case 的底部使用 break 关键字来终止每个语句序列并防止 switch-case 语句的混合。
Java
// Java Program to illustrate the use of break statement
// inside the Switch Statement
// Importing generic Classes/Files
import java.util.*;
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Declaring a variable for switch expression
int numb = 20;
// Switch expression
switch (numb) {
// Case statements
case 10:
System.out.println("TEN");
// Break keyword to suspend the switch case
// if given condition is fulfilled
break;
case 20:
System.out.println("TWENTY");
break;
case 30:
System.out.println("THIRTY");
break;
// Default case statement
default:
System.out.println("INFINITE");
}
}
}
输出:
TWENTY