• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
xiaoyaovo
博客园    首页    新随笔    联系   管理    订阅  订阅
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是先执行一次循环语句,再判断循环条件。

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

posted on 2021-05-03 13:23  豆本豆红枣豆奶  阅读(21)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3