Branching 语句
中断声明
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
循环(标记为“搜索”):
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
循环的当前迭代。未标记的形式跳到最内层循环主体的末尾,并评估控制循环的boolean
表达式。下面的程序ContinueDemo逐步通过String
,计算字母“ p”的出现。如果当前字符不是 p,则continue
语句将跳过循环的其余部分,并 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.
若要更清楚地看到此效果,请try删除continue
语句并重新编译。当您再次运行该程序时,计数将是错误的,表示它找到的是 35 p 而不是 9.
带有continue
标记的语句跳过标有给定标签的外部循环的当前迭代。下面的示例程序ContinueWithLabelDemo
使用嵌套循环在另一个字符串 中搜索子字符串。需要两个嵌套循环:一个循环访问子字符串,另一个循环访问要搜索的字符串。以下程序ContinueWithLabelDemo使用标记形式的 Continuecontinue 跳过外循环中的迭代。
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;
类和对象类将涵盖您需要有关编写方法的所有知识。