Home > Java > Javagetting Started > Usage analysis of break and continue

Usage analysis of break and continue

王林
Release: 2020-07-07 16:35:22
forward
3445 people have browsed it

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:

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:

        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:

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!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template