控制流语句通过使用决策、循环和分支来分解执行流。Java 编程语言支持的决策语句(if、if-else、switch)、循环语句(for、while、do-while)和分支语句(break、continue、return)。
java中的if和if-else语句和C/C++中的没有什么区别。下面直接用例子来展示。
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
另一个例子如下所示:
class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
}
if (testscore >= 80) {
grade = 'B';
}
if (testscore >= 70) {
grade = 'C';
}
if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
输出结果如下所示:
Grade = D
在if-else复合语句中testscore值可以满足多个表达式:76 >= 70和76 >= 60。但是,一旦满足条件,就会执行相应的语句,(grade = ‘C’;)然后跳过剩余的条件。
switch语句可以有许多可能的执行路径。switch适用于byte、short、char和int原始数据类型,此外它还适用于枚举类型、 String类型。
switch语句的用法和C/C++的一致。每个break语句都会终止switch语句。如果没有break语句,匹配case标签之后的所有语句都按顺序执行,而不管后续case标签的表达式如何,直到break遇到语句。例如:
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
现在,我们给每一个case都加上break语句,再来看一下代码执行结果。
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");break;
case 2: futureMonths.add("February");break;
case 3: futureMonths.add("March");break;
case 4: futureMonths.add("April");break;
case 5: futureMonths.add("May");break;
case 6: futureMonths.add("June");break;
case 7: futureMonths.add("July");break;
case 8: futureMonths.add("August");break;
case 9: futureMonths.add("September");break;
case 10: futureMonths.add("October");break;
case 11: futureMonths.add("November");break;
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
因此,在C/C++中switch中保持的习惯,在Java中依旧需要沿用保持。
Java的while和do-while语句和C/C++的while和do-while语句保持一致。do-while至少执行一次循环。下面是两个例子。
// while
class WhileDemo {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}
// do-while
class DoWhileDemo {
public static void main(String[] args){
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count < 11);
}
}
在Java中for语句分为普通for语句和增强型for语句。
普通for语句和C/C++中的没有什么区别,一个例子如下所示:
class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
}
}
增强型for语句是为了迭代而设计的,类似于python的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) { // 每次循环从numbers取出一个值赋给item
System.out.println("Count is: " + item);
}
}
}
使用迭代器,生成器编程读取数据的时候,非常适合使用增强for循环。
基本上和C/C++的break语句一致,但是多了一个带标签的用法,如下所示:
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; // 终止带标签的语句,意味着终止了外层的for循环。
}
}
}
if (foundIt) {
System.out.println("Found " + searchfor + " at " + i + ", " + j);
} else {
System.out.println(searchfor + " not in the array");
}
}
}
基本上和C/C++的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; // 执行了这个带标签的continue之后,会直接跳过当前的for循环,进入下一次for循环
}
}
foundIt = true;
break test; // 执行带标签的break,会终止当前for循环。
}
System.out.println(foundIt ? "Found it" : "Didn't find it");
}
}
执行结果如下:
Found it
return语句从当前方法退出。return之后,控制流返回到调用方法的地方。return语句有两种形式:一种返回值,另一种不返回。
返回值的数据类型必须与方法声明的返回值的类型相匹配。声明方法时是void,则return不返回值。
参考资料:Java8官方教程