从零开始学JAVA(03):流程控制语句

目录

一、if-else

二、for语句

三、Break语句

四、continue语句

五、代码块和变量的作用域

六、while语句

七、do-while语句 

八、switch语句 


  • 代码块的执行是顺序执行
  • 只要程序运行过程中不出错,就会一行一行的向下顺序执行
  • 大括号括起来的就是代码块
  • 代码块也叫体,比如:for循环体,main方法体

一、if-else

if (boolean 值){

   if的语句块;

} else {

   else的语句块;

}

public class IfElse {
    public static void main(String[] args) {
        int num = 3;
        boolean flag = true;
        if (flag) {
            num += 2;
            System.out.println(num);
        } else {
            System.out.println(num);
        }

    }
}
// 如果语句中只有一行,那么可以省略大括号
public class IfElse {
    public static void main(String[] args) {
        int num = 3;
        boolean flag = true;
        if (flag)
            System.out.println(num + 2);
        else
            System.out.println(num);
    }
}

二、for语句

for (初始语句; 循环体条件表达式; 循环体后语句) {

     for循环体

}

public class ForStatement {
    public static void main(String[] args) {
        char charVar = '梁';
        int intVar = charVar;
        for (int i = 0; i < 10; i++) {
            System.out.println((charVar + i) + "\t" + (char) (charVar + i));
        }
    }
}

三、Break语句

  • break语句可以结束任何循环
public class ForStatement {
    public static void main(String[] args) {
        char charVar = '梁';
        int intVar = charVar;
        for (int i = 0; i < 10; i++) {
            if (i == 5) {
                break;
            }
            System.out.println((charVar + i) + "\t" + (char) (charVar + i));
        }
    }
}

四、continue语句

  • continue语句可以结束档次循环的执行,开始下一次循环体的执行
public class ForStatement {
    public static void main(String[] args) {
        char charVar = '梁';
        int intVar = charVar;
        for (int i = 0; i < 10; i++) {
            if (i == 5) {
                continue;
            }
            System.out.println((intVar + i) + "\t" + (char) (intVar + i));
        }
    }
}

五、代码块和变量的作用域

  • 代码块内部的代码可以访问代码块外部的变量,反之则不行
  • 一个代码块结束的时候,内部的变量也就随之消失了
  • 内层命名空间不可以重复定义外层代码块的变量,但是可以使用外层代码块的变量
  • 同一个命名空间中的变量不可以重名
  • 为了避免变量名冲突,所以必须要有命名空间
public class Example {
    public static void main(String[] args) {
        for (int i = 1; i < 10; i++) {
            String line = "";
            for (int j = 1; j < 10; j++) {
                if (j > i) {
                    break;
                }
                line += i + "*" + j + "=" + (i * j) + "\t";
            }
            System.out.println(line);
        }
    }
}

六、while语句

  • 条件表达式的结果是一个Boolean值,如果为true,则执行循环体,如果为false,则循环结束
  • while循环体是一个代码块。所以while循环也是可以嵌套别的语句的,包括while语句,for语句,if-else语句等

while (条件表达式) {

        while循环体

}

public class Example {
    public static void main(String[] args) {
        int n = 10;
        int dividend = 100;
        int divisor = 89;

        int found = 0;

        while (found < n) {
            if (dividend % divisor == 0) {
                found++;
                System.out.println(dividend + "可以整除" + divisor + "商为" + (dividend / divisor));
            }
            dividend++;
        }
    }
}

七、do-while语句 

  • do-while语句的循环体至少可以执行一次

do {

        while循环体

} while (条件表达式);

public class Example {
    public static void main(String[] args) {
        int n = 2;
        do {
            System.out.println("执行");
        } while (n < 2);
    }
}

八、switch语句 

  • switch语句中用于比较的值,必须是int
  • switch语句适用于有固定多个目标值匹配,然后执行不同逻辑的情况
  • 必须使用break语句显式的结束一个case语句,否则switch语句会从第一个match的case语句开始执行直到遇到break语句或者switch语句结束
  • default语句是可选的,如果所有的case语句都没有匹配上,才会执行default中的代码

switch (用于比较的int的值) {

        case 目标值1, 对应一个if else{xxx};

                匹配后可以执行的语句

        case 目标值2, 不可以和别的case字句重复;

                匹配后可以执行的语句

        default (对应最后的else,可选)

                default语句

}

public class Example {
    public static void main(String[] args) {
        switch (10) {
            case 1:
                System.out.println("1");
                break;
            case 10:
                System.out.println("10");
                break;
            case 2:
                System.out.println("2");
                break;
            default:
                System.out.println("没找到对应的");
        }
    }
}

猜你喜欢

转载自blog.csdn.net/m0_47135993/article/details/128066280