Java流程控制

2018-06-28  本文已影响0人  暴走的朝天椒

一些感想

做了很久的Java Web开发,回顾过去,除了代码,没有什么留下,有些遗憾,需要找个地儿来写写遗失的部分。
如果说架构是结构,那么基础就是地基;如果地基都没打好,那么架构都是花架子。
我本以为写基础很简单,主要是大部分都写烂了,然而在写的过程中,并没有想象中那么容易,这就是所谓的“知易行难”。
比如:写变量,会涉及到JVM的知识;写集合,会有数据结构的知识,同时要了解 各个类之间的不同、各个类得适用场景;写框架知识你得知道基础知识在框架之中如何使用,框架包含了哪些设计模式、框架之间的比较、在框架中是如何使用多线程解决问题,当这些知识纠缠在一起得时候,想写明白,还有许多待学习的地方,既是挑战也是学习机会。

前言

*.java源文件中的语句通常按照它们出现的顺序从上到下执行。而在现实生活中,我们需要做一些判断、决策 等控制,因此在面向对象的Java语言中,就出现了关于判断、循环 和 分支来控制执行流程,从而执行特定的代码块。

Java版本

JDK8

概况

1、 判断语句(if、if-else、switch)
2、 循环语句(for、while、do-while)
3、 分支语句(break、continue、return)

判断语句(if、if-else、switch)

if判断
只有当if判断为true的时候,才会执行if代码块中的语句。

public void apply(){
    if(p2==1){ // 当它为true时
        System.out.println("当为true时,执行该代码块");
    }
}

if-else判断
当if判断为true时,执行if代码块中的语句;否则,执行else代码块中的语句。

public void apply(){
    if(p2==1){ // 当它为true时
        System.out.println("当if判断为true时,执行该代码块语句");
    } else { // 当p2==1为false时
        System.out.println("当if判断为false时,执行该代码块语句");
    }
}

switch语句
不像if、if-else语句,switch语句可能有多条执行路径。一个switch语句既可以用char、byte、short、int基本数据类型,也可以用Character、Byte、Short、Integer封装的基本类型,以及 枚举 和 String类型。

int month = 8;
String monthString;
public void apply(){
    switch (month){ // 可以是byte、short、int、char基本类型;也可以是Byte、Short、Integer、Character包装类;以及枚举或String类型
        case 1:  monthString = "January";
            break;
        case 2:  monthString = "February";
            break;
        default: monthString = "Invalid month";
            break;
    }
}

循环语句(for、while、do-while)

for语句
for语句是用重复循环的方式直到满足特定的条件。
for语句的一般形式表示如下:

for(initialization ; termination ;
      increment){
     statement(s)
}

1、 initialization表达式初始化,它会在循环开始时执行一次。
2、 termination表达式为flase时,循环终止。
3、 increment每次迭代之后被调用。

public void apply(){
    for (int i=1 ; i<11 ; i++) {
        System.out.println("Count is:"+ i);
    }
}

增量型for语句

public void apply(){
    int [] numbers =
            {1,2,3,4,5,6,7,8,9,10};
    for(int item:numbers){
        System.out.println("Count is:"+item);
    }
}

while和do-while语句
While语句的语法表达式如下:

while (expression) {
     statement(s)
}

当while中的expression表达式为true时,才执行statement代码块。

public static void main(String[] args){
    int count = 1;
    while (count < 11) {
        System.out.println("Count is: " + count);
        count++;
    }
}

do-while语句的语法表达式如下:

do {
     statement(s)
} while (expression);

do块中的语句至少执行一次。

public static void main(String[] args){
    int count = 1;
    do {
        System.out.println("Count is: " + count);
        count++;
    } while (count < 11);
}

分支语句(break、continue、return)

break语句
break语句有两种形式:标记和未标记。可以用未标记的break语句终止最内层for、while、do-while循环;但标记的break终止一个标记所在循环。

未标记break语句

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; // 终止最内层for循环
        }
    }

    if (foundIt) {
        System.out.println("Found " + searchfor + " at index " + i);
    } else {
        System.out.println(searchfor + " not in the array");
    }
}

标记break终止标记所在循环

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; // 终止标签search所在的循环
            }
        }
    }

    if (foundIt) {
        System.out.println("Found " + searchfor + " at " + i + ", " + j);
    } else {
        System.out.println(searchfor + " not in the array");
    }
}

continue语句
continue是跳过当前迭代的for、while、do-while循环。未标记的continue跳到最内层循环体的末尾,并评估控制循环的表达式。

例子:计算字母“p”的出现次数。如果当前字符不是“p”,则跳过循环,继续下一个字符的判断;否则计算p的次数。

package com.sample;

public 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会达到标签的位置,并重新进入紧接在那个标签后面的循环。
例子:用嵌套循环搜索另一个字符串中的子字符串。需要两个嵌套循环:一个循环遍历子串,一个迭代要搜索的字符串。

package com.sample;

public 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声明有两种形式:一种返回值,另一种不返回。

上一篇下一篇

猜你喜欢

热点阅读