当前位置:网站首页>第五章:流程控制
第五章:流程控制
2022-07-25 16:51:00 【执手天涯@】
第五章:流程控制
5.1、分支语句
简介:Java的分支语句是在部分程序代码满足特定条件的情况下才会被执行。
5.1.1、if else语句
基本语法:
if(布尔表达式){
程序代码块
}else{
程序代码块
}
注意事项:
- if后面的表达式必须是布尔表达式,而不能为数字类型。
- if后面的else语句不是必须的。
- 加入if语句后面或else语句后面的程序代码块中包含多条语句,则必须存放在{}内部。如果代码块只有一句可以不使用{}
- if else语句有一种特殊的串联编程分格。
public void method(int x){
if(x>0){
System.out.println("大于0");
}else if(x==0){
System.out.println("等于0");
}else{
System.out.println("小于0");
}
}
经典案例:
//判断某年是否式闰年
//方法一
public boolean idleapYear(int year){
if((year%4==0&&year%100!==0)||(year%400==0))
return true;
else
return false;
}
5.1.2、switch语句
简介:switch语句是多路分支语句,基本语法
switch (expr){
case value1:
...
break;
case value2:
...
break;
...
default:
...
break;
}
注意事项:
- 在表达式中的参数值类型包含以下几种
- 与int类型兼容的类型
- 字符串类型
- 枚举类型
- switch语句中最多只能有一个default子句。
- switch语句中与那个case匹配就从那个地方开始执行。遇到break就跳出整个switch
- switch语句可以使用if else语句来实现
经典案例:
public void sortScore(char grade){
switch(grade){
case'A':
System.out.println(grade+"is 85~100");
break;
case'B':
System.out.println(grade+"is 70~84");
break;
case'C':
System.out.println(grade+"is 60~69");
break;
case'D':
System.out.println(grade+"is<60");
break;
default:
System.out.println("没有等级");
break;
}
}
字符串类型的参数
String color="red";
switch(color){
case "blue":
System.out.println("蓝色");
break;
case "red":
System.out.println("红色");
break;
default:
System.out.println("其他颜色");
break;
}
枚举类型的参数:
public class SwitchTest{
enum Color{
red,blue,yellow}//定义枚举类
public static void main(String [] args){
Color c=Color.blue;
switch(c){
case red:
System.out.println("红色");
break;
case blue:
System.out.println("蓝色");
break;
case yellow:
System.out.println("黄色");
break;
default:
System.out.println("其他颜色");
}
}
}
5.2、循环语句
简介:循环语句的作用是反复执行一段代码,直到不满足条件为止。
四大部分:
- 初始化部分:用来设置循环的一些初始条件,设置循环控制变量的初始值
- 循环条件:这是一个布尔表达式
- 循环体:这是循环的主体内容
- 迭代部分:用来改变循环变量的值。
5.2.1、while语句
简介:while循环语句是循环语句中最基本的循环语句。基本语法如下:
[初始化部分]
while(循环条件){
循环体(包含迭代部分)
}
使用注意事项:
- 如果循环体包含多条语句,必须放在大括号内,如果循环体只有一条语句,可以不用大括号。
- while循环一开始就开始判断表达式,如果返回值是false则循环体一次也不会执行。
- 循环体可以为空
- 确保有终止循环的条件,避免死循环。
经典案例:
public static int max(int [] array){
if(array==null||array.length==0){
throw new IllegalArgumentException("无效的数组");
int index=1,location=0;//初始化部分
while(index<array.length){
//循环条件,index为循环控制变量
//循环体
if(array[location]<array[index]){
location=index;
index++;
}
}
return array[location];
}
}
5.2.2、do while语句
简介:与while语句一样都是循环语句,但是do while循环会先执行一次循环体,再进行条件的判断,无论如何都会执行一次循环体。
语法格式:
[初始化部分]
do{
循环体 ,包含迭代部分
}while(循环条件);
5.2.3、for语句
简介:for循环和while循环一样都是先判断循环条件,再执行循环体。语法如下:
for(初始化部分,循环条件,迭代部分){
循环体
}
注意事项:
- 如果for循环的循环体只有一行语句,可以不使用{}
- 控制for循环的变量常常只是用于本循环,而不能再程序的其他地方使用。其作用域为当前for循环不能在for循环以外的地方使用它。
- 在初始化和迭代部分可以使用逗号语句,逗号语句是用逗号进行语句序列的分隔。
- for循环的初始化部分和迭代部分都可以为空
- for循环语句一般用在循环次数事先可以确定的情况。while循环和do while循环用在循环次数不清楚的情况。
5.2.4、foreach语句
简介:foreach语句是for语句的特殊简化版本,它可以简化遍历数组和集合的代码。
语法格式:
for(元素类型 元素变量X :待遍历的数组){
引用了变量x的Java语句
}
5.2.5、多重循环
简介:各种循环语句可以互相嵌套,组成多重循环;
经典案例:九九乘法表
public static void main(String[] args){
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+j*i+"\t");
}
System.out.println();
}
}
5.3、流程跳转语句
简介:break、continue、return语句都是流程跳转语句,但是他们有不同的使用条件和作用
- break:表示跳出switch或者当前的循环。
- continue:表示跳出本次循环,执行下一次循环。
- return:表示退出本方法,返回上一层调用的方法,如果该方法不是void,则需要提供相应的返回值。
边栏推荐
- 如何使用 4EVERLAND CLI 在 IPFS 上部署应用程序
- 【目标检测】YOLOv5跑通VOC2007数据集(修复版)
- WPF implements user avatar selector
- LVGL 7.11 tileview界面循环切换
- 【redis】redis安装
- Google Earth Engine——全球建筑物GlobalMLBuildingFootprints矢量集合下载
- SAP Fiori 的附件处理(Attachment handling)
- Is the online account opening of Founder futures reliable and safe?
- Baidu rich text editor ueeditor single image upload cross domain
- 自定义mvc项目登录注册和树形菜单
猜你喜欢

easyui datagrid控件使用

2D semantic segmentation -- deeplabv3plus reproduction
![[OBS] Reprint: what about the serious delay of OBS live broadcast and Caton?](/img/fd/54fcae2bc3f4313e9401c735ef74b8.png)
[OBS] Reprint: what about the serious delay of OBS live broadcast and Caton?

如何安装govendor并打开项目

微信公众号开发之消息的自动回复

城市燃气安全再拉警钟,如何防患于未“燃”?

数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?

How to deploy applications on IPFs using 4everland cli

IAAs infrastructure cloud cloud network

Outlook 教程,如何在 Outlook 中搜索日历项?
随机推荐
【redis】redis安装
7.依赖注入
Breakthrough in core technology of the large humanoid Service Robot Walker x
[OBS] frame loss and frame priority before transmission
Register service instances in ngmodule through dependency injection
C # introductory basic tutorial
【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part3):基于规则的问题分类
ILSSI认证|六西格玛DMAIC的历程
QT listview list display component notes
多租户软件开发架构
80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
What is the monthly salary of 10000 in China? The answer reveals the cruel truth of income
mindoc制作思维导图
测试框架-unittest-命令行操作、断言方法
unity 最好用热更方案卧龙 wolong
[xiao5 chat] check the official account < the service provided by the official account has failed, please wait a moment>
如何安装govendor并打开项目
Baidu rich text editor ueeditor single image upload cross domain
数据分析与隐私安全成 Web3.0 成败关键因素,企业如何布局?
激荡20年,芯片产能从零起步到反超美国,中国制造的又一大成就