flow-control.md 15.9 KB
Newer Older
沉默王二's avatar
沉默王二 已提交
1
---
沉默王二's avatar
沉默王二 已提交
2
title: Java流程控制语句详解:带你轻松学会各类控制结构
3
shortTitle: Java流程控制语句
沉默王二's avatar
沉默王二 已提交
4
category:
沉默王二's avatar
沉默王二 已提交
5
  - Java核心
沉默王二's avatar
沉默王二 已提交
6
tag:
沉默王二's avatar
沉默王二 已提交
7
  - Java语法基础
沉默王二's avatar
沉默王二 已提交
8
description: 本文全面讲解了Java流程控制语句,包括if、switch、while、for等结构。通过学习本文,你将了解到Java流程控制语句的基本概念、语法结构和使用场景,帮助你在实际编程过程中更加灵活地运用各类控制结构。
9 10 11
head:
  - - meta
    - name: keywords
沉默王二's avatar
沉默王二 已提交
12
      content: Java, 流程控制语句, if, switch, while, for, 控制结构, 编程基础, 语法结构
沉默王二's avatar
沉默王二 已提交
13
---
沉默王二's avatar
沉默王二 已提交
14

沉默王二's avatar
沉默王二 已提交
15
# 3.7 Java流程控制语句
沉默王二's avatar
doc  
沉默王二 已提交
16 17 18 19 20

“二哥,流程控制语句都有哪些呢?”三妹的脸上泛着甜甜的笑容,她开始对接下来要学习的内容充满期待了,这正是我感到欣慰的地方。

“比如说 if-else、switch、for、while、do-while、return、break、continue 等等,接下来,我们一个个来了解下。”

沉默王二's avatar
沉默王二 已提交
21
### 01、if-else 相关
沉默王二's avatar
doc  
沉默王二 已提交
22

沉默王二's avatar
沉默王二 已提交
23
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/control/thirteen-01.png)
沉默王二's avatar
doc  
沉默王二 已提交
24

沉默王二's avatar
沉默王二 已提交
25
#### **1)if 语句**
沉默王二's avatar
doc  
沉默王二 已提交
26 27 28 29 30 31 32 33 34 35 36

if 语句的格式如下:

```java
if(布尔表达式){  
// 如果条件为 true,则执行这块代码
} 
```

画个流程图表示一下:

沉默王二's avatar
沉默王二 已提交
37
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/control/thirteen-02.png)
沉默王二's avatar
doc  
沉默王二 已提交
38 39 40 41 42


来写个示例:

```java
沉默王二's avatar
沉默王二 已提交
43
int age = 20;
沉默王二's avatar
沉默王二 已提交
44 45 46
if (age < 30) {
    System.out.println("青春年华");
}
沉默王二's avatar
doc  
沉默王二 已提交
47 48 49 50 51 52 53 54
```

输出:

```
青春年华
```

沉默王二's avatar
沉默王二 已提交
55
#### **2)if-else 语句**
沉默王二's avatar
doc  
沉默王二 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68

if-else 语句的格式如下:

```java
if(布尔表达式){  
// 条件为 true 时执行的代码块
}else{  
// 条件为 false  时执行的代码块
}  
```

画个流程图表示一下:

沉默王二's avatar
沉默王二 已提交
69
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/control/thirteen-03.png)
沉默王二's avatar
doc  
沉默王二 已提交
70 71 72 73

来写个示例:

```java
沉默王二's avatar
沉默王二 已提交
74 75 76 77 78
int age = 31;
if (age < 30) {
    System.out.println("青春年华");
} else {
    System.out.println("而立之年");
沉默王二's avatar
doc  
沉默王二 已提交
79 80 81 82 83 84 85 86 87 88 89 90
}
```

输出:

```
而立之年
```

除了这个例子之外,还有一个判断闰年(被 4 整除但不能被 100 整除或者被 400 整除)的例子:

```java
沉默王二's avatar
沉默王二 已提交
91 92 93 94 95
int year = 2020;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
    System.out.println("闰年");
} else {
    System.out.println("普通年份");
沉默王二's avatar
doc  
沉默王二 已提交
96 97 98 99 100 101 102 103 104 105 106 107
}
```

输出:

```
闰年
```

如果执行语句比较简单的话,可以使用三元运算符来代替 if-else 语句,如果条件为 true,返回 ? 后面 : 前面的值;如果条件为 false,返回 : 后面的值。

```java
沉默王二's avatar
沉默王二 已提交
108 109 110
int num = 13;
String result = (num % 2 == 0) ? "偶数" : "奇数";
System.out.println(result);
沉默王二's avatar
doc  
沉默王二 已提交
111 112 113 114 115 116 117 118
```

输出:

```
奇数
```

沉默王二's avatar
沉默王二 已提交
119
#### **3)if-else-if 语句**
沉默王二's avatar
doc  
沉默王二 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

if-else-if 语句的格式如下:

```java
if(条件1){  
// 条件1 为 true 时执行的代码
}else if(条件2){  
// 条件2 为 true 时执行的代码
}  
else if(条件3){  
// 条件3 为 true 时执行的代码
}  
...  
else{  
// 以上条件均为 false 时执行的代码
} 
```

画个流程图表示一下:

沉默王二's avatar
沉默王二 已提交
140
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/control/thirteen-04.png)
沉默王二's avatar
doc  
沉默王二 已提交
141 142 143 144

来写个示例:

```java
沉默王二's avatar
沉默王二 已提交
145 146 147 148 149 150 151 152 153
int age = 31;
if (age < 30) {
    System.out.println("青春年华");
} else if (age >= 30 && age < 40 ) {
    System.out.println("而立之年");
} else if (age >= 40 && age < 50 ) {
    System.out.println("不惑之年");
} else {
    System.out.println("知天命");
沉默王二's avatar
doc  
沉默王二 已提交
154 155 156 157 158 159 160 161 162
}
```

输出:

```
而立之年
```

沉默王二's avatar
沉默王二 已提交
163
#### **4)if 嵌套语句**
沉默王二's avatar
doc  
沉默王二 已提交
164 165 166 167 168 169

if 嵌套语句的格式如下:

```java
if(外侧条件){    
     // 外侧条件为 true 时执行的代码 
沉默王二's avatar
沉默王二 已提交
170 171
    if(内侧条件){  
        // 内侧条件为 true 时执行的代码
沉默王二's avatar
doc  
沉默王二 已提交
172 173 174 175 176 177
    }    
}  
```

画个流程图表示一下:

沉默王二's avatar
沉默王二 已提交
178
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/control/thirteen-05.png)
沉默王二's avatar
doc  
沉默王二 已提交
179 180 181 182 183


来写个示例:

```java
沉默王二's avatar
沉默王二 已提交
184 185 186 187 188
int age = 20;
boolean isGirl = true;
if (age >= 20) {
    if (isGirl) {
        System.out.println("女生法定结婚年龄");
沉默王二's avatar
doc  
沉默王二 已提交
189 190 191 192 193 194 195 196 197 198
    }
}
```

输出:

```
女生法定结婚年龄
```

沉默王二's avatar
沉默王二 已提交
199
### 02、switch 语句
沉默王二's avatar
doc  
沉默王二 已提交
200

沉默王二's avatar
沉默王二 已提交
201
switch 语句用来判断变量与多个值之间的相等性。变量的类型可以是 byte、short、int 或者 char,或者对应的包装器类型 Byte、Short、Integer、Character,以及[字符串](https://javabetter.cn/string/immutable.html)[枚举](https://javabetter.cn/basic-extra-meal/enum.html)类型。
沉默王二's avatar
doc  
沉默王二 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

来看一下 switch 语句的格式:

```java
switch(变量) {    
case 可选值1:    
 // 可选值1匹配后执行的代码;    
 break;  // 该关键字是可选项
case 可选值2:    
 // 可选值2匹配后执行的代码;    
 break;  // 该关键字是可选项
......    
    
default: // 该关键字是可选项     
 // 所有可选值都不匹配后执行的代码 
}    
```

- 变量可以有 1 个或者 N 个值。
- 值类型必须和变量类型是一致的,并且值是确定的。
- 值必须是唯一的,不能重复,否则编译会出错。
- break 关键字是可选的,如果没有,则执行下一个 case,如果有,则跳出 switch 语句。
- default 关键字也是可选的。

画个流程图:

沉默王二's avatar
沉默王二 已提交
228
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/control/thirteen-06.png)
沉默王二's avatar
doc  
沉默王二 已提交
229 230 231 232

来个示例:

```java
沉默王二's avatar
沉默王二 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246
int age = 20;
switch (age) {
    case 20 :
        System.out.println("上学");
        break;
    case 24 :
        System.out.println("苏州工作");
        break;
    case 30 :
        System.out.println("洛阳工作");
        break;
    default:
        System.out.println("未知");
        break; // 可省略
沉默王二's avatar
doc  
沉默王二 已提交
247 248 249 250 251 252 253 254 255 256 257 258
}
```

输出:

```
上学
```

当两个值要执行的代码相同时,可以把要执行的代码写在下一个 case 语句中,而上一个 case 语句中什么也没有,来看一下示例:

```java
沉默王二's avatar
沉默王二 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
String name = "沉默王二";
switch (name) {
    case "詹姆斯":
        System.out.println("篮球运动员");
        break;
    case "穆里尼奥":
        System.out.println("足球教练");
        break;
    case "沉默王二":
    case "沉默王三":
        System.out.println("乒乓球爱好者");
        break;
    default:
        throw new IllegalArgumentException(
                "名字没有匹配项");
沉默王二's avatar
doc  
沉默王二 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

}
```

输出:

```
乒乓球爱好者
```

枚举作为 switch 语句的变量也很常见,来看例子:

```java
public class SwitchEnumDemo {
    public enum PlayerTypes {
        TENNIS,
        FOOTBALL,
        BASKETBALL,
        UNKNOWN
    }

    public static void main(String[] args) {
        System.out.println(createPlayer(PlayerTypes.BASKETBALL));
    }

    private static String createPlayer(PlayerTypes playerType) {
        switch (playerType) {
            case TENNIS:
                return "网球运动员费德勒";
            case FOOTBALL:
                return "足球运动员C罗";
            case BASKETBALL:
                return "篮球运动员詹姆斯";
            case UNKNOWN:
                throw new IllegalArgumentException("未知");
            default:
                throw new IllegalArgumentException(
                        "运动员类型: " + playerType);

        }
    }
}
```

输出:

```
篮球运动员詹姆斯
```

沉默王二's avatar
沉默王二 已提交
324
### 03、for 循环
沉默王二's avatar
doc  
沉默王二 已提交
325

沉默王二's avatar
沉默王二 已提交
326
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/control/thirteen-07.png)
沉默王二's avatar
doc  
沉默王二 已提交
327

沉默王二's avatar
沉默王二 已提交
328
#### **1)普通 for 循环**
沉默王二's avatar
doc  
沉默王二 已提交
329 330 331 332 333 334 335 336 337

普通的 for 循环可以分为 4 个部分:

1)初始变量:循环开始执行时的初始条件。

2)条件:循环每次执行时要判断的条件,如果为 true,就执行循环体;如果为 false,就跳出循环。当然了,条件是可选的,如果没有条件,则会一直循环。

3)循环体:循环每次要执行的代码块,直到条件变为 false。

moyufxst's avatar
moyufxst 已提交
338
4)自增/自减:初始变量变化的方式。
沉默王二's avatar
doc  
沉默王二 已提交
339 340 341 342

来看一下普通 for 循环的格式:

```java
moyufxst's avatar
moyufxst 已提交
343
for(初始变量;条件;自增/自减){  
沉默王二's avatar
doc  
沉默王二 已提交
344 345 346 347 348 349
// 循环体
}  
```

画个流程图:

沉默王二's avatar
沉默王二 已提交
350
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/control/thirteen-08.png)
沉默王二's avatar
doc  
沉默王二 已提交
351 352 353 354 355


来个示例:

```java
沉默王二's avatar
沉默王二 已提交
356 357
for (int i = 0; i < 5; i++) {
    System.out.println("沉默王三好美啊");
沉默王二's avatar
doc  
沉默王二 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
}
```

输出:

```
沉默王三好美啊
沉默王三好美啊
沉默王三好美啊
沉默王三好美啊
沉默王三好美啊
```

“哎呀,二哥,你真的是变着法夸我啊。”

“非也非也,三妹,你看不出我其实在夸我自己吗?循环语句还可以嵌套呢,这样就可以打印出更好玩的呢,你要不要看看?”

“好呀好呀!”

“看好了啊。”

```java
沉默王二's avatar
沉默王二 已提交
380 381 382
for (int i = 0; i < 5; i++) {
    for (int j = 0;j<= i;j++) {
        System.out.print("❤");
沉默王二's avatar
doc  
沉默王二 已提交
383
    }
沉默王二's avatar
沉默王二 已提交
384
    System.out.println();
沉默王二's avatar
doc  
沉默王二 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
}
```

打印出什么玩意呢?

```

❤❤
❤❤❤
❤❤❤❤
❤❤❤❤❤
```

“哇,太不可思议了,二哥。”

“嘿嘿。”

沉默王二's avatar
沉默王二 已提交
402
#### **2)for-each**
沉默王二's avatar
doc  
沉默王二 已提交
403 404 405 406 407 408 409 410 411 412 413 414

for-each 循环通常用于遍历数组和集合,它的使用规则比普通的 for 循环还要简单,不需要初始变量,不需要条件,不需要下标来自增或者自减。来看一下语法:

```java
for(元素类型 元素 : 数组或集合){  
// 要执行的代码
}  
```

来看一下示例:

```java
沉默王二's avatar
沉默王二 已提交
415
String[] strs = {"沉默王二", "一枚有趣的程序员"};
沉默王二's avatar
doc  
沉默王二 已提交
416

沉默王二's avatar
沉默王二 已提交
417 418
for (String str : strs) {
    System.out.println(str);
沉默王二's avatar
doc  
沉默王二 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432
}
```

输出:

```
沉默王二
一枚有趣的程序员
```

“呀,二哥,你开始王哥卖瓜了啊。”

“嘿嘿,三妹,你这样说哥会脸红的。”

沉默王二's avatar
沉默王二 已提交
433
#### **3)无限 for 循环**
沉默王二's avatar
doc  
沉默王二 已提交
434 435 436 437 438 439 440 441

“三妹,你想不想体验一下无限 for 循环的威力,也就是死循环。”

“二哥,那会有什么样的后果啊?”

“来,看看就知道了。”

```java
沉默王二's avatar
沉默王二 已提交
442 443
for(;;){
    System.out.println("停不下来。。。。");
沉默王二's avatar
doc  
沉默王二 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457
}
```

输出:

```
停不下来。。。。
停不下来。。。。
停不下来。。。。
停不下来。。。。
```

一旦运行起来,就停不下来了,除非强制停止。

沉默王二's avatar
沉默王二 已提交
458
### 04、while 循环
沉默王二's avatar
doc  
沉默王二 已提交
459 460 461 462 463 464 465 466 467 468 469

来看一下 while 循环的格式:

```java
while(条件){  
//循环体  
}  
```

画个流程图:

沉默王二's avatar
沉默王二 已提交
470
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/control/thirteen-09.png)
沉默王二's avatar
doc  
沉默王二 已提交
471 472 473 474

来个示例:

```java
沉默王二's avatar
沉默王二 已提交
475 476 477 478 479 480
int i = 0;
while (true) {
    System.out.println("沉默王三");
    i++;
    if (i == 5) {
        break;
沉默王二's avatar
doc  
沉默王二 已提交
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    }
}
```

“三妹,你猜猜会输出几次?”

“五次吗?”

“对了,你可真聪明。”

```
沉默王三
沉默王三
沉默王三
沉默王三
沉默王三
```

“三妹,你想不想体验一下无限 while 循环的威力,也就是死循环。”

“二哥,那会有什么样的后果啊?”

“来,看看就知道了。”

```java
沉默王二's avatar
沉默王二 已提交
506 507
while (true) {
    System.out.println("停不下来。。。。");
沉默王二's avatar
doc  
沉默王二 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521
}
```

输出:

```
停不下来。。。。
停不下来。。。。
停不下来。。。。
停不下来。。。。
```

把 while 的条件设置为 true,并且循环体中没有 break 关键字的话,程序一旦运行起来,就根本停不下来了,除非强制停止。

沉默王二's avatar
沉默王二 已提交
522
### 05、do-while 循环
沉默王二's avatar
doc  
沉默王二 已提交
523 524 525 526 527 528 529 530 531 532 533

来看一下 do-while 循环的格式:

```java
do{  
// 循环体
}while(提交);  
```

画个流程图:

沉默王二's avatar
沉默王二 已提交
534
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/control/thirteen-10.png)
沉默王二's avatar
doc  
沉默王二 已提交
535 536 537 538

来个示例:

```java
沉默王二's avatar
沉默王二 已提交
539 540 541 542 543 544
int i = 0;
do {
    System.out.println("沉默王三");
    i++;
    if (i == 5) {
        break;
沉默王二's avatar
doc  
沉默王二 已提交
545
    }
沉默王二's avatar
沉默王二 已提交
546
} while (true);
沉默王二's avatar
doc  
沉默王二 已提交
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
```

“三妹,你猜猜会输出几次?”

“五次吗?”

“对了,你可真聪明。”

```
沉默王三
沉默王三
沉默王三
沉默王三
沉默王三
```

“三妹,你想不想体验一下无限 do-while 循环的威力......”

“二哥,又来啊,我都腻了。”

“来吧,例行公事,就假装看看嘛。”

```java
沉默王二's avatar
沉默王二 已提交
570 571 572
do {
    System.out.println("停不下来。。。。");
} while (true);
沉默王二's avatar
doc  
沉默王二 已提交
573 574 575 576 577 578 579 580 581 582 583 584 585
```

输出:

```
停不下来。。。。
停不下来。。。。
停不下来。。。。
停不下来。。。。
```

把 do-while 的条件设置为 true,并且循环体中没有 break 关键字的话,程序一旦运行起来,就根本停不下来了,除非强制停止。

沉默王二's avatar
沉默王二 已提交
586
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/control/thirteen-11.png)
沉默王二's avatar
doc  
沉默王二 已提交
587

沉默王二's avatar
沉默王二 已提交
588
### 06、break
沉默王二's avatar
doc  
沉默王二 已提交
589 590 591 592 593 594 595

break 关键字通常用于中断循环或 switch 语句,它在指定条件下中断程序的当前流程。如果是内部循环,则仅中断内部循环。

可以将 break 关键字用于所有类型循环语句中,比如说 for 循环、while 循环,以及 do-while 循环。

来画个流程图感受一下:

沉默王二's avatar
沉默王二 已提交
596
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/control/thirteen-12.png)
沉默王二's avatar
doc  
沉默王二 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653

用在 for 循环中的示例:

```java
for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        break;
    }
    System.out.println(i);
}
```

用在嵌套 for 循环中的示例:

```java
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (i == 2 && j == 2) {
            break;
        }
        System.out.println(i + " " + j);
    }
}
```

用在 while 循环中的示例:

```java
int i = 1;
while (i <= 10) {
    if (i == 5) {
        i++;
        break;
    }
    System.out.println(i);
    i++;
}
```

用在 do-while 循环中的示例:

```java
int j = 1;
do {
    if (j == 5) { 
        j++;
        break;
    }
    System.out.println(j);
    j++;
} while (j <= 10);
```

用在 switch 语句中的示例:

```java
switch (age) {
沉默王二's avatar
沉默王二 已提交
654 655 656 657 658 659 660 661 662 663 664 665
    case 20 :
        System.out.println("上学");
        break;
    case 24 :
        System.out.println("苏州工作");
        break;
    case 30 :
        System.out.println("洛阳工作");
        break;
    default:
        System.out.println("未知");
        break; // 可省略
沉默王二's avatar
doc  
沉默王二 已提交
666 667 668
}
```

沉默王二's avatar
沉默王二 已提交
669
### 07、continue
沉默王二's avatar
doc  
沉默王二 已提交
670 671 672 673 674 675

当我们需要在 for 循环或者 (do)while 循环中立即跳转到下一个循环时,就可以使用 continue 关键字,通常用于跳过指定条件下的循环体,如果循环是嵌套的,仅跳过当前循环。

来个示例:

```java
沉默王二's avatar
沉默王二 已提交
676 677 678 679
for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        // 使用 continue 关键字
        continue;// 5 将会被跳过
沉默王二's avatar
doc  
沉默王二 已提交
680
    }
沉默王二's avatar
沉默王二 已提交
681
    System.out.println(i);
沉默王二's avatar
doc  
沉默王二 已提交
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
}
```

输出:

```
1
2
3
4
6
7
8
9
10
```

“二哥,5 真的被跳过了呀。”

“那必须滴。不然就是 bug。”

再来个循环嵌套的例子。

```java
沉默王二's avatar
沉默王二 已提交
706 707 708 709 710
for (int i = 1; i <= 3; i++) {
    for (int j = 1; j <= 3; j++) {
        if (i == 2 && j == 2) {
            //  当i=2,j=2时跳过
            continue;
沉默王二's avatar
doc  
沉默王二 已提交
711
        }
沉默王二's avatar
沉默王二 已提交
712
        System.out.println(i + " " + j);
沉默王二's avatar
doc  
沉默王二 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    }
}
```

打印出什么玩意呢?

```
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
```

“2 2” 没有输出,被跳过了。

再来看一下 while 循环时 continue 的使用示例:

```java
沉默王二's avatar
沉默王二 已提交
735 736 737 738 739
int i = 1;
while (i <= 10) {
    if (i == 5) {
        i++;
        continue;
沉默王二's avatar
doc  
沉默王二 已提交
740
    }
沉默王二's avatar
沉默王二 已提交
741 742
    System.out.println(i);
    i++;
沉默王二's avatar
doc  
沉默王二 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
}
```

输出:

```
1
2
3
4
6
7
8
9
10
```

注意:如果把 if 条件中的“i++”省略掉的话,程序就会进入死循环,一直在 continue。

最后,再来看一下 do-while 循环时 continue 的使用示例:

```java
沉默王二's avatar
沉默王二 已提交
765 766 767 768 769
int i=1;
do{
    if(i==5){
        i++;
        continue;
沉默王二's avatar
doc  
沉默王二 已提交
770
    }
沉默王二's avatar
沉默王二 已提交
771 772 773
    System.out.println(i);
    i++;
}while(i<=10);
沉默王二's avatar
doc  
沉默王二 已提交
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
```

输出:

```
1
2
3
4
6
7
8
9
10
```

沉默王二's avatar
更新  
沉默王二 已提交
790 791
注意:同样的,如果把 if 条件中的“i++”省略掉的话,程序就会进入死循环,一直在 continue。

沉默王二's avatar
沉默王二 已提交
792 793 794 795
### 08、小结

本文全面讲解了Java流程控制语句,包括if、switch、while、for等结构。通过学习本文,你将了解到Java流程控制语句的基本概念、语法结构和使用场景,帮助你在实际编程过程中更加灵活地运用各类控制结构。

沉默王二's avatar
沉默王二 已提交
796 797
---

沉默王二's avatar
9000+  
沉默王二 已提交
798
GitHub 上标星 9000+ 的开源知识库《[二哥的 Java 进阶之路](https://github.com/itwanger/toBeBetterJavaer)》第一版 PDF 终于来了!包括Java基础语法、数组&字符串、OOP、集合框架、Java IO、异常处理、Java 新特性、网络编程、NIO、并发编程、JVM等等,共计 32 万余字,可以说是通俗易懂、风趣幽默……详情戳:[太赞了,GitHub 上标星 9000+ 的 Java 教程](https://javabetter.cn/overview/)
沉默王二's avatar
沉默王二 已提交
799 800

微信搜 **沉默王二** 或扫描下方二维码关注二哥的原创公众号沉默王二,回复 **222** 即可免费领取。
沉默王二's avatar
沉默王二 已提交
801

沉默王二's avatar
沉默王二 已提交
802
![](https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/gongzhonghao.png)