3.5 循环语句
循环语句可以在满足循环条件的情况下,反复执行某一段代码,这段被重复执行的代码被称为循环体语句,当反复执行这个循环体时,需要通过修改循环变量使得循环判断条件为false,从而结束循环,否则循环将一直执行下去,形成死循环。
3.5.1 for循环
for循环语句格式:
for(初始化语句①; 循环条件语句②; 迭代语句④){
循环体语句③
}
注意:
- for(;;)中的两个;是不能多也不能少
- 循环条件必须是boolean类型
执行流程:
-
第一步:执行初始化语句①,完成循环变量的初始化;
-
第二步:执行循环条件语句②,看循环条件语句的值是true,还是false;
-
如果是true,执行第三步;
-
如果是false,循环语句中止,循环不再执行。
-
-
第三步:执行循环体语句③
-
第四步:执行迭代语句④,针对循环变量重新赋值
-
第五步:根据循环变量的新值,重新从第二步开始再执行一遍
int i = 1;//表达式 1 初始化量 执行一次
i<=10;//表达式 2 条件表达式判断是否为真,如果为真,执行循环体语句(当循环体语句执行完毕时在执行表达式 3)
i++;//表达式 3 执行++ -- 继续执行表达式 2 然后进行判断,再次判断是否执行循环体语句,
直到表达式 2 判断条件为假,就结束整个循环
for (int i = 1;i<=10;i++){
循环体语句;
}
1、使用for循环重复执行某些语句
int i;
for (i = 1;i<=5;i++){
System.out.println(i);
}
思考:
(1)使用循环和不使用循环的区别
(2)如果要实现输出从5到1呢
(3)如果要实现输出从1-100呢,或者1-100之间3的倍数或以3结尾的数字呢
2、变量作用域
int i;
for (i = 1;i<=5;i++){
System.out.println(i);
}
System.out.println(i);
3、死循环
public class ForCase2 {
public static void main(String[] args) {
//死循环语句
// for (;;){
// System.out.println("我爱你");
// }
//没办法到达
// System.out.println("真的吗?");
// for (;true;){
// System.out.println("我爱你");
// }
//绝不执行
// for (;false;){
// System.out.println("我爱你");
// }
}
}
注意:
(1)如果两个;之间写true的话,就表示循环条件成立
(2)如果两个;之间的循环条件省略的话,就默认为循环条件成立
(3)如果循环变量的值不修改,那么循环条件就会永远成立
3.5.2 关键字
1、break;
使用场景:终止switch或者当前循环
-
在选择结构switch语句中
-
在循环语句中
-
离开使用场景的存在是没有意义的
2、continue;
使用场景:提前结束本次循环,继续下一次的循环
跳过本次循环
public class ForCase3 {
public static void main(String[] args) {
// 逢七过
// 7的倍数或个位为十七
for (int i = 1; i <= 100; i++) {
if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7) {
System.out.println("过");
//结束循环 break continue
continue;//结束当前循环,接着进行下次循环
// break;//结束整个循环
}
System.out.println(i);
}
}
}
3.5.3 while循环
while循环语句基本格式:
while (循环条件语句①) {
循环体语句②;
}
注意:
while(循环条件)中循环条件必须是boolean类型
执行流程:
-
第一步:执行循环条件语句①,看循环条件语句的值是true,还是false;
-
如果是true,执行第二步;
-
如果是false,循环语句中止,循环不再执行。
-
-
第二步:执行循环体语句②;
-
第三步:循环体语句执行完后,重新从第一步开始再执行一遍
1、使用while循环重复执行某些语句
public class WhileCase1 {
public static void main(String[] args) {
// ① ② ③
//for (int i = 1; i <=10 ; i++) {}
// while(条件表达式){
// 循环体语句
// }
int i = 1;
while (i <= 10) {
System.out.println(i);
i++;
}
}
}
2、死循环
while(true){
循环体语句;//如果此时循环体中没有跳出循环的语句,就是死循环
}
注意:
(1)while(true):常量true表示循环条件永远成立
(2)while(循环条件),如果循环条件中的循环变量值不修改,那么循环条件就会永远成立
(3)while()中的循环条件不能空着
package com.huohua.day0407;
import java.util.Scanner;
public class WhileCase3 {
public static void main(String[] args) {
// 输入性别来进行判断输入的性别是不是期待的结果,输入正确的就停止,如果不正确继续输入
// "男" "女" "保密"
//while
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("请输入你的性别");
String str = scan.next();
if(!str.equals("男") && !str.equals("女") && !str.equals("保密")){
System.out.println("输入有误,请重新输入");
}
if (str.equals("男") || str.equals("女") || str.equals("保密")) {
System.out.println("您的性别是" + str);
break;
}
}
}
}
3.5.4 do...while循环
do...while循环语句标准格式:
do{
循环体语句①;
}while(循环条件语句②);
注意:
(1)while(循环条件)中循环条件必须是boolean类型
(2)do{}while();最后有一个分号
(3)do...while结构的循环体语句是至少会执行一次,这个和for和while是不一样的
执行流程:
-
第一步:执行循环体语句①;
-
第二步:执行循环条件语句②,看循环条件语句的值是true,还是false;
-
如果是true,执行第三步;
-
如果是false,循环语句终止,循环不再执行。
-
-
第三步:循环条件语句执行完后,重新从第一步开始再执行一遍
1、do...while循环至少执行一次循环体
package com.huohua.day0407;
import java.util.Scanner;
public class WhileCase5 {
public static void main(String[] args) {
int i = 10;
while (i<=5){
System.out.println(i);
i++;
}
System.out.println("=====================");
int j = 10;
do {
System.out.println(j);
j++;
}while (j<=5);
}
}
2、死循环
do{
循环体语句;//如果此时循环体中没有跳出循环的语句,就是死循环
}while(true);
注意:
(1)while(true):常量true表示循环条件永远成立
(2)while(循环条件),如果循环条件中的循环变量值不修改,那么循环条件就会永远成立
(3)while()中的循环条件不能空着
3.5.5 循环语句的区别
-
从循环次数角度分析
-
do...while循环至少执行一次循环体语句
-
for和while循环先循环条件语句是否成立,然后决定是否执行循环体,至少执行零次循环体语句
-
-
如何选择
-
遍历有明显的循环次数(范围)的需求,选择for循环
-
遍历没有明显的循环次数(范围)的需求,循环while循环
-
如果循环体语句块至少执行一次,可以考虑使用do...while循环
-
本质上:三种循环之间完全可以互相转换,都能实现循环的功能
-
-
三种循环结构都具有四要素:
-
循环变量的初始化表达式
-
循环条件
-
循环变量的修改的迭代表达式
-
循环体语句块
-
3.5.6 循环嵌套
案例一:
直角三角形
package com.huohua.day0407;
import java.util.Scanner;
public class ForFor1 {
public static void main(String[] args) {
//输出直角三角形
// * i=1 j=1
// ** i=2 j=2
// *** i=3 j=3
// **** i=4 j=4
// ***** i=5 j=5
// for (int i = 1; i <= 5; i++) {
// for (int j = 1; j <= i; j++) {
// System.out.print("*");
// }
// System.out.println("");
// }
// for (int i = 1; i <= 5; i++) {
// for (int j = 1; j <= 5; j++) {
// System.out.print("*");
// }
// System.out.println("");
// }
Scanner scan = new Scanner(System.in);
System.out.println("请输入行数");
int level = scan.nextInt();
for (int i = 1; i <= level; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println("");
}
}
}
案例二:
九九乘法表
package com.huohua.day0407;
//九九乘法表
public class ForFor2 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (i * j) + "\t");
}
System.out.println("");
}
}
}
案例三 :
1
22
333
4444
55555
package com.huohua.day0407;
import java.util.Scanner;
public class ForFor3 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入层数");
int level =scan.nextInt();
for (int i = 1; i <=level; i++) {
for (int j = 1; j <=level ; j++) {
System.out.print(i);
if (j==i){
break;
}
}
System.out.println("");
}
}
}
案例四:
金字塔
package com.huohua.day0407;
import java.util.Scanner;
public class ForFor4 {
public static void main(String[] args) {
//金字塔
Scanner scan = new Scanner(System.in);
System.out.println("请输入层数");
int level =scan.nextInt();
for (int i = 1; i <=level; i++) {
for (int j = 1;j<=level-i;j++){
System.out.print(" "+" ");
}
for (int k = 1;k<=2*i-1;k++){
System.out.print("*"+" ");
}
System.out.println();
}
}
}