current position:Home>"Java" logic control

"Java" logic control

2022-09-23 10:17:59Desperately Azi

当你停下脚步

Don't forget that others are still running


目录

1.Program control statement classification 

2.顺序结构 

2.选择结构

2.1 if 选择语句

2.2 switch 选择语句

3.循环结构 

3.1 while 循环语句 

3.2 break

3.3 continue 

3.4 for 循环

3.5 do...while 循环 

4.输入输出 

4.2 输入 


1.Program control statement classification 

JavaThere are three structures in total,分别是 顺序结构、选择结构、循环结构

  1. 顺序结构:executed in sequence
  2. 选择结构:because of different choices,The corresponding results are also different
  3. 循环结构:Doing something repeatedly,The loop will not be terminated until the condition is not met

2.顺序结构 

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a);
        System.out.println(b);
    }
}

Execution line by line in the order of the code is a sequential structure

If the order of the code has changed,Then the order of execution will also change accordingly

2.选择结构

The selection structure is also called the branch structure,Because the selection is different, the code in the final execution of the selection body is also different.Suppose Mei goes to buy a lottery ticket,If the lottery number she buys is the same as the winning lottery number, Mei wins,Otherwise, Xiaomei will not win the lottery

There are two types of statements in the selection structure. One is if 语句,还有一个是 switch 语句 

2.1 if 选择语句

①单独的 if 语句

public class Test {
    public static void main(String[] args) {
        int a = 10;
        if(a == 10) {
            System.out.println("中奖");
        }
        System.out.println(a);
    }
}

打印结果: 

如果 if The following conditional judgment statement为真,则执行 if 语句体,That is, print winning.如果为假,则不执行 if 语句体.

注:不管执不执行 if 语句体,if Statements other than the statement body are executed.That is, whether it is implemented or not if 语句体,both print a 的值

② if...else... 语句

public class Test {
    public static void main(String[] args) {
        int a = 10;
        if(a == 10) {
            System.out.println("中奖");
        } else {
            System.out.println("没中奖");
        }
    }
}

执行结果: 

如果 if The following conditional judgment statement为真,则执行 if 语句体,否则执行 else 语句体

注:if 语句体 和 else Statement bodies are not executed at all,只会执行其中一个 

③ if...else if...else Multi-layer judgment selection statement

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        if(a == 10) {
            System.out.println("a中奖");
        } else if(b == 20) {
            System.out.println("b中奖");
        } else {
            System.out.println("没中奖");
        }
    }
}

 执行结果:

如果 if The following conditional judgment statement为真,则执行 if 语句体 .否则判断 else if The following condition determines whether the statement is为真,如果为真则执行 else if 语句体,否则执行 else 语句

注:The multi-level judgment selection statement will only execute one of the statement bodies

if Select Statement Considerations

注意1:分号问题

This code will still print a中奖,But this is not printed in the body of the select statement,造成的原因就是 if The semicolon after the judgment statement.if The following conditional statement is true,则执行 if 后面的一条语句.All statements are {} The curly braces are called the statement body,被视为一条语句.A semicolon is by default the end marker of a statement,当 if The following conditional statement is true when ready to execute if the following statement,Found to be a semicolon,It means that it has been executed,In fact, the execution is an empty statement

注意2:悬垂 else 问题

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        if (a == 6)
            if(a == 5)
                System.out.println("yes");
        else
                System.out.println("no");
    }
}

问题:else 与哪个 if 匹配? 

答:if / else 语句中可以不加 大括号,You can also write sentences(只能写一条语句).此时 else 是和最接近的 if 匹配 

In the above code the result is not printed at the end because else 与第二个 if 匹配了. 

Through the above two codes, we can fully feel the importance of code style and code specification.

注:Do not put semicolons anywhere,Remember to add curly braces to the body of the statement

2.2 switch 选择语句

switch Statements are also called multi-branch statements,Usually used in multi-branch.当用 if When it is more complicated to use, we can use multi-branch.

比如:输入 1~7 之间的某一个数,然后打印对应的星期,如果用 if There are multiple layers involved else if It is relatively complicated,这时我们就可以使用 switch 选择语句

public class Test {
    public static void main(String[] args) {
        int a = 1;
        switch (a) {
            case 1:
                System.out.println("星期一");
                break;
            case 2:
                System.out.println("星期二");
                break;
            case 3:
                System.out.println("星期三");
                break;
            case 4:
                System.out.println("星期四");
                break;
            case 5:
                System.out.println("星期五");
                break;
            case 6:
                System.out.println("星期六");
                break;
            case 7:
                System.out.println("星期天");
                break;
            default:
                System.out.println("超出1~7范围");
        }

    }
}

执行流程

  1. 先计算表达式的值
  2. 和case依次比较,一旦有响应的匹配就Execute the statement under this item,直到遇到break时结束
  3. 当表达式的值没有与所列项匹配时,执行default 

上述代码执行逻辑,首先计算 switch 后面表达式的值,Then take this value in turn with case 后面的常量匹配,一旦匹配上Execute the statement under this item,直到遇到break退出,Otherwise, it will always be executed next time.If one isIf there is no match, execute default

注:case后的常量值不可以重复,不要遗忘 break

3.循环结构 

当条件满足,A statement that keeps looping is called a loop statement.Suppose Xiaomei buys a lottery ticket and doesn't win,As long as Xiaomei buys lottery tickets and doesn't win,Then she will buy it every day,As soon as she wins, she stops buying,This is called a cycle

3.1 while 循环语句 

public class Test {
    public static void main(String[] args) {
        int a = 1;
        while(a <= 10) {
            System.out.printf("%d ",a);
            a++;
        }
    }
}

执行结果: 

The above code loop statement is executed first while The following judgment expression,如果结果为 true 则执行循环体,然后再判断 while The following judgment expression,如果结果为 true 则继续执行循环体,...,直到 while The result of the following judgment expression is false 则退出循环

如果上述代码中没有 a++ 则会造成死循环 

So there are three parts to the loop,分别是:初识值,结束条件,Changes in near-end conditions 

3.2 break

break:If inside the loop body,则直接跳出循环.If in multi-branch,Then jump out of the multi-branch

public class Test {
    public static void main(String[] args) {
        int a = 1;
        while(a <= 10) {
            if(a == 7) {
                break;
            }
            System.out.printf("%d ",a);
            a++;
        }
    }
}

运行结果:

执行到 break 就会让循环结束 

上述代码执行逻辑,先执行 while The following judgment expression,如果为 true the loop body,The body of the loop determines whether the select statement is true ,如果为 true 直接跳出循环,否则继续往下执行

3.3 continue 

continue 直接结束本次循环,不会直接跳出循环 

public class Test {
    public static void main(String[] args) {
        int a = 1;
        while(a <= 10) {
            a++;
            if(a == 7) {
                continue;
            }
            System.out.printf("%d ",a);
        }
    }
}

执行结果: 

上述代码执行逻辑,先执行 while The following judgment expression,如果为 true the loop body,The body of the loop determines whether the select statement is true ,如果为 true 跳出本次循环,继续执行下一次循环

注意: 

public class Test {
    public static void main(String[] args) {
        int a = 1;
        while(a <= 10) {
            if(a == 7) {
                continue;
            }
            a++;
            System.out.printf("%d ",a);
        }
    }
}

一样的代码,The order may be different and the final result may be different.This code is because a++选择语句后面就造成了死循环,当 a == 7 时,跳出本次的循环,也就是本次循环 if Statements following the statement are not executed.Then this cycle is not executed a++,那么 a 的值还是 7,进入循环遇到 if 语句执行 continue,This cycle is over again a 还是 7,因此就造成了死循环

注:The order of code statements is still very important 

3.4 for 循环

语法格式:

表达式①:Used to set initial values ​​for loop variables, It will be executed when the program starts,且只会执行一次

表达式②:循环条件的判断,如果判断为 true 则继续执行循环体,否则循环结束

表达式③:循环变量更新方式

for 循环的执行过程:

  1. 首先执行表达式①,然后判断表达式②,如果为真则执行表达式④(否则直接退出循环),Then execute the expression③
  2. 判断表达式②,如果为真则执行表达式④(否则退出循环).Then execute the expression③
  3. 跟 2 的顺序一样,直到表达式②为false,退出循环
public class Test {
    public static void main(String[] args) {
        for (int i = 1; i <= 10;i++) {
            System.out.printf("%d ",i);
        }
    }
}

执行结果: 

  • for 下面的语句可以不写 { } , 但是不写的时候只能支持一条语句. 建议还是加上 { }
  • for 后面的 { 建议和 for 写在同一行.
  • for 后面不要多写 分号, 否则可能导致循环不能正确执行.
  • 和while循环一样,结束单趟循环用continue,结束整个循环用break 

3.5 do...while 循环 

do...whileLoops are also called until loops,It is a judgment after execution 

语法格式: 

先执行循环语句, 再判断循环条件,循环条件为 true 则继续执行,否则循环结束 

public class Test {
    public static void main(String[] args) {
        int i = 1;
        do {
            System.out.printf("%d ",i);
            i++;
        }while (i <= 10);
    }
}

 执行结果:

注:do while 循环最后的分号不要忘记 

4.输入输出 

4.1 输出 

public class Test {
    public static void main(String[] args) {
        int a = 1;
        System.out.println(a);//输出加换行
        System.out.print(a);//输出不换行
        System.out.printf("%d",a);//格式化形式输出
    }
}

输出结果:

  • println:输出,加换行
  • print:输出,不换行
  • printf:格式化形式输出 

So it's better to print both prints to one line. 

4.2 输入 

使用 Scanner 读取字符串/整数/浮点数 

在使用 Scannery 之前需要导入 util 包 

After learning the class, you will naturally understand why you need to import util 包了 

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        System.out.println(a);
    }
}

You just need to remember for now that you need to before using the classnew一个对象,也就是在使用Scanner 之前需要 new 一个Scanner ,然后才能通过 scanner use this object

copyright notice
author[Desperately Azi],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/266/202209231011089014.html

Random recommended