Java知识点复习:Day05

流程控制语句


目录

  1. 顺序结构
  2. if语句
             •2.1 if…else 判断语句
             •2.2 if…else if…else 判断语句
             •2.3 if语句和三元运算符的互换
  3. switch 选择语句
             •3.1 case的穿透性
  4. 循环语句
             •4.1 for循环
             •4.2 while 循环
             •4.3 do…while 循环语句
             •4.4 for 和 while 的小区别
             •4.5 跳出语句
                                ♢break
                                ♢continue

1. 顺序结构


public class new1 {
	public static void main(String[] args){
		// 顺序执行,根据编写的顺序,从上到下运行
		System.out.println("第一");
		System.out.println("第二");
		System.out.println("第三");
		}
	}

2. if 语句

格式:if(关系表达式)  {
语句体;
}

执行流程:
首先判断关系表达式看其结果是true还是false,如果是true就执行语句体,如果是false就不执行语句体。

public class new2 {
	public static void main(String[] args){
		System.out.println("开始");
		// 定义两种变量
		int a = 10;
		int b = 20;
		// 变量使用if判断
		if (a==b) {
			System.out.println("a等于b");
		}
		int c = 10;
		if (a==c) {
		System.out.println("a等于c");
		}
		System.out.println("结束");
		}
}

2.1 if–else 判断语句
格式:if (关系表达式)  {
语句体1;
} else  {
语句体2;
}

执行流程:
首先判断关系表达式看其结果是true还是false, 如果是true就执行语句体1 ,如果是false就执行语句体2。

public class new2 {
	public static void main(String[] args){
	// 判断给定的数据是奇数还是偶数
	// 定义变量
	int a = 1;
	if (a % 2 == 0) {
		System.out.println("a是偶数");
	} else {
		System.out.println("a是奇数");
	}
	System.out.println("结束");
		}
}

2.2 if…else if…else 判断语句
格式:if (判断条件1) {
执行语句1;
} else if (判断条件2) {
执行条件2;
}

} else if  (判断条件n) {
执行语句n;
} else  {
执行语句n+1;
}

执行流程:
首先判断关系表达式1看其结果是true还是false, 如果是true就执行语句体1 ,如果是false就继续判断关系表达式2看其结果是true还是false ,如果是true就执行语句体2 ,如果是false就继续判断关系表达式…看其结果是true还是false ,如果没有任何关系表达式为true,就执行语句体n+1。

public class new2 {
	public static void main(String[] args){
	// X 和 y的关系满足如下:
	// x>=3 y = 2x + 1;
	// -1<x<3   y = 2x;
	// x<=-1    y = 2x-1;
	// 根据给定的X的值,计算出y的值并输出
	// 定义变量
	int x = 5;
	int y;
	if (x>=3) {
		y=2*x+1;
	} else if (x>-1 && x<3) {
		y=2*x;
	} else {
		y=2*x-1;
	}
	System.out.println("y的值是"+y);
	}
}

2.3 if语句和三元运算符的互换

int a = 20;
		int b = 40;
		
		// 首先使用三元运算符
		
		int max1 = a > b ? a : b;
		System.out.println("最大值"+ max1);
		
		// 使用if 语句
		
		int max;
		if (a > b) {
			
			max = a;
		} else {
			max = b;
		}
		System.out.println("最大值" + max);

3. switch 选择语句
格式:
switch(表达式) {
case 常量值1;
break;
case 常量值2;
break;

default:
语句体n+1;
break;
}

执行流程:

首先计算出表达式的值。
其次,和case依次比较,一旦有对应的值,就会执行相应的语句,在执行的过程中,遇到break就会结束。
最后,如果所有的case都和表达式的值不匹配,就会执行default语句体部分,然后程序结束掉。

public class new2 {
	public static void main(String[] args){
	// 定义变量,判断是星期几
	int day = 20;
	// switch语句实现选择
		int num2 = day&7;
		
		switch (num2) {
			case 1:
			System.out.println("今天是星期一");
			break;
			case 2:
			System.out.println("今天是星期二");
			break;
			case 3:
			System.out.println("今天是星期三");
			break;
			case 4:
			System.out.println("今天是星期四");
			break;
			case 5:
			System.out.println("今天是星期五");
			break;
			case 6:
			System.out.println("今天是星期六");
			break;
			case 0:
			System.out.println("今天是周日");
			break;
			
			default:
			System.out.println("您输入的数字有误");
			break; 
	}
}
}

3.1 case的穿透性

在switch语句中,如果case的后面不写break,将出现穿透现象,也就是不会在判断下一个case的值,直接向后运行,直到遇到break,或者整体switch结束。

public class new2 {
	public static void main(String[] args){
		int num3 = 2;
		
		switch (num3) {
		case 1:
		System.out.println("你好");
		break;
		case 2:
		System.out.println("我好");
		// break;
		case 3:
		System.out.println("大家好");
		break;
		
		default:
		System.out.println("哈哈哈");
		break;
		
		}
}
}

这里的输出结果是:我好大家好


4. 循环语句

4.1 for循环
for(初始化表达式①; 布尔表达式②; 步进表达式④){
循环体③
}

执行流程:
执行顺序:①②③④>②③④>②③④…②不满足为止。
①负责完成循环变量初始化
②负责判断是否满足循环条件,不满足则跳出循环
③具体执行的语句
④循环后,循环条件所涉及变量的变化情况

public class new2 {
	public static void main(String[] args){
		// 定义一个初始化变量,记录累加求和,初始值为0
		int sum = 0;
		// 利用for循环获取1-100之间的数字
		for (int i = 1;i <= 100; i++) {
			// 判断获取的数组是奇数还是偶数
			if (i % 2 == 0){
				// 如果是偶数就累加求和
				sum += i;
				
			}
		}
		// 循环语句结束之后,打印累加结果
		System.out.println("sum"+sum); // sum2550
}
}


4.2 while 循环

格式:
初始化表达式①
while(布尔表达式②){
环体③
步进表达式④
}

执行流程:
执行顺序:①②③④>②③④>②③④…②不满足为止。
①负责完成循环变量初始化。
②负责判断是否满足循环条件,不满足则跳出循环。
③具体执行的语句。
④循环后,循环变量的变化情况。

while循环计算1-100之间的和

public class new2 {
	public static void main(String[] args){
		// 使用while循环实现
		// 定义一个变量,记录累加求和
		int sum = 0;
		// 定义初始化表达式
		int i = 1;
		// 使用while循环让初始化表达值变化
		while(i<=100) {
			// 累加求和
			sum += i;
			// 步进表达式改变变量的值
			i++;
		}
		// 打印求和的变量
		System.out.println("1-100的和是"+sum); // 1-100的和是5050

			}
		}


在这里插入图片描述


4.3 do…while 循环语句

格式:
初始化表达式①
do{
循环体③
步进表达式④
}while(布尔表达式②);

执行流程
执行顺序:①③④>②③④>②③④…②不满足为止。
①负责完成循环变量初始化。
②负责判断是否满足循环条件,不满足则跳出循环。
③具体执行的语句
④循环后,循环变量的变化情况

// 输出10次快写完作业
public class new2 {
	public static void main(String[] args){
		int x = 1;
		do {
			System.out.println("快写完作业");
			x++;
		}while(x<=10);

			}
}
		

4.4 for 和 while 的小区别:

控制条件语句所控制的那个变量,在for循环结束后,就不能再被访问到了,而while循环结束还可以继
续使用,如果你想继续使用,就用while,否则推荐使用for。原因是for循环结束,该变量就从内存中消失,能够提高内存的使用效率。
已知循环次数的时候使用推荐使用for循环次数未知的时推荐使用while。


4.5 跳出语句

break
使用场景:终止switch或者循环
在选择结构switch语句中
在循环语句中
离开使用场景的存在是没有意义的

public class new2 {
	public static void main(String[] args){
		for (int i =1; i <=20; i++) {
			
			// 需求:打印完三次你好之后结束循环
			if (i ==4){
				break;
			}
			System.out.println("你好"+i); // 你好1你好2你好3
		}

			}
}
		

continue

使用场景:结束本次循环,继续下一次的循环

public class new2 {
	public static void main(String[] args){
		for (int i =1; i <=10; i++) {
			
			// 需求:不打印第四次你好
			if (i ==4){
				continue;
			}
			System.out.println("你好"+i); // 你好1你好2你好3你好5你好6你好7你好8你好9你好10


		}

			}
}
		


附:判断语句练习:

在这里插入图片描述

public class new2 {
	public static void main(String[] args){
		int chengji = 10 ;
		if(chengji<0 || chengji >100){
			System.out.println("朋友,你的成绩有点东西啊");
			
		} else if(chengji>=90 && chengji <=100){
			System.out.println("您的成绩属于优秀");
		} else if(chengji>=80 && chengji<90){
			System.out.println("您的成绩属于好");
			
		} else if (chengji>=70 && chengji<80){
			System.out.println("您的成绩属于良");
		} else if (chengji>=60 && chengji<70){
			System.out.println("您的成绩属于及格");
		}else {
			System.out.println("您的成绩属于不及格,请继续努力啊");
		}

		}

			}

在这里插入图片描述


2020080605036 +luoxin
有运行结果截图的为课后练习题目

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值