Java Control Statements
Java Control Statements

Java For Loop
public class ForExample1 {
public static void main(String[] args) {
// Java for loop
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}
运行结果
1
2
3
4
5
6
7
8
9
10
```
public class ForExample2 {
public static void main(String[] args) {
// 无限 for 循环
for (; ; ) {
System.out.println("infinitive loop");
}
}
}
运行结果
死循环...
```
public class ForEachExample1 {
public static void main(String[] args) {
int arr[] = {12, 23, 44, 56, 78};
// for-each loop
for (int i : arr) {
System.out.println(i);
}
}
}
运行结果
12
23
44
56
78
```
import java.util.Arrays;
import java.util.List;
public class ForEachExample2 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(12, 23, 44, 56, 78);
list.stream().forEach((i) -> System.out.println(i));
}
}
运行结果
12
23
44
56
78
```
public class LabeledForExample1 {
public static void main(String[] args) {
aa:
for (int i = 1; i <= 3; i++) {
bb:
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
break aa;
// break bb;
}
System.out.println(i + " " + j);
}
}
}
}
运行结果
1 1
1 2
1 3
2 1
```
public class LabeledForExample2 {
public static void main(String[] args) {
aa:
for (int i = 1; i <= 3; i++) {
bb:
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
// break aa;
break bb;
}
System.out.println(i + " " + j);
}
}
}
}
运行结果
1 1
1 2
1 3
2 1
3 1
3 2
3 3
```
Java While Loop
public class WhileExample1 {
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
System.out.println(i);
i++;
}
}
}
运行结果
1
2
3
4
5
6
7
8
9
10
```
public class WhileExample2 {
public static void main(String[] args) {
while (true) {
System.out.println("infinitive while loop");
}
}
}
运行结果
死循环...
```
Java Do While Loop
public class DoWhileExample1 {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
}
运行结果
1
2
3
4
5
6
7
8
9
10
```
public class DoWhileExample2 {
public static void main(String[] args) {
do {
System.out.println("infinitive do while loop");
} while (true);
}
}
运行结果
死循环...
```
Java Break
public class BreakExample1 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
}
}
运行结果
1
2
3
4
```
public class BreakExample2 {
public static void main(String[] args) {
// outer loop
for (int i = 1; i <= 3; i++) {
// inner loop
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
// using break statement inside the inner loop
break;
}
System.out.println(i + " " + j);
}
}
}
}
运行结果
1 1
1 2
1 3
2 1
3 1
3 2
3 3
```
public class BreakExample3 {
public static void main(String[] args) {
aa:
for (int i = 1; i <= 3; i++) {
bb:
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
break aa;
}
System.out.println(i + " " + j);
}
}
}
}
运行结果
1 1
1 2
1 3
2 1
```
public class BreakWhileExample {
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
if (i == 5) {
i++;
break;
}
System.out.println(i);
i++;
}
}
}
运行结果
1
2
3
4
```
public class BreakDoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
if (i == 5) {
i++;
break;
}
System.out.println(i);
i++;
} while (i <= 10);
}
}
运行结果
1
2
3
4
```
Java Continue
public class ContinueExample1 {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;
}
System.out.println(i);
}
}
}
运行结果
1
2
3
4
6
7
8
9
10
```
public class ContinueExample2 {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
continue;
}
System.out.println(i + " " + j);
}
}
}
}
运行结果
1 1
1 2
1 3
2 1
2 3
3 1
3 2
3 3
```
public class ContinueExample3 {
public static void main(String[] args) {
aa:
for (int i = 1; i <= 3; i++) {
bb:
for (int j = 1; j <= 3; j++) {
if (i == 2 && j == 2) {
continue aa;
}
System.out.println(i + " " + j);
}
}
}
}
运行结果
1 1
1 2
1 3
2 1
3 1
3 2
3 3
```
public class ContinueWhileExample {
public static void main(String[] args) {
int i = 1;
while (i <= 10) {
if (i == 5) {
i++;
continue;
}
System.out.println(i);
i++;
}
}
}
运行结果
1
2
3
4
6
7
8
9
10
```
public class ContinueDoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
if (i == 5) {
i++;
continue;
}
System.out.println(i);
i++;
} while (i <= 10);
}
}
运行结果
1
2
3
4
6
7
8
9
10
```
break 和 continue 的作用
- 1、当循环执行到 break 语句时,就退出整个循环,然后执行循环外的语句。
- 2、当循环语句执行到 continue 时,当次循环结束,重新开始下一轮循环。如果已经是最后一轮循环了,那么这是的 continue 就与 break 效果一样了。
参考资料
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
- https://www.javatpoint.com/java-tutorial
Java Control Statements的更多相关文章
- Go xmas2020 学习笔记 06、Control Statements、Declarations & Types
06-Control Statements. If-then-else. Loop. for. range array. range map. infinite loop. common mistak ...
- Core Java Volume I — 3.8. Control Flow
3.8. Control FlowJava, like any programming language, supports both conditional statements and loops ...
- Scala For Java的一些参考
变量 String yourPast = "Good Java Programmer"; val yourPast : String = "Good Java ...
- 使Eclipse符合Java编程规范
编程规范是很重要的东西,能让团队的代码易于阅读和维护,也便于日后的功能扩展. 工欲善其事必先利其器!作为一个Java程序员,与Eclipse打交道可能是一辈子的事情.将Eclipse设置为符合公司编程 ...
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- 标准的Java编码规范手册
编码规范体现出一个开发者的基本素质,良好的编码规范可以提高团队编码的效率,避免很多不必要的问题.今天分享一个标准的Java编码规范给大家,希望对于大家今后的开发工作带来帮助. 编码规范的意义 ...
- java web 开发三剑客 -------电子书
Internet,人们通常称为因特网,是当今世界上覆盖面最大和应用最广泛的网络.根据英语构词法,Internet是Inter + net,Inter-作为前缀在英语中表示“在一起,交互”,由此可知In ...
- Troubleshooting tips for using Java on Windows 8
This article applies to: Platform(s): Windows 8 Will Java run in Start screen on Windows 8? Microsof ...
- Mac 上 java 究竟在哪里,本文彻底让你搞清楚!
Mac下当你在[终端]输入java -version时,是执行的哪里的java呢,which java命令可以看到,就是[/usr/bin/java] [/usr/bin/java]只是个替身,实际指 ...
随机推荐
- Dom中select练习
选择框checkbox练习 select练习 注意select的selected属性 <!DOCTYPE html> <html xmlns="http://www.w3. ...
- HDU1711 KMP(模板题)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 面包旅行Android业务设计分析
面包旅行的业务设计不错,Android app也是清晰简洁又大方的样子,所以画了个业务脑图出来. 重要的几个业务特点分析如下: 1.账号绑定社交账号,方便社交推广 2.城市猎人活动,通过内容.时间.地 ...
- 在IIS中寄存服务
http://blog.csdn.net/songyefei/article/details/7381595 第三篇 在IIS中寄宿服务 通过前两篇的学习,我们了解了如何搭建一个最简单的WCF通信模型 ...
- 快速排序Quick sort
快速排序Quick sort 原理,通过一趟扫描将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归 ...
- redis 模糊查找keys
Redis入门教程可参考:超强.超详细Redis数据库入门教程 Redis操作命令可参考:Redis操作命令总结 redis可以通过命令Keys Match来进行键值的模糊匹配,借助StackExch ...
- 【POJ】1222 EXTENDED LIGHTS OUT
[算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...
- poj 3104 Drying(二分查找)
题目链接:http://poj.org/problem?id=3104 Drying Time Limit: 2000MS Memory Limit: 65536K Total Submissio ...
- 数组返回NULL绕过
BUGKU:http://120.24.86.145:9009/19.php 还没看完源码,我就直接加了一个password[]=1结果就拿到flag了.然后再看源码我自己都搞不懂为什么可以得到源码. ...
- 【转】CVE-2010-4258 漏洞分析
一. 漏洞简介 CVE-2010-4258这个漏洞很有意思,主要思路是如果通过clone函数去创建进程,并且带有CLONE_CHILD_CLEARTID标志,那么进程在退出的时候,可以造成内核任意地址 ...