Basic data types

In Java, there are basic data types and reference data types, and the basic data types are stored on the stack. There are mainly eight kinds as follows:

Integer:

  • byte: 8 bits. The value ranges from -128 to 127
  • short: 16,
  • int: 32-bit, the most commonly used integer type
  • long: 64

Floating point Numbers:

  • float: single-precision floating point number, suffix plus f (f)
  • double: indicates a double precision floating point number

Character:

  • char: one character

Boolean type:

  • boolean: The value can be true or false

Second, judge (branch) structure

If the else:

If else is the most commonly used statement structure. The statement is executed if the statement is true. If the statement is not true, the statement is skipped

boolean istrue=true;
if(istrue)
        {
            // If the condition is met, execute and skip directly. If not, skip directly
        }
        else  if(istrue)
        {
            // If the preceding criteria are met, the criteria will be skipped. If the preceding criteria are not met, the criteria will be skipped
        }
        else {
            // If all the above criteria are not met, execute directly
        }
Copy the code

The switch case:

The criteria are judged by matching numbers or strings. If the criteria are met, the criteria are executed. If the criteria are not met, the criteria are skipped

int nub=2;
        switch (nub)
        {
            case 0:
                // If nub is 0, this step is performed and no judgment is performed. If nub is not 0, this step is skipped
                break;
            case 1:
               // If nub is 0, this step is performed and no judgment is performed. If nub is not 0, this step is skipped
               break;
            default:
                // If the number is none of the above, perform this step
        }
Copy the code

Third, circular structure

while

The implementation logic of while is simple. If the judgment condition of while is true, the loop is executed, and if false, the loop is broken:

		boolean isOver;
        while(isOver)
        {
            // The loop continues when the statement isOver is true
           
            // The loop ends when isOver is false
        }
Copy the code

The for loop:

The for loop executes the loop by judging the range of the integer parameters in the structure. If the loop is within the range, it executes the loop; if it is not, it terminates the loop.

		int nub=10;
        for(int i=0; i<nub; i++) {// If I 
           
                // If I 
        }
Copy the code

Do while: Unlike while, do while executes the code of the loop before making a judgment, so the code is executed at least once in a do while statement

boolean isTrue=true;
        do{
            // Start sequential execution, then judge while, if while satisfies the condition, execute again until isTrue is false

        }while(isTrue);
Copy the code

Other ways to break the loop:

For loops, in addition to using constraints in the structure to break the loop, you can also use break to break the loop, or continue to skip a loop:

int i=0;
            while (true)
            {
                i++;
                if(i>4) break;  // If the value is greater than 4, the loop will be broken
                if(i==2) continue;  // If I is equal to 2, the next round is skipped
                System.out.println(i);
                // The output is: 1, 3, 4
            }
Copy the code