〔003〕Java basic process control

▷ If branch of process control

  • ifUsed to make logical branches. Different code segments are executed when judging different logical ranges. There are mainly three formats:
  • 格式一:if(条件表达式) { 代码逻辑 }It is used to execute code logic that meets the conditions.
  • 格式二:if(条件表达式) { 代码逻辑1 } else { 代码逻辑2 }It is used to execute the code logic of establishment and failure.
  • 格式三:if(条件表达式1) { 代码逻辑1 } else if(条件表达式2) { 代码逻辑2 } else { 代码逻辑3 }It is used to execute code logic under multiple different conditions.

if branch

▷ Switch branch of process control

  • switchThe difference between the branch and if is that switch can only compare certain values, while if can judge a certain range Whether the value within meets the conditions
  • Soif is actually more powerful than switch, but for multiple conditionsswitch the branch looks more clear< /span>

switch branch

▷ Break penetration of switch branch

  • Don’t forget to write in each judgment in switch, otherwise penetration will occurbreak
  • means that the code in the next judgment will be executed directly, and the branch will not be jumped out until break is encountered
  • But sometimes if the code executed in multiple branches is the same, you can use this method to reduce the amount of code, which looks more concise.

break penetration

▷For loop of process control

  • Loop is a method to control the execution of a piece of code multiple times in the code, mainly including for循环, while循环, do-while循环
  • The format of the for loop: for (初始化语句; 循环条件; 迭代语句) { 重复执行的代码 }
  • For example, print the output statement of 5次 as follows

for loop

▷ While loop of process control

  • while loop and for loop actually have exactly the same function, and both can solve the same loop problem
  • But there is a slight difference: if you know how many times you need to loop, it is recommended to use for循环; if you don’t know how many times you need to loop, it is recommended to use while循环
  • The format of the while loop: while (循环条件) { 重复执行的代码; 迭代语句; }

while loop

▷ Process control while loop case

  • Maybe the above cannot be understood and for循环的区别, then you can make a case
  • Case: The height of Mount Everest, the highest mountain in the world, is: 8848.86 meters = 8848860 millimeters. If I have a large enough piece of paper, its thickness is 0.1 millimeters. I would like to ask: How many times can this paper be folded to the height of Mount Everest?
  • You can know that at the beginning, you don’t know how many times you need to fold it to meet the requirements. At this time, you can use while循环 to answer this question
  • Paper 每折叠一次厚度加一倍, then exit the cycle when the thickness exceeds the peak
package tiny.geeker;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        int count = 0; // 定义折叠次数
        double thickness = 0.1; // 定义纸张厚度

        while (thickness < 8848860) {
    
    
            thickness *= 2; // 每折叠一次,厚度加一倍
            count++;
        }

        System.out.println("总共需要次数为:" + count);
    }
}

while loop case

▷ Flow control do-while loop

  • do-while循环The difference between and while循环 is actually just one point. while循环 Because the loop condition is in front, the loop condition will be judged first and then the code in the loop body will be executed. do-while循环 is just the opposite. It executes the loop body first and then judges the condition
  • The format of the do-while loop: do { 重复执行的代码; 迭代语句; } while (循环条件)
package tiny.geeker;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        int i = 0;
        do {
    
    
            System.out.println("飞兔小哥");
            i++;
        } while (i < 5);
    }
}

do-while loop

▷ Infinite loop

  • 死循环:This is when the loop condition is written to always hold true, causing the program to keep running and unable to stop.
  • 死循环It is easy to cause memory leaks, system freezes and other situations, so use it with caution.死循环
  • Since the condition is always true, it is equivalent to writing 2 > 1. This situation where it is always true can also be written directly as true

infinite loop
infinite loop

▷ Nested loops

  • 循环嵌套:is a loop containing another set of loops
package tiny.geeker;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        for (int i = 1; i <= 5; i++) {
    
    
            for (int j = 1; j <= 2 * i - 1; j++) {
    
    
                System.out.print("*");
            }
            System.out.println(" ");
        }
    }
}

Nested loops

▷ Loop keywords break and continue

  • When you branch aboveswitch, you can see that the branch body has the break keyword, which is used to jump out of the branch. Use < in the loop /span>break Just to get out of the loop
  • break:Jump out of all loops directly
  • continue:There are many loops, just skip the current one and execute the next loop directly.
package tiny.geeker;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        int i = 0;
        while (i < 5) {
    
    
            i++;
            if (i >= 3) break; // 大于3就跳出所有循环
            System.out.println("飞兔小哥" + i);
        }

        System.out.println("---------------------");

        int j = 0;
        while (j < 5) {
    
    
            j++;
            if (j == 3) continue; // 跳出3的循环
            System.out.println("飞兔小哥" + j);
        }
    }
}

Loop keyword

▷ Random number Random

  • Random number is an officially provided API, which returns a value to you randomly. It is usually used in random events such as lottery.
  • For example, there are 5 people in the company, and the prizes are first, second, and third place, and the users randomly draw the prizes.
package tiny.geeker;

import java.util.Random;

public class Test {
    
    
    public static void main(String[] args) {
    
    
        for (int i = 1; i <= 5; i++) {
    
    
            Random r = new Random();

            int level = r.nextInt(3) + 1;

            System.out.println("用户" + i + "获得奖品等级:" + level);
        }
    }
}

Random number usage

▷ Notes on Random Numbers

  • You can see that when using the method in new Random(), nextInt() is used. This method returns an integer randomly. If the parameters are filled in, It will return an integer value between [0, 参数), excluding the parameter itself, so add 1 after it.
  • Of course, in addition to the nextInt() method, there are also the nextFloat(), nextBoolean(), and nextDouble() methods It can be used. You can see from the literal meaning that it randomly returns floating point numbers, Boolean values, and decimals
  • Case: How to get a number between 65-91?

Random number case

Guess you like

Origin blog.csdn.net/weixin_41635750/article/details/134376890