java流程控制语句

1.if语句

if(bool){}这种叫条件判断语句。如果是真的执行大括号里面的语句

         if(1 == 1){
            System.out.println("if笔记");
        }

可以和else一起使用,也可以单独使用

        if(1 == 1){
            System.out.println("if笔记");
        }else{
            System.out.println("else笔记");
        }

或者多个嵌套

     
    public static void main(String[] args) {
        int a = 1;
        if(a > 1){
            System.out.println("if笔记");
        }else if(a < 1){
            System.out.println("else if笔记");
        }else {
            System.out.println("else笔记");
        }
    }

2.switch语句

switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持
字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。
switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。
case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现
才会跳出 switch 语句。

        int a = 2;
        switch (a){
            case 1:
                System.out.println("我是case1");
            case 2:
                System.out.println("我是case2");
                System.out.println("我是case2");
            default:
                System.out.println("default");

这样只会跳到对应的case执行代码块,但是会顺序执行下去,比如case2以下的包括case3 4 default都会执行。
default是在没case匹配到的地方执行的代码,可以理解为if else中的else

【JDK7增加了字符串表达式】

3.break

break跳出当前代码块,switch case 执行时,一定会先进行匹配,匹配成功返回当前 case 的值,再根据是否有 break,判断
是否继续输出,或是跳出判断。

         int a = 2;
         switch (a){
            case 1:
                System.out.println("我是case1");
                break;
            case 2:
                System.out.println("我是case2");
                System.out.println("我是case2");
                break;
            default:
                System.out.println("default");
        }

4.while循环

while( 布尔表达式 ) { //循环内容 }

       while (1==1){
           System.out.println("循环了");
       }

break不止可以结束switch语句的代码块,while循环的代码块也可以结束,直接跳出当前循环。

       while (1==1){
           System.out.println("循环了");
           break;
       }

do { //代码语句 }while(布尔表达式);是先走do,这样至少会走一次循环体。

 do {
            System.out.println("循环了");
        }
        while (1!=1);

5.for循环

for(初始化; 布尔表达式; 更新) { //代码语句 }

itar可以快速生成for循环

        for (int i = 0; i < 10; i++) {
            System.out.println(i);
        }

同样的可以break结束循环

        for (int i = 0; i < 10; i++) {
            System.out.println(i);
            if(i>5){
                break;
            }
        }

iter生成快速for循环,也叫增强for循环

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

break 也可以跳出。

6.break & continue的区别

          for (int i = 0; i <10; i++) {
            if(i == 5){
                break;
            }
            System.out.println(i);
        }
          for (int i = 0; i <10; i++) {
            if(i == 5){
                continue;
            }
            System.out.println(i);
        }

break 上面介绍了,是跳出代码块,当等于5的时候直接跳出,就没有循环了。
continue是结束本次循环,i==5 这个没有打印,继续下一次循环。

7.goto这个保留字

这个c语言有,java暂时保留,可能会加上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值