Java 基本语法---流程控制


0. 概述

三大流程控制语句:顺序、选择、循环。

选择结构:

  • if 结构,if - else结构;
  • 多重 if - else 语句 ;
  • 嵌套 if - else 语句 ;
  • switch 结构 ;

循环结构:

  • while 循环, do - while 循环, for循环 ;
  • Java增强for循环
  • 循环嵌套

1. 选择结构

1.1 if 结构

一个if语句包含一个布尔表达式和一条或多条执行语句;

布尔表达式值为true,执行if 语句;

格式:

if(布尔表达式)
{
    //布尔表达式值为true,执行语句;
}

1.2 if - else 结构

布尔表达式值为true,执行 if 语句;

布尔表达式值为false,执行 else 语句;

格式:

if(布尔表达式)
{
    //布尔表达式值为true,执行语句;
}
else
{
    //布尔表达式值为false,执行语句;
}

1.3 多重 if - else 结构

格式:

if(布尔表达式1)
{
    //布尔表达式1值为true,执行语句;
}
else if(布尔表达式2)
{
    //布尔表达式2值为true,执行语句;
}
else if(布尔表达式3)
{
    //布尔表达式值3为true,执行语句;
}
else
{
    //如果以上所有表达式的值都为false,则执行语句;
}

1.4 if 嵌套结构

格式:

if(布尔表达式1)
{
    //布尔表达式1值为true,执行语句
    if(布尔表达式2)
    {
        //布尔表达式2值为true,执行语句
    }
}

1.5 switch结构

格式:

switch(常量值/expression)
{
    case value1:
    //执行语句
    break;  //可选

    case value2:
    //执行语句
    break;  //可选

    ......
    default :
    //执行语句
}

switch语句有如下规则:

  • switch语句中的变量类型只能为byte、short、int或者char。
  • switch语句可以拥有多个case语句。每个case后面跟一个要比较的值和冒号。
  • case语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。
  • 当变量的值与case语句的值相等时,那么case语句之后的语句开始执行,直到break语句出现才会跳出switch语句。
  • 当遇到break语句时,switch语句终止。程序跳转到switch语句后面的语句执行。case语句不必须要包含break语句。如果没有break语句出现,程序会继续执行下一条case语句,直到出现break语句。
  • switch语句可以包含一个default分支,该分支必须是switch语句的最后一个分支。default在没有case语句的值和变量值相等的时候执行。default分支不需要break语句。

1.6 if 和 switch 区别

if 结构

  • 判断条件为布尔类型(布尔表达式)
  • 判断条件是一个范围

switch 结构

  • 判断条件为常量值

1.7 案例1--输出九九乘法表

public class MultiplicationTable {
    public static void main(String[] args) {
        for (int i = 1;  i <= 9; ++i)
        {
            for (int j = 1; j <= 9; j++)
            {
                if(j < i)
                {
                    //输出的空格由"%d * %d = %2d "决定
                    System.out.print("            ");
                }
                else
                {
                    System.out.printf("%d * %d = %2d ", i ,j , i*j);
                }
            }
            System.out.println();
        }
    }
}

Output:
1 * 1 =  1 1 * 2 =  2 1 * 3 =  3 1 * 4 =  4 1 * 5 =  5 1 * 6 =  6 1 * 7 =  7 1 * 8 =  8 1 * 9 =  9
           2 * 2 =  4 2 * 3 =  6 2 * 4 =  8 2 * 5 = 10 2 * 6 = 12 2 * 7 = 14 2 * 8 = 16 2 * 9 = 18
                      3 * 3 =  9 3 * 4 = 12 3 * 5 = 15 3 * 6 = 18 3 * 7 = 21 3 * 8 = 24 3 * 9 = 27
                                 4 * 4 = 16 4 * 5 = 20 4 * 6 = 24 4 * 7 = 28 4 * 8 = 32 4 * 9 = 36
                                            5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45
                                                       6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54
                                                                  7 * 7 = 49 7 * 8 = 56 7 * 9 = 63
                                                                             8 * 8 = 64 8 * 9 = 72
                                                                                        9 * 9 = 81

1.8 案例2--输出某一年的某一天有多少天

public class Days {
    public static void main(String[] args) {
        int days = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要确定的年份:");
        int year = sc.nextInt();
        System.out.println("请输入要确定的月份:");
        int month = sc.nextInt();

        switch (month)
        {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;
                break;
            case 2:
                if((year %400 == 0) || (year % 100 != 0 && year %4 == 0))
                {
                    days = 29;
                    break;
                }
                else
                {
                    days = 28;
                    break;
                }
            default:
                System.out.println("您输入的月份有误!");
                System.exit(0);
        }
        System.out.printf("%4d 年 %2d 月 共有 %2d 天\n",year,month,days);
    }
}

Output:
请输入要确定的年份:
2018
请输入要确定的月份:
02
2018 年  2 月 共有 28 天

请输入要确定的年份:
2008
请输入要确定的月份:
02
2008 年  2 月 共有 29 天

2.循环结构

2.1 while循环

只要布尔表达式值为true,就会执行循环内容。直到布尔表达式值为false,退出循环;

while(布尔表达式)
{
    //布尔表达式值为true,执行循环内容
}

2.2 do ... while 循环

只要布尔表达式值为true,就会执行循环内容。直到布尔表达式值为false,退出循环;和while类似,不同的是do...while语句至少会被执行一次;

do
{
    //布尔表达式值为true,执行循环内容
}while(布尔表达式)

2.3 for循环

for循环执行次数在执行前确定。

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

关于for的几点说明:

  • 最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
  • 然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。
  • 执行一次循环后,更新循环控制变量。
  • 再次检测布尔表达式。循环执行上面的过程。

2.4 Java增强for循环

声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

for(声明语句:表达式)
{
    //执行代码
}

2.5 案例1--循环输出26个英文小写字母,要求分两行输出

public class OutputLetters {
    public static void main(String[] args) {
        //循环输出26个英文小写字母,要求分两行输出
        char ch = 'a';
        int count = 0;//控制换行
        while(ch <= 'z')
        {
            System.out.printf(ch + " ");
            ch ++;
            count ++;

            if (count % 13 == 0)
            {
                System.out.println();
            }
        }

        System.out.println();
        ch = 'a';
        count = 0;
        for(count = 1, ch = 'a';ch <= 'z';ch ++,count ++)
        {
            System.out.printf(ch + " ");
            if (count % 13 == 0)
            {
                System.out.println();
            }
        }

    }
}

Output:
a b c d e f g h i j k l m
n o p q r s t u v w x y z 

a b c d e f g h i j k l m
n o p q r s t u v w x y z 

2.6 break

break语句

  • break语句可以结束当前循环的执行;
  • 执行完break语句,循环体中位于break语句后面的语句就不会被执行;
  • 在多重循环嵌套中,break语句相当于向外跳一层;

2.7 continue

continue语句:

  • continue语句只能用在循环里;
  • continue语句可以结束当前循环的执行,但是要继续下一次循环的执行;
public class OutputLettersDemo {
    public static void main(String[] args) {
        //循环输出26个英文小写字母,要求分两行输出
        //练习break,cotinue
        char ch = 'a';
        int count = 0;//控制换行
        while(ch <= 'z')
        {
            if(ch == 'x')
                break;
            System.out.printf(ch + " ");
            ch ++;
            count ++;

            if (count % 13 == 0)
            {
                System.out.println();
            }
        }

        System.out.println();
        ch = 'a';
        count = 0;
        for(count = 1, ch = 'a';ch <= 'z';ch ++,count ++)
        {
            if(ch == 'x')
                continue;
            System.out.printf(ch + " ");
            if (count % 13 == 0)
            {
                System.out.println();
            }

        }

    }
}

Output:
a b c d e f g h i j k l m
n o p q r s t u v w 

a b c d e f g h i j k l m
n o p q r s t u v w y z 

从上面的例子可以看出,break语句直接退出当层循环(到x直接退出,不再输出),而continue语句只是结束当前循环,并没有退出(只是没有输出x)。

最新文章

  1. Operation not allowed after ResultSet closed--操作mysql数据库
  2. 动画的使用&mdash;Drawable Animation
  3. 写css时要注意数字的浮动方向
  4. js const
  5. review过去的10年
  6. mysql初识之数据文件及其他文件
  7. 转:android 设计模式合集
  8. 如何使用PHP实现一个WebService
  9. Delphi XE5 android 图解为Android应用制作签名
  10. Ubuntu将新增磁盘挂载到home下
  11. 关于Ubuntu的ssh免密登录
  12. ArcGIS连带文字注记导出为CAD格式
  13. Supervisor管理进程
  14. curl模拟请求常用参数
  15. linux安全加固浅谈
  16. docker 下运行 postgresql 的命令
  17. linux一切皆文件之tty字符设备(深入理解sshd创建pty的过程) (五)
  18. mysql 删除某一个数据库下面的所有表,但不删除数据库
  19. EasyTouch5初步用法和其中的一个Bug
  20. [GDAL]在三维场景中显示DEM

热门文章

  1. MySQL中EXPLAIN解释命令 查看索引是否生效
  2. UML实践详细经典教程
  3. 在delphi中我用DBGrid选择多条记录,如何一次把选择的多条记录删掉
  4. Windows 64位环境的Java 服务配置
  5. oracle-表空间剩余空间大小占比查询
  6. Luogu4717 【模板】快速沃尔什变换(FWT)
  7. sql bak还原到新数据库
  8. 【Revit API】调用Revit内部命令PostableCommand
  9. 洛谷 P2051 [AHOI2009]中国象棋 解题报告
  10. vim使用入门设置