Java™ 教程(控制流语句)

项充
• 阅读 2675

控制流语句

源文件中的语句通常按照它们出现的顺序从上到下执行,但是,控制流语句通过使用决策、循环和分支来分解执行流程,使你的程序能够有条件地执行特定的代码块,本节描述Java编程语言支持的决策语句(if-thenif-then-elseswitch),循环语句(forwhiledo-while)以及分支语句(breakcontinuereturn)。

if-then和if-then-else语句

if-then语句

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语句的末尾。

此外,只要“then”子句只包含一个语句,开括号和结束括号是可选的:

void applyBrakes() {
    // same as above, but without braces 
    if (isMoving)
        currentSpeed--;
}

决定何时省略括号是个人品味的问题,省略它们会使代码变得更脆弱,如果稍后将第二个语句添加到“then”子句中,则常见的错误是忘记添加新所需的花括号,编译器无法捕获这种错误,你会得到错误的结果。

if-then-else语句

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 >= 7076 >= 60,但是,一旦满足条件,就会执行适当的语句(grade ='C';),并且不评估其余条件。

switch语句

if-thenif-then-else语句不同,switch语句可以有许多可能的执行路径,switch使用byteshortcharint原始数据类型,它还适用于枚举类型(在枚举类型中讨论),String类,以及一些包含某些基本类型的特殊类:CharacterByteShortInteger(在NumberString中讨论)。

以下代码示例SwitchDemo声明了一个名为monthint,其值表示月份,代码使用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

在switch语句中使用String

在Java SE 7及更高版本中,你可以在switch语句的表达式中使用String对象,以下代码示例StringSwitchDemo根据名为monthString的值显示月份的编号:

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示例无论何种情况都接受任何monthmonth将转换为小写(使用toLowerCase方法),并且与case标签关联的所有字符串均为小写。

此示例检查switch语句中的表达式是否为null,确保任何switch语句中的表达式不为null,以防止抛出NullPointerException

while和do-while语句

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-whilewhile之间的区别在于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语句的变量,则最好在初始化表达式中声明该变量。名称ijk通常用于控制循环,在初始化表达式中声明它们会限制它们的生命周期并减少错误。

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语句

break语句有两种形式:标记和未标记,你在前面的switch语句讨论中看到了未标记的形式,你还可以使用未标记的break来终止forwhiledo-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语句终止最内层的switchforwhiledo-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语句

continue语句跳过forwhiledo-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关键字之后。

return ++count;

返回值的数据类型必须与方法声明的返回值的类型匹配,当方法声明为void时,请使用不返回值的return形式。

return;

类和对象课程将涵盖你需要了解的有关编写方法的所有内容。

控制流程语句总结

if-then语句是所有控制流语句中最基本的语句,它告诉程序只有在特定测试评估为true时才执行某段代码。if-then-else语句在“if”子句求值为false时提供辅助执行路径,与if-thenif-then-else不同,switch语句允许任意数量的可能执行路径。whiledo-while语句在特定条件为true时不断执行语句块,do-whilewhile之间的区别在于do-while在循环的底部而不是顶部计算它的表达式,因此,do块中的语句总是至少执行一次。for语句提供了一种迭代一系列值的简洁方法,它有两种形式,其中一种用于循环集合和数组。


上一篇;表达式、语句和块
下一篇:类
点赞
收藏
评论区
推荐文章
Karen110 Karen110
3年前
一篇文章带你了解JavaScript While 循环
循环可以执行一个代码块,只要指定条件为真,循环就可以执行代码块。一、While循环while只要指定条件的值为真,循环就会执行指定语句。while循环的语法while(condition)statement//只要条件为真,就执行代码例:项目JavaScriptwhile语句只要n小于5,就遍历一段代码:varn0;while(n<
CuterCorley CuterCorley
4年前
C语言入门系列之5.循环控制结构程序
@一、概述循环结构是程序中一种很重要的结构。其特点是:在给定条件成立时,反复执行某程序段,直到条件不成立为止。给定的条件称为循环条件,反复执行的程序段称为循环体。C语言提供了多种循环语句,可以组成各种不同形式的循环结构:goto语句和if语句构成循环;while语句;dowhile语句;for语句。二、got
Gwendolyn62 Gwendolyn62
4年前
MySQL的语句执行顺序
今天遇到一个问题就是mysql中insertinto和update以及delete语句中能使用as别名吗?目前还在查看,但是在查阅资料时发现了一些有益的知识,给大家分享一下,就是关于sql以及MySQL语句执行顺序:sql和mysql执行顺序,发现内部机制是一样的。最大区别是在别名的引用上。 一、sql执行顺序 
CuterCorley CuterCorley
4年前
C语言入门系列之3.顺序程序设计和输入输出
从程序流程的角度来看,程序可以分为三种基本结构,即顺序结构、分支结构、循环结构,这三种基本结构可以组成各种复杂程序,C语言提供了多种语句来实现这些程序结构。同时C语言提供的输入输出的函数为IO提供了方便的工具。一、C语句介绍1.C基本语句C程序的执行部分是由语句组成的,程序的功能也是由执行语句实现的。C语句可分为以下五类:表达式语句表达式语句:
Wesley13 Wesley13
3年前
MySQL EXPLAIN 详解
一.介绍  EXPLAIN命令用于SQL语句的查询执行计划。这条命令的输出结果能够让我们了解MySQL优化器是如何执行SQL语句的。这条命令并没有提供任何调整建议,但它能够提供重要的信息帮助你做出调优决策。先解析一条sql语句,你可以看出现什么内容1EXPLAIN SELECT
Wesley13 Wesley13
3年前
Java入门(五):控制流程
在Java中,使用条件语句和循环结构确定控制流程,在本文中,主要包括块作用域、条件语句、循环结构、中断循环这四部分。一、块作用域块,也叫复合语句,是指由一对大括号括起来的若干条Java语句。块决定了变量的作用域。一个块可以嵌套多个块。二、条件语句如果判断超过三层,建议拆分开来写,这样更加清晰。packagejav
Wesley13 Wesley13
3年前
C语言二次系统学习3(分支循环、函数)
本次学习,主要针对语句和函数两部分进行学习。分支语句(if,switch)与循环语句(while,for,dowhile)switch语句中,在每个选择之后如果不进行break跳出,会执行下一个case,而且应注意default语句的使用,一般会放在{}代码块的后方dowhile语句中,会首先执行一次循环,再进行判定,所以使用相对较少。whi
Wesley13 Wesley13
3年前
Mysql存储过程
SQL语句需要先编译然后执行,而存储过程(StoredProcedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给定参数(如果该存储过程带有参数)来调用执行它。存储过程是可编程的函数,在数据库中创建并保存,可以由SQL语句和控制结构组成。当想要在不同的应用程序或平台上执行相同的函数,或者封装特定功能时
小万哥 小万哥
1年前
C 语言教程:条件和 if...else 语句
C语言中的条件和if...else语句您已经学习过C语言支持数学中的常见逻辑条件:小于:a<b小于或等于:ab大于或等于:ab等于:ab不等于:a!b您可以使用这些条件来根据不同的决策执行不同的操作。C语言具有以下条件语句:使用if来指定要执行的代码块,如
小万哥 小万哥
1年前
C 语言中的 switch 语句和 while 循环详解
C语言中的switch语句替代多重if..else语句,可以使用switch语句。switch语句用于选择多个代码块中的一个来执行cswitch(表达式)casex://代码块break;casey://代码块break;default://代码块工作原理
小万哥 小万哥
1年前
深入解析 C 语言中的 for 循环、break 和 continue
C语言中的for循环当您确切地知道要循环执行代码块的次数时,可以使用for循环而不是while循环cfor(语句1;语句2;语句3)//要执行的代码块语句1在执行代码块之前执行(一次)。语句2定义执行代码块的条件。语句3在执行代码块后执行(每次)。下面的示