java 顺序、分支与循环

顺序结构

1、顺序结构`

    public static void main(String[] args) {
        System.out.println("a");
        System.out.println("b");
        System.out.println("c");

        System.out.println("====================");
        
        System.out.println("a");
        System.out.println("c");
        System.out.println("b");
    }

打印顺序依次是a b c 和 a c b。
顺序结构就是按照书写的代码一行一行的执行。

分支结构

1、if语句

    public static void main12(String[] args) {
        int x = 1;
        if (x > 1 ){
            System.out.println(x > 1);
        }else {
            System.out.println(x <= 1);
        }
    }

if语句的基本语法:

        if (布尔表达式1){
            //满足布尔表达式1时执行
        }else if (布尔表达式2){
            //满足布尔表达式2时执行
        }else if (布尔表达式3){
            //满足布尔表达式3时执行
        }else {
            //不满足布尔表达式1、2、3时执行
        }

注意:

    public static void main(String[] args) {
        int a = 100;
        if (a == 100){
            System.out.println("a == 100");
        }
        System.out.println("a != 100");
    }

至少我在使用中经常犯的错误,如果没有else,那么if语句结束后还是会执行。如上的代码中,运行后所打印的结果位a == 100; a != 100。记得判断是否要加else。

2、switch语句

   public static void main(String[] args) {
        int x = 1;
        switch (x){
            case 1 :{
                System.out.println("今天周一");
                break;
            }
            case 2 :{
                System.out.println("今天周二");
                break;
            }
            case 3 :{
                System.out.println("今天周三");
                break;
            }
            default:{
                System.out.println("今天可能周四");
                break;
            }
        }
    }

执行结果:今天周一

switch语句基本语法

switch(整数|枚举|字符|字符串){
 case 内容1 : {
 内容满足时执行语句;
 break;
 }
 case 内容2 : {
 内容满足时执行语句;
 break;
 }
 ...
 default:{
 内容都不满足时执行语句;
 break;
 } 
}

整数中不包括double和float。

    public static void main(String[] args) {
        int x = 1;
        switch ( x){
            case 1 :{
                System.out.println("今天周一");
            }
            case 2 :{
                System.out.println("今天周二");
                break;
            }
            case 3 :{
                System.out.println("今天周三");
                break;
            }
            default:{
                System.out.println("今天可能周四");
                break;
            }
        }
    }

执行结果:今天周一;今天周二
记得每条case分钟要写break,不然程序就会执行到遇到break或者程序执行完才结束。

if和switch的区别:
switch(只能是整数|枚举|字符|字符串),当我们想要判断复杂得条件时,比如:

    public static void main(String[] args) {
        int x = 22;
        switch (x > 20) {//编译错误
            case 1: {
                System.out.println("x > 20");
            }
        }
    }

这个时候就只能使用if条件句了。

循环结构

1、while循环

    public static void main(String[] args) {
        while(循环条件){
            循环语句;
        }
    }

2、for循环

    public static void main(String[] args) {
        for(表达式1;布尔表达式2;表达式3){
            循环体;
        }
    }

3、do while循环

    public static void main(String[] args) {
        do{
            循环语句;
        }while(循环条件);
    }
    public static void main(String[] args) {
        int x = 3;
        do {
            System.out.println(x > 3);
            x--;
        } while (x > 3) ;
    }

执行结果:x > 3
if 和 while没有本质上是没有区别的,都是先判断条件,条件满足就执行循环体。do while是先执行一次循环语句,再判断循环条件。

为了方便总结,我重新下了另外一篇博客,关于循环和条件结构的经典习题:
循环和分支结构中常见习题

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值