流程控制语句:
源文件中的语句通常按照它们的出现顺序从上到下执行。然而,控制流程语句通过使用选择(决策),循环和分支来分解执行流程,使程序有条件地执行特定的代码块。本节介绍将要介绍三大结构:
- 条件分支语句(if-then,if-then-else,switch)
- 循环语句(for,while,do-while)
- switch分支语句(break,continue,return)
No.1—条件结构
if-then语句
它是所有控制流语句中最基本的。它告诉您的程序只有在特定测试评估为true时才执行某段代码。例如,如果自行车已经运动,则自行车类可以允许制动器降低自行车的速度。 applyBrakes方法的一个可能的实现可以如下:
void applyBrakes() {
// the "if" clause: bicycle must be moving
if (isMoving){
// the "then" clause: decrease current speed
currentSpeed--;
}
}
如果此测试评估为false(意味着自行车不运动),则控制跳转到if-then语句的末尾。 另外,打开和关闭大括号是可选的,只要“then”子句只包含一个语句:
void applyBrakes() {
// same as above, but without braces
if (isMoving)
currentSpeed--;
}
决定何时忽略大括号是个人品味的问题。但是,省略它们可以使代码更脆弱。如果第二个语句后来添加到“then”子句中,常见的错误就是忘记添加新的必需大括号。编译器无法捕获这种错误 ; 你会得到错误的结果。所以,我们更推荐使用前一种方式,用花括号括起来。
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。 但是,一旦满足其中的一个条件,76 > 70,则执行适当的语句(grade =’C’; ),并且不再执行剩余条件的计算。
No.2—switch结构
与if-then和if-then-else语句不同,switch语句可以有多个可能的执行路径。一个switch可以使用byte,short,char和int原语数据类型。它也适用于枚举类型(在Enum Types中讨论),String类和一些包含某些基本类型的特殊类:Character,Byte,Short和Integer(在Numbers和Strings中讨论)。 以下代码示例SwitchDemo声明一个名为month的int值,其值表示一个月。代码使用switch语句根据月份的值显示月份的名称。
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);
}
}
在这种情况下,8月打印到标准输出。 switch语句的主体称为开关块。开关块中的语句可以标注一个或多个案例或默认标签。 switch语句对其表达式进行求值,然后执行匹配案例标签后面的所有语句。 当然,您也可以使用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语句,是基于代码的可读性和语句正在测试的表达式。
if-then-else语句可以根据值或条件的范围测试表达式,而switch语句仅基于单个整数,枚举值或String对象来测试表达式。
另一个兴趣点是break声明。每个break语句终止封闭的switch语句。控制流继续开关块后面的第一个语句。
break语句是必要的,因为没有它们,交换机块中的语句落空:匹配的案例标签之后的所有语句都将按顺序执行,而不管后续案例标签的表达,直到遇到break语句。
程序SwitchDemoFallThrough显示开关块中的语句。该程序显示与整数月份相应的月份以及年份中的以下月份:
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
从技术上讲,最终的中断是不需要的,因为流程不在switch语句中。建议使用中断,以便修改代码更容易,更少的出错。默认部分处理一个案例部分未明确处理的所有值。 以下代码示例SwitchDemo2显示了一个语句如何具有多个案例标签。代码示例计算特定月份的天数:
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
No.3—循环结构
while和do-while语句
while语句在特定条件为真时持续执行一组语句。其语法可以表示为:
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);
}
}
输出的结果是:Count is : 1
for语句
for语句提供了一个紧凑的方法来迭代一系列值。程序员通常将其称为“for循环”,因为它反复循环的方式直到满足特定条件。 for语句的一般形式可以表示如下:
for (initialization; termination;
increment) {
statement(s)
}
当使用此版本的for语句时,请记住:
- 初始化表达式,初始化循环 ; 当循环开始时,它被执行一次。
- 当终止表达式求值为false时,循环终止。
- 递增(自增)表达式通过循环进行每次迭代后调用 ; 这个表达式特别适用于增加或减少一个值。
以下程序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使用增强型循环遍历数组:
class EhancedForDemo{
public static void main(String[] args){
byte[] charArray ={0,1,2,3,4,5,6,7,8,9,10};
//tranditional way to use for statements
/* for ( byte i = 0 ; i < 11 ; i ++){
System.out.println("count is : " + charArray[i]);
} */
//Enhanced For statements
for(byte item : charArray ){
System.out.println("count is : " + 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语句中看到了未标记的形式。您还可以使用未标记的中断来终止for,while或do-while循环,如以下BreakDemo程序所示:
未标记的:
class BreakDemo{
public static void main (String[] agrs){
short[] shortArray = {1,0,-12,123,6688,32767,12};
short searchFor = -12;
boolean foundIt = false;
byte i;
for(i = 0 ; i < shortArray.length ; i ++ ){
if(searchFor == shortArray[i]){
foundIt = true;
break ;
}
}
if(foundIt){
System.out.println("we find it ! it is at the index :" + i );
}else{
System.out.println( searchFor + " not in array !");
}
}
}
该程序在数组中搜索数字-12。 break语句以粗体显示,当找到该值时终止for循环。控制流然后在for循环之后传输到语句。该程序的输出是:
we find it ! it is at the index : 2
使用标记的break语句:
未标记的break语句终止最内层的switch,for,while,或do-while语句,但标记的break终止外部语句。以下程序BreakWithLabelDemo与以前的程序类似,但使用嵌套for循环来搜索二维数组中的值。当找到该值时,标记的中断终止外部for循环(标记为“search”):
class LibeledBreakDemo{
public static void main (String[] agrs){
short[][] shortArray = {{1,2,-12,1234,2000,6570,32767},{-16,23,55,767,668,6688,98},{23,43,123,88,45,45,65,56},{23,-12,32,54,77,88,98}};
short searchFor = -12;
byte i ;
byte j = 0 ;
boolean foundIt = false;
search:
for(i = 0 ; i < shortArray.length ; i ++ ){
for(j = 0 ; j < shortArray.length ; j ++ ){
if(searchFor == shortArray[i][j]){
foundIt = true;
break search;
}
}
}
if(foundIt){
System.out.println(searchFor + " at the index[" + i +"]" + "[" + j + "]" );
}else{
System.out.println(searchFor + " not in shortArray !");
}
}
}
程序的输出结果为:-12 at the index [0][2]
continue语句
未标记的continue语句:
continue语句跳过for,while或do-while循环的当前迭代。未标记的结构跳到最内循环的正文的末尾,并计算控制循环的布尔表达式。以下程序,ContinueDemo,通过一个字符串,计数字母“p”的发生。如果当前字符不是p,则continue语句跳过循环的其余部分,并转到下一个字符。如果是“p”,程序会增加字母数。
class ContinueDemo{
public static void main(String[] agrs){
String searchMe = "ppip is a very funny and stupid MV," + " do you aggree with me ?";
int maxValue = searchMe.length();
int numPs = 0 ;
for( int i = 0 ; i < maxValue ; i ++ ){
if(searchMe.charAt(i) != 'p'){
continue;
}else{
numPs ++;
}
}
System.out.println("Found " + numPs + " p's in the string.");
}
}
程序的输出结果:Found 4 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关键字之后。
return ++count;
返回值的数据类型必须与方法声明的返回值的类型相匹配。当一个方法声明为void时,请使用不返回值的返回形式。