[Java Basics - Java Control Statements]

Knowledge points: 1. Conditional control statement 2. Loop control statement 3. Jump statement

The following pictures are all from "realization building" and do not mean any advertising. In this statement, I am afraid of causing unnecessary disputes such as copyright.

1. Conditional control statement

1.1 if statement

grammar:

if (condition) {
    code to execute when the condition is true
}

 

If...else statement When the condition is satisfied, execute the code block in the if part; when the condition is not satisfied, enter the else part. For example, if a month has more than 30 days, it is a big month, otherwise it is a small month.

grammar:

if (condition) {
    code block 1
}
else{
    code block 2
}

 

For multiple if statements, when condition 1 is not satisfied, the judgment of condition 2 will be performed, and so on; when none of the previous conditions are established, the code in the else block will be finally executed.

grammar:

if (condition 1) {
    code block 1
}
else if (condition 2) {
    code block 2
}
...
else {
    code block n
}

 

Note: If there is only one execution statement when the if (or else if, or else) condition is true, the curly braces can be omitted! But if there are multiple execution statements, then curly braces are indispensable.

for example:

int days = 31;
if(days > 30)
    System.out.println("This month is a big month");
else
    System.out.println("This month is a small month");

 

Next, let's do a simple exercise: Xiaoming scored 78 points in the test, 60 points and above are pass, 80 points and above are good, 90 points and above are excellent, below 60 points, you need to retake the test, write a program, and output Xiaoming's situation .

The reference code is as follows:

public class ScoreJudge {
    public static void main(String[] args){
        int score = 78;
        if(score >= 60){
            if(score >= 80){
                if(score >= 90){
                    System.out.println("Excellent results");
                }
                else{
                    System.out.println("Good grades");
                }
            }
            else{
                System.out.println("Passed");
            }
        }
        else{
            System.out.println("Need to make up the test");
        }
    }
}

 

Note :所有的条件语句都是利用条件表达式的真或假来决定执行路径,Java里不允许将一个数字作为布尔值使用,虽然这在C和C++是允许的,如果要在布尔测试里使用一个非布尔值,需要先用一个条件表达式将其转换成布尔值,其他控制语句同理。

Second, the loop control statement

There are three loops, while, do{}while, for loop

2.1while

while syntax:

while(condition){
    code block
}

 

The execution process of while is to judge first and then execute.

  1. Determine if the condition after while is true ( true or false )

  2. When the condition is true, execute the code inside the loop, and then repeat the execution 1., 2., until the loop condition does not hold as

int i = 0;
while(i < 100){
    System.out.println("I love ShiYanlou!");
    i++;
}

 

 

2.2 do-while

grammar:

do{
    code block
}while(condition);

 

The execution process of do-while is to execute first and then judge (so the code in the loop will be executed at least once)

  1. First execute the loop operation once, and then determine whether the loop condition is true

  2. If the condition is true, continue to execute 1., 2., until the loop condition is not satisfied

 

int i = 0;
do {
    System.out.println("I love ShiYanlou!");
    i++;
} while(i < 100);

 

 2.3 for loop

grammar:

for(loop variable initialization; loop condition; loop variable change){
    Loop operation
}

 

For is more concise and readable than while and do-while statement structure, its execution order:

  1. Execute the loop variable initialization part to set the initial state of the loop, this part is executed only once in the entire loop

  2. Judging the loop condition, if the condition is true, execute the code in the loop body; if it is false, exit the loop directly

  3. Execute the loop variable change part to change the value of the loop variable for the next conditional judgment

  4. Re-execute 2., 3., , in turn 4., until you exit the loop

For example, to calculate the sum of numbers up to 100 that are not divisible by 3:

int sum = 0; // store the sum of numbers not divisible by 3
    // The initial value of the loop variable i is 1, and the variable is incremented by 1 each time it is executed, and the loop is repeated as long as it is less than or equal to 100
    for (int i = 1;i<=100;i++) {
    // The variable i and 3 are modulo (remainder), if not equal to 0, it means that it cannot be divisible by 3
        if (i % 3 != 0) {
            sum = sum + i; // accumulate the sum
        }
    }
    System.out.println("The sum of the numbers between 1 and 100 not divisible by 3 is: " + sum);

 

When talking about conditional control statements, I explained the nesting of if statements to the students. In the loop statement, three kinds of loop statements can be nested by themselves or each other. The most common one is the double loop. In the double loop, each time the outer loop is executed once, the inner loop needs to be executed once.

For example I want to print

*

**

***

****

graphics like this

// Outer loop controls the number of lines
        for (int i = 1; i<=4; i++) {

            // The inner loop controls the number of * signs in each line
            // The maximum value of the inner loop variable is equal to the value of the outer loop variable
            for (int j = 1; j<=i; j++) {
                System.out.print("*");
            }

            // wrap every line after printing
            System.out.println();
        }

 

3. Jump Statement

When we explained the switch statement above, we saw breakthis keyword. What is the use of it? Students, let's breakremove the code in the switch exercise above, run it, and find any problems? breakMeans to jump out, often used in conditional and loop statements, used to jump out of loop statements .

E.g:

for(int i = 1; i <= 10; i++){
        System.out.println("loop "+i+" times");
    if(0 == i % 3){
        break;
    }
    if(0 == i % 5){
        System.out.println("I'm in!");
    }
}

 operation result:

In the above code, the for statement would make the code inside the loop loop 10 times, but i=3at that time , it entered the first conditional statement, and when it encountered it break, the loop ended, and the second conditional statement could never be entered.

In the jump statement, there is another very continuesimilar break, its function is to skip the remaining statements in the loop body to execute the next loop.

For example, we want to print all odd numbers up to 10:

for(int i = 1; i <= 10; i++){
    if(0 == i % 2) //Determine whether i is even
        continue; //End this loop by continuing
    System.out.println(i);
}

 operation result:

 

3.1 Others

In loop statements, Java SE5 introduced a new more concise for syntax for arrays and containers, ie foreach语法. We will meet in later chapters.

gotoProgram control originated in assembly language, and although it is still a reserved word in Java, it is not used in the language.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325213798&siteId=291194637