#JAVA# 学习笔记 Branching Statements 分支语句

inner break

An unlabeled break statement terminates the innermost switch, for, while, or do-while statement, but a labeled break terminates an outer statement. The following program, BreakWithLabelDemo, is similar to the previous program, but uses nested for loops to search for a value in a two-dimensional array. When the value is found, a labeled breakterminates the outer for loop (labeled "search"):

查找数组中某个数的序号(序号从0开始排)

  1. 一维数组
//input 
class breakDemo{

    public static void main(String[] args) {
        int[] arrayInt = { 32, 87, 3, 589,
                12, 1076, 2000,
                8, 622, 127 };

        int searchNumber = 1270;
        boolean numberFound = false;
        int searchIndex;

        //search number's index and return
        for (searchIndex = 0; searchIndex < arrayInt.length ;searchIndex++){
            if(arrayInt[searchIndex] == searchNumber){
                numberFound = true;
                break;
            }
        }

        //print the searchNumber and its index
        if (numberFound) {
            System.out.println("Searched number " + searchNumber + " at index " + searchIndex);
        }else{
            System.out.println("the number " +searchNumber + " is not found");
        }
    }
}

//output 
the number 1270 is not found
  1. 二维数组,这里用循环。不过一般MATLAB和python都直接对数组进行操作,java应该也有类似的库的。

注意加label,break;意思是跳出那一整个label的循环,没加label就会循环到结束

break with label

2.1. 正确例子,label把双层循环包起来,break label;就是跳出一整个循环

//input 
class breakDemoTwoDimention {
    public static void main(String[] args) {
        int[][] arrayInts = {
                {32, 87, 3, 589},
                {12, 1076, 2000, 8 },
                {622, 127, 77, 955}
        };

        int searchNumber = 12;

        int i,j=0;
        boolean numberFound = false;

        search:
            for (i=0; i<arrayInts.length;i++){
                for(j=0;j<arrayInts[i].length;j++){
                    if(arrayInts[i][j] == searchNumber){
                        numberFound = true;
                        break search;  //break this label "search"
                    }
                }
            }
        System.out.println(i);
        System.out.println(j);
        System.out.println(numberFound);
    }
}

//correct output 
1
0
true

break without label

2.2. 错误例子,break只能跳出if圈,完全不能影响两个for循环。所以i,j是整个二维数组的行和列。

//input 

class breakDemoTwoDimentionWrong {
    public static void main(String[] args) {
        int[][] arrayInts = {
                {32, 87, 3, 589},
                {12, 1076, 2000, 8 },
                {622, 127, 77, 955}
        };

        int searchNumber = 12;

        int i,j=0;
        boolean numberFound = false;

        for (i=0; i<arrayInts.length;i++){
            for(j=0;j<arrayInts[i].length;j++){
                if(arrayInts[i][j] == searchNumber){
                    numberFound = true;
                    break;
                }
            }
        }
        System.out.println(i);
        System.out.println(j);
        System.out.println(numberFound);
    }
}

//incorrect output 
3
4
true

The continue Statement

全称是继续下一个循环,continue 后,忽略以下操作,然后进入下一个for循环。

//input
class ContinueDemo {
    public static void main(String[] args) {
        String searchSentence = "peter piper picked a peck of pickled peppers";
        char searchLetter = 'p';
        int letterCounts = 0;

        for (int searchIndex = 0;searchIndex<searchSentence.length();searchIndex++ ){
            if (searchSentence.charAt(searchIndex) != searchLetter)
                continue;
            letterCounts++;
        }
        System.out.println("the sentence contains " + letterCounts + " " + searchLetter);
    }
}

//output 
the sentence contains 9 p

continue and break mixed up

查找字符串中是否包含一个小的字符串。

class ContinueWithLabelDemo {

    public static void main(String[] args) {
        String searchSent = "Look for a substring in me";
        String searchSub = "sub";
        boolean foundString = false;

        int searchStep = searchSent.length() - searchSub.length();

        test:
            for (int searchIndex = 0; searchIndex<searchStep;searchIndex++){

                int subSteps = searchSub.length();
                int senIndex = searchIndex;
                int subIndex = 0;
                while (subSteps-- !=0){
                    if ((searchSent.charAt(senIndex++)) != (searchSub.charAt(subIndex++)))
                        continue test;
                }
                foundString = true;
                break test;
                }
        System.out.println(foundString);
            }
    }

2018.6.22

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,438评论 0 10
  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 12,260评论 3 20
  • 没什么好抱怨的,今天的每一步,都是在为之前的每一次选择买单,这也叫担当;没什么好抱怨的,今天的每一步,都是在为`今...
    弘毅A阅读 236评论 0 1
  • 写在前面的话 程序包括代码、数据、文档。在当今,数据对我们来说,尤为重要。或存数据库或写入文件。这样对于File类...
    冯文议阅读 643评论 0 0