Home Java Javagetting Started Usage analysis of break and continue

Usage analysis of break and continue

Jul 07, 2020 pm 04:35 PM
break continue

Usage analysis of break and continue

Introduction to the usage of break and continue:

(recommended learning: java entry program)

break

1. Break is used in a switch statement to terminate the switch statement;

2. When break is used in a loop, it jumps out of the loop;

(1) break is used In the switch statement, terminate the switch statement

Example:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

int a=4;

        switch(a){

            case 1:

                System.out.println("星期一");

            case 2:

                System.out.println("星期二");

            case 3:

                System.out.println("星期三");

            case 4:

                System.out.println("星期四");

                break;

            case 5:

                System.out.println("星期五");

            case 6:

                System.out.println("星期六");

            case 7:

                System.out.println("星期日");

            default:

                System.out.println("你输入的有误");

        }

Copy after login

Execution result:

Usage analysis of break and continue

(2) break When used in loops, jump out of the loop

Example:

1

2

3

4

5

6

int sum=0;

for(int i=1;i<=100;i++){

    if(i>10)

        break;

    sum+=i;

}

Copy after login

Execution result:

Usage analysis of break and continue

(Video tutorial recommended : java video tutorial)

continue

continue is used in a loop to jump out of this loop and continue to execute the next loop;

Example:

1

2

3

4

5

6

int i;

        for(i=1;i<10;i++){

            if(i==5)

                continue;

            System.out.print(i+"  ");

        }

Copy after login

Execution result:

Usage analysis of break and continue

You can see that when i=5, the continue key is encountered word, jump out of this loop, and continue to execute the loop after i=6.

The above is the detailed content of Usage analysis of break and continue. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The role and use of the continue keyword in PHP The role and use of the continue keyword in PHP Jun 28, 2023 pm 08:07 PM

The role and use of the continue keyword in PHP In PHP programming, continue is a very useful keyword. It is used to control the execution flow of loop statements, allowing to skip the remaining code in the current loop and directly enter the execution of the next loop. The function of continue is to skip the code in the current iteration in the loop statement and start the next iteration directly. When the continue statement is executed, loop control will immediately go to the beginning of the loop body without executing continue.

Does continue jump out of the current loop or all loops? Does continue jump out of the current loop or all loops? Feb 02, 2023 pm 04:20 PM

continue is to jump out of the current loop. The continue statement is used to skip this loop and execute the next loop; when encountering the continue statement, the program will immediately recheck the conditional expression. If the expression result is true, the next loop will start. If the expression result is false, Exit the loop.

Let's talk about not using break in PHP switch statement Let's talk about not using break in PHP switch statement Mar 20, 2023 pm 04:55 PM

It is very common to use switch statements to select multiple branches in PHP. Usually, a break statement is used to exit the switch statement after each branch. However, there are situations where we do not want to use the break statement. This article will introduce the situation of not using break in the PHP switch statement.

What is the function of break keyword in Java? What is the function of break keyword in Java? Apr 23, 2023 am 10:13 AM

Note 1. The function of break is to jump out of the current loop block (for, while, dowhile) or program block (switch). 2. The function of the loop block is to jump out of the loop body currently in the loop. The function of the program block is to interrupt and compare the next case condition. Use break in a switch statement to terminate the switch statement. When break is used in a loop, it breaks out of the loop. There is no point in using break elsewhere. Example intsum=0;inti;for(i=1;i

JS loop learning: break out of loop statements break and continue JS loop learning: break out of loop statements break and continue Aug 03, 2022 pm 07:08 PM

In the previous article, we took you to learn several loop control structures in JS (while and do-while loops, for loops). Let’s talk about the break and continue statements to jump out of the loop. I hope it will be helpful to everyone!

What is the usage of break in php What is the usage of break in php Jan 31, 2023 pm 07:33 PM

In PHP, break is used to jump out of the current syntax structure and execute the following statements; it can be used in statements such as switch, for, while, and do while. It can terminate the code of the loop body and jump out of the current loop immediately, and execute the following statements after the loop. code. The break statement can take a parameter n, which represents the number of levels to jump out of the loop. If you want to jump out of multiple loops, you can use n to represent the number of levels to jump out of. If there is no parameter, the default is to jump out of the current loop.

What is the use of break stop statement in Go language? What is the use of break stop statement in Go language? Jan 18, 2023 pm 03:46 PM

In the Go language, the break stop statement is used to jump out of the loop in a loop statement and start executing the statement after the loop. The break statement can end the code blocks of for, switch and select. In addition, the break statement can also add a label after the statement to indicate exiting the code block corresponding to a certain label. The label requirement must be defined on the corresponding code block of for, switch and select. .

The function and precautions of break statement in PHP The function and precautions of break statement in PHP Mar 20, 2024 pm 05:33 PM

The role and precautions of the break statement in PHP In PHP programming, the break statement is a control statement used to interrupt the execution of a loop or switch statement. The break statement can immediately jump out of the current loop or switch statement, allowing the program execution flow to jump directly to the code part after the loop or switch statement, thus improving the efficiency and flexibility of program execution. In actual programming, the break statement plays an important role, but it also requires attention to some details and precautions. 1.b

See all articles