Java笔记02:控制流

if-else

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

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

和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()方法。code

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 (类型 变量名: 序列)的奇葩写法让人一眼根本看不出来是在作什么啊。

顺带一提:it

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则是直接本次循环结束,进入下一次循环。

相关文章
相关标签/搜索