Branch structure and loop structure

Posted by chucklarge on Sun, 03 Oct 2021 23:03:08 +0200

1. Branch

2. Circulation

1. Branch structure

Branch of life: two-way choice. If you come to the crossroads. How many options do you have

Branching in a program: that's an option

1.1if structure

Syntax format

if (conditional){

Statement body

}

Condition: must be data of type boolean

Execution process: when the program runs to the if structure, first judge the conditions in the if parentheses. If true, execute the statement body. If false, skip the statement body and execute the following code.

package com.qfedu.app;

/**
 * This is the code of the if branch structure
 */
public class Demo5 {
    public static void main(String[] args) {
        int money = 10;
        if (money > 12) {
            System.out.println("Buy a Lamborghini");
        }
        System.out.println("Program end");
    }
}

1.2 if else structure

Syntax format:

if (conditional){

Statement body 1

} else {

Statement body 2

}

Execution process: when the program runs to the if else structure, first judge the conditions in the parentheses. If it is true, execute statement body 1. If it is false, execute statement body 2

package com.qfedu.app;

/**
 * if-else Structure code
 */
public class Demo6 {
    public static void main(String[] args) {
        int a = 10;
        if (a >= 10) {
            System.out.println("Have a good lunch today, instant noodles");
        } else {
            System.out.println("Northwest wind");
        }
        System.out.println("Ha ha Da");
    }
}

1.3 if else if structure

Syntax format:

if (condition 1){

Statement body 1

}else if (condition 2){

Statement body 2

}Else if (condition 3){

Statement body 3

}else if (condition 4){

Statement body 4

}else {

Statement body 5

}

Execution process:

When the program runs to if else, it matches all the conditions after if. If the match is true, it outputs the current statement body s

package com.qfedu.app;

/**
 * This is the if else if code
 */
public class Demo7 {
    public static void main(String[] args) {
        /*
        * Student achievement:
        *       90-100  Print Xiuer
        *       80-90   Good printing
        *       70-80   Print general
        *       60-70   Print pass
        *       60 Print mouse tail juice below
        *
        * */
        int score = 90;
        if (score >= 90 && score <= 100) {
            System.out.println("Xiuer");
        } else if (score >= 80) {
            System.out.println("good");
        } else if (score >= 70) {
            System.out.println("commonly");
        } else if (score >= 60) {
            System.out.println("pass");
        } else {
            System.out.println("Mouse tail juice");
        }
    }
}

1.4 switch case branch

Syntax format:

Switch (condition matching){

case constant:

Statement 1;

​ break;

case constant:

Statement 2;

​ break;

case constant:

Statement 3;

​ break;

​ default:

Statement 4;

​ break;

}

Execution process: when the program runs to switch, the value of the variable in parentheses behind it matches the constant after case. If it matches, it is the current statement. If it does not match, it will continue to match until all constants are matched. If it is found that there is no match, it will execute the statement under default

Break: break!!!

2. Circulation structure

2.1 why use circular structure

If there are a lot of duplicate functions in the code, you can use the cv method. It can be solved, but some problems will occur:

1. The code is too bloated

2. The reading ability of the code is very poor

3. Code maintenance is extremely poor

2.2 while loop

Syntax format:

while (judgment of loop conditions){

Circulatory body

}

Execution process: after the code is executed to while, first judge whether the loop condition in the parentheses after while is true. If it is true, continue execution

Loop body. The loop ends when the condition in the parentheses is false.

package com.qfedu.app;


/**
 *
 */
public class Demo12 {
    public static void main(String[] args) {
        //Print out 10 lines, eat, sleep and beat peas
        /*
        * When num = 10 > 0 true, output the first num minus 1 num =9
        *When num = 9 > 0 true, output the second num minus 1 num = 8
        *When num = 8 > 0 true, output num itself minus 1 num = 7 for the third time
        * .....
        *When num = 1 > 0 true, output the 10th num minus 1 num = 0
        * When num = 0 > 0 false, the loop ends
        * */
        //A loop has at least three conditions: initial value, termination condition and loop condition
        int num = 10;
        while (num > 0 ) {
            System.out.println("Eat, sleep and beat peas");
            num--;
        }
    }
}

2.3do while cycle

Syntax format:

do {

Circulatory body

}While (cycle condition);

Execution process: when the program runs to do while, first execute the loop body in the braces after do. Then judge the conditions in the braces after while. If the condition is false, the loop body will be printed once after the loop ends. If the condition is true, continue to execute the loop body in the braces after do.

package com.qfedu.app;

public class Demo13 {
    public static void main(String[] args) {
        //Also print eating, sleeping and playing peas
        /*
        * Printed the first time to eat and sleep, hit Doudou num itself minus 1 9 > 0 true
        * Printed the second time to eat and sleep, hit Doudou num itself minus 1 8 > 0 true
        * Printed the third time to eat and sleep, hit Doudou num itself minus 1 7 > 0 true
        * ......
        * Print the 9th meal and sleep, beat Doudou num itself minus 1 1 > 0 true
        * Print the 10th meal and sleep, beat Doudou num itself minus 1 0 > 0 false, and end the cycle
        * */
        int num = 10;
        do {
            System.out.println("Eat and sleep and beat peas");
            num--;
        } while(num > 0);
    }
}

The while loop directly judges the condition. If the condition judgment is not true, it will not be executed. Do while executes once first, and then judges whether the condition is true.

Only while will be used in future development

2.4 for loop

Syntax format:

For (condition 1; condition 2; condition 3){

Statement body

}

Execution process: execute condition 1 first and then condition 2. Condition 2 is an expression that returns a Boolean value. If condition 2 is true, the statement body is executed.

Then execute condition 3. This loop continues until condition 2 is false

package com.qfedu.app;

public class Demo14 {
    public static void main(String[] args) {

        /*
        * int i = 0 Declared an i variable condition 1
        * i< 10  Termination condition 2
        * i++  Cycle condition 3
        *
        *
        * When i = 0 (the first condition) 0 < 10 (the second condition) true, print once, eat and sleep, hit Doudou (sentence body), and then look at the third condition i itself plus 1, i = 1
        * When i = 1 1 < 10 true, print once, eat and sleep, beat peas, and then look at the third condition, i itself plus 1, i = 2
        * When i = 2 < 10 true, print once, eat and sleep, beat peas, and then look at the third condition, i itself plus 1, i = 3
        *
        *.......
        * When i = 8 8 < 10 true, print once, eat and sleep, beat peas, and then look at the third condition, i itself plus 1, i = 9
        * When i = 9 9 < 10 true, print once, eat and sleep, beat peas, and then look at the third condition, i itself plus 1, i = 10
        * False terminates when I = 10 < 10
        * */
        for(int i = 0; i < 10; i++) {
            System.out.println("Eat and sleep and beat peas");
        }
    }
}

task

1.Print even numbers within 100
2.Print odd numbers within 100
3.100 Every 7 within
4.Print 99 multiplication table
 Today's cases must be typed more than three times
 Jinshan typing

Supplementary knowledge points

Use of Scanner (not used in the future!!!)

In the code, the user needs to modify the data to meet other needs. However, we cannot ask the user to modify the code. Recompile and reproduce the execution
 You need to provide users with a way to enter data in the code:
1.Skill points (Guide Package)
	stay class before
	import java.util.Scanner
2.Create a scanner One of"variable"
	Scanner scanner = new Scanner(System.in);
3.use scanner Scanner some methods. Get the data you enter from the keyboard;
	obtain int Type data   int num = scanner.nextInt();
	obtain float Type data  float num = scanner.nextFloat();
	obtain double Type of data  double num = scanner.nextDouble();
	obtain char Type of data   char ch = scanner.nextLine().charAt(0);
package com.qfedu.app;

//Guide Package
import java.util.Scanner;
//It is a class encapsulated in java. The function of the class is the input of the console
public class Demo8 {
    public static void main(String[] args) {
        int score = 0;
        //1. Create a Scanner object
       Scanner scanner = new Scanner(System.in);
        System.out.println("Pro! Please enter a student's grade");
        //2. Get a data from the keyboard and assign the data to score
        score = scanner.nextInt();
        //3. Legitimacy of the data entered by the user
        if (score < 0 || score > 100) {
            System.out.println("Pro, the data you entered is illegal!!!");
            //Exit program
            System.exit(0);
        }
        if (score >= 90 && score <= 100) {
            System.out.println("Xiuer");
        } else if (score >= 80) {
            System.out.println("good");
        } else if (score >= 70) {
            System.out.println("commonly");
        } else if (score >= 60) {
            System.out.println("pass");
        } else {
            System.out.println("Mouse tail juice");
        }

    }
}

Topics: Python Java