源文件中的语句一般按照它们出现的顺序从上到下执行,可是,控制流语句经过使用决策、循环和分支来分解执行流程,使你的程序可以有条件地执行特定的代码块,本节描述Java编程语言支持的决策语句(if-then
,if-then-else
,switch
),循环语句(for
,while
,do-while
)以及分支语句(break
,continue
,return
)。java
if-then
语句是全部控制流语句中最基本的语句,它告诉程序只有在特定测试评估为true
时才执行某段代码,例如,Bicycle
类能够容许制动器仅在自行车已经运动时下降自行车的速度,applyBrakes
方法的一种可能实现以下:程序员
void applyBrakes() { // the "if" clause: bicycle must be moving if (isMoving){ // the "then" clause: decrease current speed currentSpeed--; } }
若是此测试评估为false
(意味着自行车不运动),则控制跳转到if-then
语句的末尾。express
此外,只要“then”子句只包含一个语句,开括号和结束括号是可选的:编程
void applyBrakes() { // same as above, but without braces if (isMoving) currentSpeed--; }
决定什么时候省略括号是我的品味的问题,省略它们会使代码变得更脆弱,若是稍后将第二个语句添加到“then”子句中,则常见的错误是忘记添加新所需的花括号,编译器没法捕获这种错误,你会获得错误的结果。segmentfault
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%或以上,依此类推。app
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';
),而且不评估其他条件。oop
与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; else numDays = 28; break; default: System.out.println("Invalid month."); break; } System.out.println("Number of Days = " + numDays); } }
这是代码的输出:
Number of Days = 29
在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
语句在特定条件为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
语句有两种形式:标记和未标记,你在前面的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
语句跳过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's if (searchMe.charAt(i) != 'p') continue; // process p's numPs++; } 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 ++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
语句提供了一种迭代一系列值的简洁方法,它有两种形式,其中一种用于循环集合和数组。