|
JAVA程序员必读:基础篇(3)语言基础
|
|
编译:ZSC/太平洋网络学院
|
|
|
语言基础
3.4.6 分支语句
JAVA编程语言支持下面的三种分支结构:
break语句
continue语句
return语句
下面逐个介绍:
(1)break语句
break语句有两种形式:未标志的和标志的。你在前面就已经看过了未标志的break语句。一个未标志的break语句终止swtich语句,控制流程马上转到switch语句下方的语句。下面的例程BreakDemo,它包含了一个for循环查找数组中特定的数值:
public 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 = 0;
boolean foundIt = false;
for ( ; 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");
}
}
}
当数值被找到的时候,这个break语句终止了for循环。控制流程就转到for语句的下面的语句继续执行。这个程序的输出为:
Found 12 at index 4
[上一页] [下一页]
|