Java™ 教程(控制流语句)
控制流语句
源文件中的语句通常按照它们出现的顺序从上到下执行,但是,控制流语句通过使用决策、循环和分支来分解执行流程,使你的程序能够有条件地执行特定的代码块,本节描述Java编程语言支持的决策语句(if-then
,if-then-else
,switch
),循环语句(for
,while
,do-while
)以及分支语句(break
,continue
,return
)。
if-then和if-then-else语句
if-then语句
if-then
语句是所有控制流语句中最基本的语句,它告诉程序只有在特定测试评估为true
时才执行某段代码,例如,Bicycle
类可以允许制动器仅在自行车已经运动时降低自行车的速度,applyBrakes
方法的一种可能实现如下:
void applyBrakes() {// the "if" clause: bicycle must be movingif (isMoving){ // the "then" clause: decrease current speedcurrentSpeed--;}
}
如果此测试评估为false
(意味着自行车不运动),则控制跳转到if-then
语句的末尾。
此外,只要“then”子句只包含一个语句,开括号和结束括号是可选的:
void applyBrakes() {// same as above, but without braces if (isMoving)currentSpeed--;
}
决定何时省略括号是个人品味的问题,省略它们会使代码变得更脆弱,如果稍后将第二个语句添加到“then”子句中,则常见的错误是忘记添加新所需的花括号,编译器无法捕获这种错误,你会得到错误的结果。
if-then-else语句
if-then-else
语句在“if
”子句求值为false
时提供辅助执行路径,如果在自行车不运动时应用制动器,你可以在applyBrakes
方法中使用if-then-else
语句来执行某些操作,在这种情况下,操作是简单地打印一条错误消息,指出自行车已经停止。
void applyBrakes() {if (isMoving) {currentSpeed--;} else {System.err.println("The bicycle has already stopped!");}
}
以下程序IfElseDemo
根据测试分数的值分配成绩:A得分为90%或以上,B得分为80%或以上,依此类推。
class IfElseDemo {public static void main(String[] args) {int testscore = 76;char grade;if (testscore >= 90) {grade = 'A';} else if (testscore >= 80) {grade = 'B';} else if (testscore >= 70) {grade = 'C';} else if (testscore >= 60) {grade = 'D';} else {grade = 'F';}System.out.println("Grade = " + grade);}
}
该程序的输出是:
Grade = C
你可能已经注意到testscore
的值可以满足复合语句中的多个表达式:76 >= 70
和76 >= 60
,但是,一旦满足条件,就会执行适当的语句(grade ='C';
),并且不评估其余条件。
switch语句
与if-then
和if-then-else
语句不同,switch
语句可以有许多可能的执行路径,switch
使用byte
、short
、char
和int
原始数据类型,它还适用于枚举类型(在枚举类型中讨论),String
类,以及一些包含某些基本类型的特殊类:Character
、Byte
、Short
和Integer
(在Number
和String
中讨论)。
以下代码示例SwitchDemo
声明了一个名为month
的int
,其值表示月份,代码使用switch
语句根据month
的值显示月份的名称。
public class SwitchDemo {public static void main(String[] args) {int month = 8;String monthString;switch (month) {case 1: monthString = "January";break;case 2: monthString = "February";break;case 3: monthString = "March";break;case 4: monthString = "April";break;case 5: monthString = "May";break;case 6: monthString = "June";break;case 7: monthString = "July";break;case 8: monthString = "August";break;case 9: monthString = "September";break;case 10: monthString = "October";break;case 11: monthString = "November";break;case 12: monthString = "December";break;default: monthString = "Invalid month";break;}System.out.println(monthString);}
}
在这种情况下,August
打印到标准输出。
switch
语句的主体称为switch
块,可以使用一个或多个case
或默认标签来标记switch
块中的语句,switch
语句计算其表达式,然后执行匹配的case
标签后面的所有语句。
你还可以使用if-then-else
语句显示月份的名称:
int month = 8;
if (month == 1) {System.out.println("January");
} else if (month == 2) {System.out.println("February");
}
... // and so on
决定是否使用if-then-else
语句或switch
语句是基于可读性和语句正在测试的表达式,if-then-else
语句可以基于值或条件的范围来测试表达式,而switch
语句仅基于单个整数、枚举值或String
对象来测试表达式。
另一个兴趣点是break
语句,每个break
语句都会终止封闭的switch
语句。控制流继续switch
块后面的第一个语句,break
语句是必要的,因为没有它们,switch
块中的语句就会失败:匹配的case
标签之后的所有语句都按顺序执行,而不管后续case
标签的表达式,直到遇到break
语句。程序SwitchDemoFallThrough
显示落入所有switch
块中的语句,该程序显示与整数月相对应的月份以及该年份中的月份:
public class SwitchDemoFallThrough {public static void main(String[] args) {java.util.ArrayList<String> futureMonths =new java.util.ArrayList<String>();int month = 8;switch (month) {case 1: futureMonths.add("January");case 2: futureMonths.add("February");case 3: futureMonths.add("March");case 4: futureMonths.add("April");case 5: futureMonths.add("May");case 6: futureMonths.add("June");case 7: futureMonths.add("July");case 8: futureMonths.add("August");case 9: futureMonths.add("September");case 10: futureMonths.add("October");case 11: futureMonths.add("November");case 12: futureMonths.add("December");break;default: break;}if (futureMonths.isEmpty()) {System.out.println("Invalid month number");} else {for (String monthName : futureMonths) {System.out.println(monthName);}}}
}
这是代码的输出:
August
September
October
November
December
从技术上讲,不需要最后的break
,因为流从switch
语句中退出,建议使用break
,以便更容易修改代码并减少错误,默认部分处理其中一个case
部分未明确处理的所有值。
以下代码示例SwitchDemo2
显示了语句如何具有多个case
标签,代码示例计算特定月份的天数:
class SwitchDemo2 {public static void main(String[] args) {int month = 2;int year = 2000;int numDays = 0;switch (month) {case 1: case 3: case 5:case 7: case 8: case 10:case 12:numDays = 31;break;case 4: case 6:case 9: case 11:numDays = 30;break;case 2:if (((year % 4 == 0) && !(year % 100 == 0))|| (year % 400 == 0))numDays = 29;elsenumDays = 28;break;default:System.out.println("Invalid month.");break;}System.out.println("Number of Days = "+ numDays);}
}
这是代码的输出:
Number of Days = 29
在switch语句中使用String
在Java SE 7及更高版本中,你可以在switch
语句的表达式中使用String
对象,以下代码示例StringSwitchDemo
根据名为month
的String
的值显示月份的编号:
public class StringSwitchDemo {public static int getMonthNumber(String month) {int monthNumber = 0;if (month == null) {return monthNumber;}switch (month.toLowerCase()) {case "january":monthNumber = 1;break;case "february":monthNumber = 2;break;case "march":monthNumber = 3;break;case "april":monthNumber = 4;break;case "may":monthNumber = 5;break;case "june":monthNumber = 6;break;case "july":monthNumber = 7;break;case "august":monthNumber = 8;break;case "september":monthNumber = 9;break;case "october":monthNumber = 10;break;case "november":monthNumber = 11;break;case "december":monthNumber = 12;break;default: monthNumber = 0;break;}return monthNumber;}public static void main(String[] args) {String month = "August";int returnedMonthNumber =StringSwitchDemo.getMonthNumber(month);if (returnedMonthNumber == 0) {System.out.println("Invalid month");} else {System.out.println(returnedMonthNumber);}}
}
此代码的输出为8。
将switch
表达式中的String
与每个case
标签关联的表达式进行比较,就好像正在使用String.equals
方法一样。为了使StringSwitchDemo
示例无论何种情况都接受任何month
,month
将转换为小写(使用toLowerCase
方法),并且与case
标签关联的所有字符串均为小写。
此示例检查
switch
语句中的表达式是否为null
,确保任何switch
语句中的表达式不为null
,以防止抛出NullPointerException
。
while和do-while语句
while
语句在特定条件为true
时继续执行语句块,其语法可表示为:
while (expression) {statement(s)
}
while
语句计算表达式,该表达式必须返回一个布尔值,如果表达式的计算结果为true
,则while
语句将执行while
块中的语句,while
语句继续测试表达式并执行其块,直到表达式求值为false
,使用while
语句打印1到10之间的值可以在以下WhileDemo
程序中完成:
class WhileDemo {public static void main(String[] args){int count = 1;while (count < 11) {System.out.println("Count is: " + count);count++;}}
}
你可以使用while
语句实现无限循环,如下所示:
while (true){// your code goes here
}
Java编程语言还提供了do-while
语句,可以表示如下:
do {statement(s)
} while (expression);
do-while
和while
之间的区别在于do-while
在循环的底部而不是顶部计算它的表达式,因此,do
块中的语句总是至少执行一次,如下面的DoWhileDemo
程序所示:
class DoWhileDemo {public static void main(String[] args){int count = 1;do {System.out.println("Count is: " + count);count++;} while (count < 11);}
}
for
语句
for
语句提供了一种迭代一系列值的简洁方法,程序员经常将其称为“for循环”,因为它反复循环直到满足特定条件,for
语句的一般形式可表示如下:
for (initialization; termination; increment) {statement(s)
}
使用此版本的for
语句时,请记住:
initialization
表达式初始化循环,循环开始时,它执行一次。- 当
termination
表达式的计算结果为false
时,循环终止。 - 每次迭代循环后都会调用
increment
表达式,这个表达式增加或减少一个值是完全可以接受的。
以下程序ForDemo
使用for
语句的一般形式将数字1到10打印到标准输出:
class ForDemo {public static void main(String[] args){for(int i=1; i<11; i++){System.out.println("Count is: " + i);}}
}
该程序的输出是:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
注意代码如何在初始化表达式中声明变量,此变量的范围从其声明扩展到由for
语句控制的块的末尾,因此它也可以用在终止和增量表达式中。如果在循环外部不需要控制for
语句的变量,则最好在初始化表达式中声明该变量。名称i
,j
和k
通常用于控制循环,在初始化表达式中声明它们会限制它们的生命周期并减少错误。
for
循环的三个表达式是可选的,可以创建一个无限循环,如下所示:
// infinite loop
for ( ; ; ) {// your code goes here
}
for
语句还有另一种用于迭代集合和数组的形式,此形式有时也称为增强的for
语句,可用于使循环更紧凑,更易于阅读,要演示,请考虑以下数组,其中包含数字1到10::
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
以下程序EnhancedForDemo
使用增强型for
循环遍历数组:
class EnhancedForDemo {public static void main(String[] args){int[] numbers = {1,2,3,4,5,6,7,8,9,10};for (int item : numbers) {System.out.println("Count is: " + item);}}
}
在此示例中,变量item
保存数字数组中的当前值,该程序的输出与之前相同:
Count is: 1
Count is: 2
Count is: 3
Count is: 4
Count is: 5
Count is: 6
Count is: 7
Count is: 8
Count is: 9
Count is: 10
我们建议尽可能使用for
语句的这种形式而不是一般形式。
分支语句
break语句
break
语句有两种形式:标记和未标记,你在前面的switch
语句讨论中看到了未标记的形式,你还可以使用未标记的break
来终止for
、while
或do-while
循环,如以下BreakDemo
程序所示:
class BreakDemo {public static void main(String[] args) {int[] arrayOfInts = { 32, 87, 3, 589,12, 1076, 2000,8, 622, 127 };int searchfor = 12;int i;boolean foundIt = false;for (i = 0; i < arrayOfInts.length; i++) {if (arrayOfInts[i] == searchfor) {foundIt = true;break;}}if (foundIt) {System.out.println("Found " + searchfor + " at index " + i);} else {System.out.println(searchfor + " not in the array");}}
}
该程序在数组中搜索数字12,break
语句在找到该值时终止for
循环,然后,控制流转移到for
循环后的语句,该程序的输出是:
Found 12 at index 4
未标记的break
语句终止最内层的switch
、for
、while
或do-while
语句,但带标签的break
终止外部语句。以下程序BreakWithLabelDemo
与前一个程序类似,但使用嵌套for
循环来搜索二维数组中的值,找到该值后,带标签的break
将终止外部for
循环(标记为“search”):
class BreakWithLabelDemo {public static void main(String[] args) {int[][] arrayOfInts = { { 32, 87, 3, 589 },{ 12, 1076, 2000, 8 },{ 622, 127, 77, 955 }};int searchfor = 12;int i;int j = 0;boolean foundIt = false;search:for (i = 0; i < arrayOfInts.length; i++) {for (j = 0; j < arrayOfInts[i].length;j++) {if (arrayOfInts[i][j] == searchfor) {foundIt = true;break search;}}}if (foundIt) {System.out.println("Found " + searchfor + " at " + i + ", " + j);} else {System.out.println(searchfor + " not in the array");}}
}
这是该程序的输出。
Found 12 at 1, 0
break
语句终止带标签的语句,它不会将控制流转移到标签上,控制流在标记(终止)语句之后立即转移到语句。
continue语句
continue
语句跳过for
、while
或do-while
循环的当前迭代,未标记的形式跳到最内层循环体的末尾,并计算控制循环的布尔表达式。以下程序ContinueDemo
逐步执行字符串,计算字母“p”的出现次数,如果当前字符不是p,则continue
语句将跳过循环的其余部分并继续执行下一个字符,如果是“p”,程序会增加字母数。
class ContinueDemo {public static void main(String[] args) {String searchMe = "peter piper picked a " + "peck of pickled peppers";int max = searchMe.length();int numPs = 0;for (int i = 0; i < max; i++) {// interested only in p'sif (searchMe.charAt(i) != 'p')continue;// process p'snumPs++;}System.out.println("Found " + numPs + " p's in the string.");}
}
这是该程序的输出:
Found 9 p's in the string.
要更清楚地看到此效果,请尝试删除continue
语句并重新编译,当你再次运行程序时,计数将是错误的,说它找到35个p而不是9个。
标记的continue
语句跳过标记有给定标签的外循环的当前迭代,以下示例程序ContinueWithLabelDemo
使用嵌套循环来搜索另一个字符串中的子字符串,需要两个嵌套循环:一个遍历子字符串,一个迭代搜索的字符串,以下程序ContinueWithLabelDemo
使用标记形式的continue
来跳过外部循环中的迭代。
class ContinueWithLabelDemo {public static void main(String[] args) {String searchMe = "Look for a substring in me";String substring = "sub";boolean foundIt = false;int max = searchMe.length() - substring.length();test:for (int i = 0; i <= max; i++) {int n = substring.length();int j = i;int k = 0;while (n-- != 0) {if (searchMe.charAt(j++) != substring.charAt(k++)) {continue test;}}foundIt = true;break test;}System.out.println(foundIt ? "Found it" : "Didn't find it");}
}
这是该程序的输出。
Found it
return语句
最后一个分支语句是return
语句,return
语句从当前方法退出,控制流返回到调用方法的位置,return
语句有两种形式:一种是返回值,另一种是不返回值,要返回值,只需将值(或计算值的表达式)放在return
关键字之后。
return ++count;
返回值的数据类型必须与方法声明的返回值的类型匹配,当方法声明为void
时,请使用不返回值的return
形式。
return;
类和对象课程将涵盖你需要了解的有关编写方法的所有内容。
控制流程语句总结
if-then
语句是所有控制流语句中最基本的语句,它告诉程序只有在特定测试评估为true
时才执行某段代码。if-then-else
语句在“if”子句求值为false
时提供辅助执行路径,与if-then
和if-then-else
不同,switch
语句允许任意数量的可能执行路径。while
和do-while
语句在特定条件为true
时不断执行语句块,do-while
和while
之间的区别在于do-while
在循环的底部而不是顶部计算它的表达式,因此,do
块中的语句总是至少执行一次。for
语句提供了一种迭代一系列值的简洁方法,它有两种形式,其中一种用于循环集合和数组。
上一篇;表达式、语句和块
下一篇:类
Java™ 教程(控制流语句)相关推荐
- 软考 java程序设计,软考程序员考点Java语言程序设计之控制流语句分类
下面希赛小编为大家整理的软考程序员考点Java语言程序设计之控制流语句分类,希望能帮助学友们.具体内容如下: 分类 它们由特定的语句定义符组成.C语言有九种控制语句.可分成以下三类: (1).条件判断 ...
- Java教程:Java continue语句详解
Java教程有时强迫一个循环提早反复是有用的,也就是,你可能想要继续运行循环,但是要忽略这次重复剩余的循环体的语句,所以 Java 提供了 continue 语句.continue 语句是 break ...
- Java教程:Java break语句:跳出循环
Java教程某些时候需要在某种条件出现时强行终止循环,而不是等到循环条件为 false 时才退出循环.此时,可以使用 break 来完成这个功能. 本篇文章由 泉州SEO www.234yp.com ...
- Java教程:Java return语句
Java教程return 关键字并不是专门用于结束循环的,return 语句用于终止函数的执行或退出类的方法,并把控制权返回该方法的调用者.如果这个方法带有返回类型,return 语句就必须返回这个类 ...
- Java基础教程11-switch语句
这篇来介绍Java中switch语句的基本用法.一提到switch语句,我就会想起用成绩等级划分来举例这个switch的实现过程.相关demo代码如下 package lessons;public c ...
- HowToDoInJava Java 教程·翻译完成
原文:HowToDoInJava 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远. ApacheCN 学习资源 目录 核心 Java 教程 什 ...
- Java™ 教程(目录)
Java™ 教程 Java教程是为JDK 8编写的,本页面中描述的示例和实践没有利用在后续版本中引入的改进. Java教程是希望使用Java编程语言创建应用程序的程序员的实用指南,其中包括数百个完整的 ...
- java的goto语句_语法 - Java中是否有goto语句?
语法 - Java中是否有goto语句? 我对此感到困惑. 我们大多数人都被告知Java中没有任何goto语句. 但我发现它是Java中的关键词之一. 哪里可以使用? 如果它不能使用,那么为什么它作为 ...
- mybatis java传参_[Java教程]Mybatis批量和传参
[Java教程]Mybatis批量和传参 0 2014-07-31 04:00:12 MyBatis中批量插入 方法一: resultType="java.lang.String" ...
最新文章
- 花33元租号玩2小时王者荣耀,未成年为绕过防沉迷用上黑科技上号器App
- NPS的几个技术点(在Win 2003中称为Internet 验证服务)
- c语言程序设计华北电力大学,华北电力大学-《C语言程序设计》复习资料.pdf
- CodeForces - 1549F1 Gregor and the Odd Cows (Easy)(几何+数论)
- mysql boost 5.7.21_mysql 5.7.21 安装配置方法图文教程(window)
- 简单谈谈5G/C-V2X技术与自动驾驶的关系
- Linux 多线程开发-线程创建pthread_creat
- oracle 试图访问已经在使用的事物处理临时表,解决ORA-14450:试图访问已经在使用的事务处理临时表-Oracle...
- java adminlte 使用_AdminLTE2管理后台自定义
- Nodejs Set和Map的使用
- zabbix4.2学习笔记--监控tomcat
- angular中的MVVM模式
- IDEA破解方法,永久激活
- 【Mysql】Error 1826: Duplicate foreign key constraint 错误
- 终于学会数组的使用啦~~~------C语言数组学习笔记详解
- jdbc连接MysQL数据库时报错:The server time zone value ‘ ‘ is unrecognized or represents more than
- android mvvm官方文档,MVVM: 这是一个android MVVM 框架,基于谷歌dataBinding技术实现
- Java后台下载Excel模板并输出到浏览器上的样例
- php 中日期时间函数大全,PHP 中日期时间函数 date() 用法总结
- C#开发BIMFACE系列1 BIMFACE 简介
热门文章
- java threadlocal用法_Java ThreadLocal的用法解析
- gearman mysql 扩展_Gearman的安装和使用
- eclipse error
- 【Unity与23种设计模式】访问者模式(Visitor)
- sqlite3简单操作
- OstrichNet 简易统计信息收集工具
- Linux上安装wkhtmltopdf,以及调试所遇到的问题
- Kafka核心源码解析 - SocketServer源码解析
- 设计模式之单例模式的多重实现
- linux history 看更多历史记录_每周开源点评:定义云原生、拓展生态系统,以及更多的行业趋势 | Linux 中国...