Java笔记02控制流

7次阅读

共计 2853 个字符,预计需要花费 8 分钟才能阅读完成。

if-else

if (Boolean-expression) {// 要执行的语句}

这是 if 判断的基本形式,和 C 语言没有什么差别,括号中的布尔表达式(Boolean-expression)必须要生成一个 boolean 类型的结果,要么是 true 要么是 false,而花括号中的语句块只会在表达式为true 时执行。

和 C 语言一样,当 if 后面只有一个分号 ; 结尾的语句时,可以省略花括号,但是为了以后修改程序方便,建议任何时候都保持有花括号的统一格式。

public class IfElse {public static void main(String[] args) {
        int n = 95;
        if (n >= 60) {System.out.println("及格");
        } else {System.out.println("挂科");
        }
    }
}
public class IfElse {public static void main(String[] args) {
        int n = 95;
        if (n >= 90) {System.out.println("优秀");
        } else if (n >= 60) {System.out.println("合格");
        } else {System.out.println("挂科");
        }
    }
}

上面两个程序是 if-else 结构,和 C 语言一样,else if只是 else 后面跟了一个 if 语句,不像 Python 中的 elif 是个专门的关键字。

判断字符串是否相等

String字符串是引用类型,在上一篇中讲到了 引用类型 的概念,引用类型的变量名实质上是指向对象在堆内存中的地址。考虑下面这个例子:

public class Main {public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "hello";
        System.out.println(s1);
        System.out.println(s2);
        if (s1 == s2) {System.out.println("s1 == s2");
        } else {System.out.println("s1 != s2");
        }
    }
}
public class Main {public static void main(String[] args) {
        String s1 = "hello";
        String s2 = "HELLO".toLowerCase();
        System.out.println(s1);
        System.out.println(s2);
        if (s1 == s2) {System.out.println("s1 == s2");
        } else {System.out.println("s1 != s2");
        }
    }
}

运行上面两个程序,你会发现,结果居然并不一样!为什么第一个程序显示两个变量相等,而第二个结果却是不相等呢?
对于引用类型来说,既然变量保存的是对象的地址,那么如果是同一个内存空间中的对象那自然他们的值是相等的(当然了,在一个空间里根本就是一个对象嘛)。
字符串类型有一些些特殊,第一个程序里给两个变量都赋值 hello,为了节省空间实质上只创建了一个对象,但不要把希望寄于所有字符串都这样,就像第二个例子中,虽然两个字符串最后结果都是hello 这个值,却是放在不同内存空间里的两个字符串对象。
为了精确判断两个字符串的值是否相等,应该使用 equals() 方法。

String s1 = "hello";
String s2 = "Hello";
if (s1.equals(s2)) {System.out.println("相等");
} else {System.out.println("不相等");
}

switch

public class Main {public static void main(String[] args) {
        int option = 2;
        switch (option) {
        case 1:
            System.out.println("Selected 1");
            break;
        case 2:
        case 3:
            System.out.println("Selected 2, 3");
            break;
        default:
            System.out.println("Not selected");
            break;
        }
    }
}

switch 可以用 if 来代替。要注意每个 case 后面有冒号,而不需要花括号,每个 case 需要有 break,否则将继续将后面的case 内容执行,当然有些情况就是不同的 case 要执行的操作相同,如上面例子中的 2 和 3。default是所有 case 都不匹配时执行的。
switch还可以选择字符串,比较的是字符串的值。

Python 中并没有 switch 语句,设计者认为 switch 会破坏 Python 的灵活性。

while 循环

while (Boolean-expression) {// 要执行的语句}

和 C 语言没有区别,只要满足条件,就会一直执行 while 中的语句,同时 Java 中也有 do-while 循环,do-while相比 while 的区别是保证循环中的语句至少要被执行一次。

do {// 要执行的语句} while (Boolean-expression)

for 循环与 for-in 循环

for (初始条件; 循环检测条件; 循环后更新计数器) {// 执行语句}

和 C 语言的 for 循环没有什么区别,但在 Java 中,还实现了一种更加便捷的 for-in 循环:

int[] s = { 1, 2, 3, 4, 5, 6};
for (int i = 0; i < s.length, i++) {System.out.println(s[i]);
}

这样遍历一个数组比较麻烦,用 for-in 的方法来写是这样:

int[] s = { 1, 2, 3, 4, 5, 6};
for (int n: s) {System.out.println(n);
}

for-in不使用索引,直接去遍历一个序列的每个元素,写起来更为简洁。

吐槽一下,相比起 Python 中 for i in arr 和 C# 中的 foreach (类型 变量名 in 序列) 的写法比起来,这种 for (类型 变量名: 序列) 的奇葩写法让人一眼根本看不出来是在做什么啊。

顺带一提:

for (int i = 0; i < 10 ; i++) {}

这里这个 变量 i 的作用域仅仅在 for 循环体内部,出了循环就无法使用 i 了,如果希望循环结束还可以访问 i,可以这样写:

int i;
for (i = 0; i < 10; i++) {}

break 和 continue

public class Main {public static void main(String[] args) {
        int i;
        for (i = 0; i < 100; i++) {if (i == 97) {break; // 跳出循环}
            if (i % 2 == 0) {continue; // 下一次循环}
            System.out.println("在没有 continue 时会执行" + i);
        }
        System.out.println("跳出循环后执行" + i);
    }
}

break直接跳出 当前 循环,强调当前是因为有时候会使用嵌套循环,一个 break 只会跳出所在的那一层循环。continue则是直接本次循环结束,进入下一次循环。

正文完
 0