The use of jump statements in java
There are three jump structures in java: break continue return
break: used to exit from any statement block.
1. It ends the entire loop and jumps to the end of the loop;
eg: Outputs a loop from 1 to 10, but stops when it is greater than 2 and a multiple of 3
Java code
public static void main(String[] args){ for(int i=1;i<10;i++){ if(i>2&&i%3==0){ break;} System.out.println(i); } System.out.println("结束");} //输出结果就是1,2,结束。
2. In the switch statement Jump to the end of switch;
eg: Xiao Ming ran second in the school sports meeting. What was the reward he got?
Java code
public static void main(String[] args){ int paiming i=2; switch(paiming){ case 1: System.out.println("冠军"); break; case 2: System.out.println("亚军"); break; case 3: System.out.println("季军"); break; default: System.out.println("什么都没有!!"); }} //输出的结果就是“亚军”;在判断排名之后就会直接执行case 对应的数值,在break跳出整个switch。
3. Define an alias for the for loop, and then use the break alias; it means jumping to the end of the specified outer loop.
eg: Output * when there are 5 characters in the line, the outer label will pop up;
public class ForLoop{ public static void main(String[] args){ outer:for(int i=0;i<5;i++){ for(int j=0;j<10;j++){ if(j==5) break outer; System.out.print("*"); } System.out.print("\r\n"); } } //输出:*****。break 别名 直接跳出别名的循环。
Return: End the entire function and jump to the end of the function
eg: Output an even number from 1 to 10, when it is greater than 5 is the end.
public class uuu { public static void main(String[] args){ for(int i=1;i<10;i++){ if(i%3==0){ System.out.println(i); } if(i>5){ return; } } } } //输出结果:2 4 6。当输出到6的时候判断到大于5就return结束了这个函数。
eg: Output numbers from 1 to 6, but 3 cannot be output.
public class one{ public static void main(String[] args){ for(int i=1;i<=6;i++){ if(i==3){ continue; } System.out.println(i); } } } // 输出的结果:1,2,4,5,6.只有3不会输出,continue是结束当前次的循环。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
