Detailed explanation of break statement of Java (25) from zero

Introduction to the author Author name: Ming Shiyin in programming world Introduction: CSDN blog expert has been engaged...
Example 1
Example 2
Example 1
Example 2
Example 3
Example 4
Introduction to the author

Author name: Ming Shiyin in programming world
Introduction: CSDN blog expert has been engaged in software development for many years and is proficient in Java and JavaScript. Bloggers also learn and grow step by step from scratch, know the importance of learning and accumulation, and like to fight and upgrade with ADC. Welcome to pay attention and look forward to learning, growing and taking off with you!

Navigation

✪ learn Java series Catalog Index from scratch
◄ previous article    [24] foreach statement
► next    [26] detailed explanation of continue statement

Popular column recommendation

[1] Java games (Tetris, aircraft war, plant war, zombie, etc.)
[2] JavaWeb project practice (library management, online examination, dormitory management, etc.)
[3] Wonderful examples of JavaScript (aircraft war, snake, verification code, etc.)
[4] Introduction to Java Xiaobai 200 cases
[5] Learn Java from scratch and be interested in learning Java
[6] Idea from zero to mastery

introduction

♀ Little AD: brother Ming, I'm coming!
♂ Mingshiyin: come and learn Java.
♀ Little AD: No, I just don't want to learn new knowledge today. I'm not in shape today.
♂ Ming Shiyin: why not.
♀ Little AD: it was blown up.
♂ Ming Shiyin: Oh, really. Brother Ming is not here. I can't stand it.
♀ Little AD: mainly because I was scolded by others, I was very angry and quit the game directly.
♂ Ming Shiyin: it seems inappropriate for you to withdraw from the game.
♀ Little AD: I don't care about them. I'll quit if I want. I'm in charge of my game.
♂ Ming Shiyin: well said, well done. Being a man is to be free and easy.
♀ Little AD: I can be the same as brother Ming.
♂ Ming Shiyin: actually, it's not difficult. It's the same as learning programs. Just learn to quit actively.
♀ Little AD: take the initiative to quit. I seem to have contacted it before.
♂ Ming Shiyin: Yes, there was no dead cycle before. Set the exit condition and use the break statement?
♀ Little AD: Well, I'm impressed.
♂ Ming Shiyin: let's deepen our impression and learn more about break today.

describe

Sometimes it is necessary to forcibly terminate the loop when a certain condition occurs, rather than wait until the loop condition is false to exit the loop. At this point, you can use break to complete this function.

Break is used to completely end a loop and jump out of the loop body. No matter what kind of loop, once a break is encountered in the loop body, the system will completely end the loop and start executing the code after the loop.

Using break in switch

Example 1

import java.util.Calendar; public class Demo2 { public static void main(String[] args) { String weekDate = ""; Calendar calendar = Calendar.getInstance(); // Get current time int week = calendar.get(Calendar.DAY_OF_WEEK) - 1; // Gets the day of the week switch (week) { case 0: weekDate = "Sunday"; break; case 1: weekDate = "Monday"; break; case 2: weekDate = "Tuesday"; break; case 3: weekDate = "Wednesday"; break; case 4: weekDate = "Thursday"; break; case 5: weekDate = "Friday"; break; case 6: weekDate = "Saturday"; break; } System.out.println("Today is " + weekDate);} }

Operation results:

Today is Wednesday

♂ Ming Shiyin: first of all, today is Wednesday (the day to test the code).
♀ Little AD: because break is added after case 3, the switch will be exited when you come here, right?
♂ Mingshiyin: Yes, it's very clear. You see, a break is added in each case to ensure that you exit after execution.
♀ Little AD: what if I don't add break?
♂ Mingshiyin: just adjust this break.

Example 2

import java.util.Calendar; public class Demo2 { public static void main(String[] args) { String weekDate = ""; Calendar calendar = Calendar.getInstance(); // Get current time int week = calendar.get(Calendar.DAY_OF_WEEK) - 1; // Gets the day of the week switch (week) { case 0: weekDate = "Sunday"; break; case 1: weekDate = "Monday"; break; case 2: weekDate = "Tuesday"; break; case 3: weekDate = "Wednesday"; case 4: weekDate = "Thursday"; break; case 5: weekDate = "Friday"; break; case 6: weekDate = "Saturday"; break; } System.out.println("Today is " + weekDate);} }

Operation results:

Today is Thursday

♀ Little AD: why, I can run on Thursday.
♂ Ming Shiyin: after case 3 matches, weekDate is set to "Wednesday", and then case 4 will be executed because there is no break, so weekDate is set to "Thursday" again.
♀ Small AD: then, break will be executed here on Thursday, so this result will be output in the end.
♂ Ming Shiyin: Yes, so the program is wrong.
♀ Little AD: Well, I see. Pay attention to the use of break in switch.
♂ Ming Shiyin: that's what I mean.

break in while
public class Test { public static void main(String[] args) { int num=0; while (true) { //Print System.out.println("Print numbers:"+num); //Increasing num++; //Add active exit conditions if(num>5){ break; } } } }

Operation results:

Print number: 0
Print number: 1
Print number: 2
Print number: 3
Print number: 4
Print number: 5

Analysis: if break is not added to this code, it will be an endless loop.

Use break in a for loop

Example 1

for loop without break.

public class Test2 { public static void main(String[] args) { //for loop printing for (int i = 0; i < 10; i++) { System.out.println("Print:"+i); } } }

Operation results:

Print: 0
Print: 1
Print: 2
Print: 3
Print: 4
Print: 5
Print: 6
Printing: 7
Printing: 8
Print: 9

If I want to stop printing after 5, I need to use the break statement at this time.

Example 2

Use the break statement in the for loop (print to 5 and exit)

public class Test2 { public static void main(String[] args) { //for loop printing for (int i = 0; i < 10; i++) { System.out.println("Print:"+i); if(i==5){ break; } } } }

Print: 0
Print: 1
Print: 2
Print: 3
Print: 4
Print: 5

♀ Little AD: I'm not happy with this game. Just quit?
♂ Ming Shiyin: Yes, it's a similar effect.
♀ Little AD: so I understand. It means that the later ones are filtered out without need, saving time.
♂ Ming Shiyin: it saves performance.

Example 3

If break is used in a nested for loop, it will only jump out of the current loop.

public class Test2 { public static void main(String[] args) { // External circulation, 5 times for (int i = 0; i < 5; i++) { System.out.print("The first" + (i + 1) + "Second cycle:"); // Internal circulation, designed as 10 cycles for (int j = 0; j < 10; j++) { // Judge whether j is equal to 3. If so, terminate the cycle if (j == 3) { break; } System.out.print("Second stage of internal circulation" + (j + 1) + "Secondary cycle\t"); } System.out.println(); } } }

Operation results:

1st cycle: 1st cycle of internal cycle 2nd cycle of internal cycle 3rd cycle of internal cycle
2nd cycle: 1st cycle of internal cycle 2nd cycle of internal cycle 3rd cycle of internal cycle
The third cycle: the first cycle of the internal cycle, the second cycle of the internal cycle, and the third cycle of the internal cycle
Cycle 4: the first cycle of the internal cycle, the second cycle of the internal cycle, and the third cycle of the internal cycle
Cycle 5: the first cycle of the internal cycle, the second cycle of the internal cycle, and the third cycle of the internal cycle

♂ Ming Shiyin: it can be seen that after the condition j==3 is met in the inner loop, the loop is exited.
♀ Little AD: but the external circulation is still 5 times.
♂ Ming Shiyin: because break will only jump out of the current cycle.
♀ Little AD: then I want to jump out of the loop.
♂ Ming Shiyin: there are also ways.

Example 4

Use break label syntax to implement goto like functions

public class Test3 { public static void main(String[] args) { // External circulation out:for (int i = 0; i < 5; i++) { System.out.print("The first" + (i + 1) + "Second cycle:"); // Internal circulation, designed as 10 cycles for (int j = 0; j < 10; j++) { // Judge whether j is equal to 3. If so, terminate the cycle if (j == 3) { break out; } System.out.print("Second stage of internal circulation" + (j + 1) + "Secondary cycle\t"); } System.out.println(); } } }

Operation results:

1st cycle: 1st cycle of internal cycle 2nd cycle of internal cycle 3rd cycle of internal cycle

Analysis: we use out to name the entire code block of the outer layer. When j==3 is true, breaking out directly is equivalent to directly ending the entire loop code.

♀ Little AD: so break has so many uses, seconds.
♂ Ming Shiyin: so we should learn knowledge often. The more we know, the more we feel we need to learn. The more we learn, the more energetic we are.
♀ Little AD: it seems that there is such a thing. I don't really want to learn today, but it doesn't taste.
♂ Ming Shiyin: Hey, I'm addicted.
♀ Little AD: it's like playing a game. When you lose, you still have to continue to play.
♂ Mingshiyin: learning Java with this momentum can certainly become a great God.
♀ Little AD: I'm super God.

Summary

This section summarizes the "detailed explanation of break statements". I hope it can be helpful to you. Please help [like] + [collection] + [punch in the comment area]. If you are interested in learning Java with brother Xiao Ming, [pay attention to a wave] won't get lost.

Let me know you by punching in the comment area. Mingge will continue to pay attention to your learning progress!

Navigation

✪ learn Java series Catalog Index from scratch
◄ previous article    [24] foreach statement
► next    [26] detailed explanation of continue statement

Popular column recommendation

[1] Java games (Tetris, aircraft war, plant war, zombie, etc.)
[2] JavaWeb project practice (library management, online examination, dormitory management, etc.)
[3] Wonderful examples of JavaScript (aircraft war, snake, verification code, etc.)
[4] Introduction to Java Xiaobai 200 cases
[5] Learn Java from scratch and be interested in learning Java
[6] Idea from zero to mastery

3 November 2021, 14:57 | Views: 9077

Add new comment

For adding a comment, please log in
or create account

0 comments