Java Keyword - loop and judgment

Java Keyword - program control

for,while,do..while,switch
break,continue,if,else,case
default,instanceof,return,assert

loop

for loop

For loops are divided into enhanced for loops and general for loops
General for loop format:
for (initialization variables are executed only once; variables judged by entering the loop; changes of variables){
Circulatory body;
}
Simple for loop application

public class Demo {

    public static void main(String[] args) {
        for (int i = 0; i < 3; i++) {
            System.out.println(i);
        }
        //You can also remove the elements before and after the for bracket as follows
        int j = 0;
        for (; j < 3; ) {
            System.out.println(j);
            j++;
        }
        //Of course, you can remove them all
        j = 3;
        for (; ; ) {
            System.out.println(j);
            j--;
            if (j < 0) {
                break;
            }
        }
    }
}


Enhanced for loop format:
For (data type variable name: the object to be traversed){
//Loop body, variables are elements
}
The purpose of enhancing the for loop is to simplify the traversal of the collection of arrays

public class Demo {

    public static void main(String[] args) {
        //Enhanced for loop
        int[] a = {0, 1, 2, 3};
        for (int i : a) {
            System.out.println(i);
        }
    }
}

break and continue

break
In the loop, it means to terminate the loop, but only one layer of loop can be terminated. By default, the loop closest to break is terminated
continue
It is embodied in the loop. When the continue statement is encountered during the loop, it means to skip the statement under the current loop body and execute the next loop

public class Demo {

    public static void main(String[] args) {
        //break terminates the entire loop body
        for (int i = 0; i < 4; i++) {
            if (i == 2) {
                break;
            }
            System.out.println(i);
        }
        System.out.println("-------------");
        //continue is to end this cycle and then execute the next cycle
        for (int i = 0; i < 4; i++) {
            if (i == 2) {
                continue;
            }
            System.out.println(i);
        }
    }
}

while Loop

Format:
while (condition judgment){
Code block;
}

public class Demo {
    public static void main(String[] args) {
        int a = 0;
        while (a < 5) {
            System.out.println("a=" + a);
            a++;
        }
    }
}

do... while loop

do{
Statement block;
}
while (condition judgment)

import java.util.Scanner;

public class Demo {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        char c;
        do {
            System.out.println("Enter a symbol and I will determine whether it is lowercase");
            c = input.next().charAt(0);//Input character
        } while (c > 96 && c < 123);
        //Because the ASCII values corresponding to 'a' to 'z' are 97 to 122
        //If the input is correct, the cycle will continue. If not, the cycle will exit directly
    }
}

judge

if (conditional) {expression} else {expression}

Judgment means that if the condition is true, and else if not
if... else... Can be divided into the following four types
if (conditional) {expression}
if (conditional) {expression 1} else {expression 2}
If (condition 1) {expression 1} else if (condition 2) {expression 2}... Else if (condition n) {expression n}
If (condition 1) {expression 1} else if (condition 2) {expression 2}... Else if (condition n) {expression n} else {}

public class Demo {
    public static void main(String[] args) {
        int x = 10;
        if (x > 5) {
            System.out.println("Expression 1");
        } else if (x > 6) {
            System.out.println("Expression 2");
        }
        //In this case, the code is executed from top to bottom, so it is established when x > 5 is judged, and exit the judgment
        //The result should be expression 1
        System.out.println("------------------");
        x = 10;
        if (x < 5)
            System.out.println("Expression 3");
        System.out.println("Expression 4");

        //if...else is if(){} else() {} by default
        //Without parentheses, only the first line is executed by default. If the following is no longer a judgment statement, the judgment ends by default
        //So the result is expression 4
    }
}

if... else is the same in other cases

switch

switch, case, default and break are generally used together
The general form is:
switch (expression){
case constant value 1: statement block; break;
case constant value 2: statement block; break;
case constant value 3: statement block; break;
...
case constant value n: statement block; break;
default: statement block;
}
The general types of expressions can be: byte, short, int, char, enumeration, String
Constant values are fixed values, which are generally different and do not repeat
In switch, if there is no break, when a constant value matches, it will be executed from the current statement block to the end of switch. Therefore, in general, a break statement is added after each case statement block.
Default means that after excluding all break statements, the default statement is executed if the conditions are not met.

public class Demo {
    public static void main(String[] args) {
        int x = 10;
        switch (x) {
            case 1:
                System.out.println("x=1");
                break;
            case 2:
                System.out.println("x=2");
                break;
            case 3:
                System.out.println("x=3");
                break;
            default:
                System.out.println("x Greater than 3");
                //The result should be x greater than 3
        }
        System.out.println("-------------------");
        int y = 2;
        switch (y) {
            case 1:
                System.out.println("x=1");
                break;
            case 2:
                System.out.println("x=2");
            case 3:
                System.out.println("x=3");
            default:
                System.out.println("x Greater than 3");
                //The result should be x=2
                //           x=3
                //           x greater than 3
        }
    }
}

How should the default statement be executed when it is in a case
When the code is running, first check whether all case statements meet the conditions. If not, then check the default statement. However, note whether there is a break statement at the end of the default statement block. If not, execute the next case statement.

public class Demo {
    public static void main(String[] args) {
        int x = 10;
        switch (x) {
            case 1:
                System.out.println("x=1");
                break;
            default:
                System.out.println("x Greater than 3");
            case 2:
                System.out.println("x=2");
                break;
            case 3:
                System.out.println("x=3");
                break;
            //The result should be x greater than 3
            //              x=2
        }

    }
}

instanceof

instanceof is the judgment of type

public class Demo {

    public static void main(String[] args) {
        //Judgment of instanceof type
        Object x = 10;
        //Object is any type represented in java, but now it is necessary to determine which type is specified in the object
        //The return value of instanceof is also Boolean true or false
        System.out.println(x instanceof Integer);//Return true
        // Judge whether x is of integer type (wrapper class of int)
        Object y = 'a';
        System.out.println(y instanceof Character);//Return true
        System.out.println(y instanceof String);//Return false
    }
}

return

return means to terminate a function. return is used when actively ending a function
That is, all statements after return will not be executed again. When using a function, return also means to return. A value is returned from a function to the function calling the function, so in most applications, a return value will be added after return

public class Demo {
    //Main is the main function
    public static void main(String[] args) {
        System.out.println("This is the main function");
        int a = fan();//Call the fan function, which returns a value
        System.out.println("a=" + a);
    }

    public static int fan() {
        System.out.println("This is the called function");
        return 2;   //return returns the value of the function being called
    }
}

assert

As a method of software debugging, assert provides a mechanism for correctness checking in code.
In program development, in order to ensure the correctness of the program, it is usually used in development and testing

Keywords: Java

Added by ashebrian on Fri, 17 Dec 2021 01:03:29 +0200