当前位置:网站首页>流程控制之分支语句
流程控制之分支语句
2022-04-23 14:12:00 【Zinksl】
:::info
根据 遗忘曲线:如果没有记录和回顾,6天后便会忘记75%的内容
读书笔记正是帮助你记录和回顾的工具,不必拘泥于形式,其核心是:记录、翻看、思考
:::
流程控制语句概览
在默认程序中是顺序结构,从上往下依次执行;用思维导图,结构化核心概念。

分支语句
if语句(判断条件最终必须为:布尔类型值)
1 简单if语句格式:
语句格式:
if (判断条件){
语句体; //判断条件成立执行语句体;
}
int nub1 = 1;
int nub2 = 2;
if(nub1>nub2){
System.out.println("执行了语句体"); //判断条件为false,此语句不执行;
}
2 if…else语句格式
语句格式:
if (判断条件){
语句体1; //判断条件成立执行语句体1;
}else{
语句体2; //判断条件不成立执行语句体2;
}
int nub1 = 1;
int nub2 = 2;
if(nub1>nub2){
System.out.println("执行了语句体1"); //判断条件为false,此语句不执行体;
}else{
System.out.println("执行了语句体2"); //判断条件为false,执行此语句体;
}
3 if…else if…else…语句格式
语句格式:
if (判断条件1){
语句体1; //判断条件成立执行语句体1;
}else if(判断条件2){
语句体2; //判断条件1不成立,判断条件2成立,执行语句体2;
}else{
语句体3; //判断条件1和2都不成立执行语句体3;
}
/* 考试奖励: 分数>=90:奖励自行车一辆 90>分数>=80:奖励游乐园一日游 80>分数:一顿狂揍 */
public class IfTest {
public static void main(String[] args) {
int chengji;
Scanner sc = new Scanner(System.in);
System.out.println("请输入成绩:");
chengji = sc.nextInt();
if (chengji <=100 && chengji>=0){
if (chengji>=90){
System.out.println("恭喜!奖励自行车一辆!");
}else if (chengji>=80 && chengji<90){
System.out.println("恭喜!奖励游乐园一日游!");
}else{
System.out.println("来吧一顿接受狂揍!");
}
}else{
System.out.println("成绩输入有误!");
}
}
}
switch语句
switch语句格式:
switch(){
case 1:
语句体1;
break;
case 2:
语句体2;
break;
case 3:
语句体3;
break;
.
.
.
default:
默认值语句体;
break;}
1 注意事项:jdk特性与week的取值范围
- 基本类型:byte、short、int、long
- 引用类型:jdk5开始,允许枚举类型;jdk7开始,允许String
- jdk14开始允许,一个case 后跟多个值:case 1,2,3:
- if与switch选用:范围性的判断用if;固定值判断用switch;
switch(week){
case 1:
// 语句体1;
break;
case 2:
// 语句体2;
break;
case 3:
// 语句体3;
break;
default:
// 默认语句体;
break;
}
2 case穿透(可以优化代码)
case穿透示例:如果有多个重复的语句体可以使用case穿透
switch(week){
case 1:
case 2:
case 3:
//重复语句体;
break;
default:
break;
}
3 jdk14以后新特性(可以优化代码)
case穿透示例:如果有多个重复的语句体可以使用case穿透
switch(week){
case 1,2,3:
//重复语句体;
break;
default:
break;
}
注意:在编程中最常用的逻辑运算符是: 短路与(&&)、短路或(||)和非(!);可以提高代码的运算效率;
版权声明
本文为[Zinksl]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Zinkse/article/details/123876418
边栏推荐
- 翻牌效果
- Use cases of the arrays class
- 微信小程序轮播图swiper
- mysql锁数据库锁
- ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory
- On the problem of cliff growth of loss function in the process of training
- API gateway / API gateway (IV) - use of Kong - Integrated JWT and fuse plug-in
- OpenStack命令操作
- 解决ssh配置文件优化以及连接慢的问题
- After entering the new company, the operation and maintenance engineer can understand the deployment of the system from the following items
猜你喜欢

A table splitting implementation scheme of MySQL and InnoDB, MyISAM and MRG_ Introduction to MyISAM and other engine application scenarios

API Gateway/API 网关(二) - Kong的使用 - 负载均衡Loadbalance

Thread group ThreadGroup uses introduction + custom thread factory class to implement threadfactory interface

OpenStack命令操作

Installation and use of postman pit

Operation instructions of star boundary text automatic translator

在Clion中给主函数传入外部参数

MySQL数据库讲解(七)

MySQL-InnoDB-事务

常见存储类型和FTP主被动模式解析
随机推荐
yml引用其他变量
void*是怎样的存在?
STD:: map and STD:: vector memory free
asp.net使用MailMessage发送邮件的方法
MySQL数据库讲解(十)
A table splitting implementation scheme of MySQL and InnoDB, MyISAM and MRG_ Introduction to MyISAM and other engine application scenarios
贷款市场报价利率(LPR)与贷款基准利率介绍
redis数据库讲解(四)主从复制、哨兵、Cluster群集
How to do a project easily
About the configuration and use of json5 in nodejs
DP energy Necklace
gif转为静态图片处理
JS recursion (1)
xx项目架构随记
顺序栈的基本操作
json date时间日期格式化
Docker篇 (五) MySQL的安装
JumpServer
进入新公司,运维工程师从下面这几项了解系统的部署
ActiveMQ Basics