本节课程将学习以下知识点:
- 循环结构
- for循环语句
- while循环语句
循环结构
一张图看懂什么是循环结构:
for循环语句
源码:(请动手)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public class Demo01 {
public static void main(String[] args) {
System.out.println("准备进入for循环语句。");
// 1. 执行 int i = 0;
// 2. 判断i的值是否小于10(即执行i < 10;);
// 3. 如果i < 10 ,则执行{}中的语句;
// 4. {}中的语句执行完毕后,执行 i++ ;
// 5. 再次判断 i < 10;
// 6. 如果i依旧小于10,重复3~5的过程,这个过程就是循环;
// 7. 如果i >= 10 ,那么循环停止。
for(int i = 0; i < 10; i++){
System.out.println(i);
}
System.out.println("for循环语句结束。");
}
}
while循环语句
源码:(请动手)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18public class Demo02 {
public static void main(String[] args) {
System.out.println("准备进入while循环语句。");
// 1. 执行 int i = 0;
// 2. 判断i的值是否小于10(即执行i < 10;);
// 3. 如果i < 10 ,即i < 10的值为true,则执行{}中的语句;
// 4. 如果 i < 10的值为false,则while循环结束。
int i = 0;
while(i < 10){
System.out.println(i);
i++; // 如果没有这一行,则成为while死循环。
}
System.out.println("while循环语句结束。");
}
}


