Java Basic Grammar Note 3

Catalog

I. if

if

if...else

if...else if ...else

Nested if statement basic format

2. switch

Basic structure

case break default

Compare if with switch

3. while

Basic Format

4. do-while

Basic structure

Compare while with do-while

V. for

Effect

for Basic Format

Instructions for use

Enhance for

6. Flow charts for three loops

7. Keyword

break keyword

continue keyword

8. Note 2 Exercise Code (Operator)

I. if

  1. if

    Flow chart

     

    Basic structure

    If (Boolean expression) {

    //Statement to be executed if Boolean expression is true

    }

    public class Test1 {
        public static void main(String[] args) {
            int x = 10;
            if(x == 10){
                System.out.print("This is if Sentence");
            }
        }
    }
  2. if...else

    Flow chart

     

    Basic structure

    If (Boolean expression) {

    //Block of code that will execute if the final result of the condition is true

    }else{

    //Block of code that will be executed if the final result of the condition is false

    }

    public class Test1 {
        public static void main(String[] args) {
            int x = 10;
            if(x >= 10){
                System.out.print("x Greater than or equal to 10");
            }else{
                 System.out.print("x Less than 10");
            }
        }
    }
  3. if...else if ...else

    Flow chart

    Basic structure

    If (Boolean expression 1) {

    //Code Block 1

    }else if (Boolean expression 2){

    //Code Block 2

    }else{

    //Code Block 3

    }

    Judges Boolean expression 1 first, executes code block 1 if true, then exits if directly

    If Boolean expression 1 is false, then Boolean expression 2 is judged, and if true, code block 2 is executed

    If Boolean Expression 2 is not satisfied, then finally execute our code block 3

    Note: else if can write an infinite number, but else if must be written after if and before else. In the selection structure using multiple if, the order of conditions needs attention. String types cannot be judged with'==', but with'.equals()', for example (3.equals('3')).

    public class Test1 {
        public static void main(String[] args) {
            int JavaScore = 92;
            if(JavaScore >= 90){
                System.out.print("Java Excellent results");
            }else if(JavaScore >= 70){
                System.out.print("Java Good results");
            }else if(JavaScore >= 60){
                System.out.print("Java Achievement Pass");
            }else{
                System.out.print("Java Failed the grade");
            }
        }
    }

  4. Nested if statement basic format

    If (Boolean expression 1) {

    Execute code if the value of Boolean expression 1 is true

    If (Boolean Expression 2) {

    Execute code if the value of Boolean expression 2 is true

    }

    }

    public class Test1 {
        public static void main(String[] args) {
            /**
                School 50m race, requires less than or equal to 9 seconds to enter the final, and divided into men's and women's groups, otherwise you will not enter the final
            **/
            int time = 9;
            String sex = Male;
            if(time <=9){
                if(sex.equals("male")){
                    System.out.println("Enter Men's Group Final"); 
                }else{
                     System.out.println("Enter Women's Group Final");
                }
            }else{
                System.out.println("Not in the finals");
            }
        }
    }

2. switch

  1. Basic structure

    Switch (condition){

    case constant 1:

            break;

    case constant 2:

            break;

    case constant 2:

            break;

        default;

           break;

    }

    import java.util.Scanner;
    public class Test1 {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("Please enter a place");
            int num = input.nextInt();
            swicht(num){//Write the name of the variable directly here
                case 1:
                    System.out.print("Award Summer Camp");
                    break;
                case 2:
                    System.out.print("Reward Notebooks");
                    break;
                case 3:
                    System.out.print("reward U disc");
                    break;
                default:
                    System.out.print("No reward");
                    break;
            }
        }
    }

    Variables written in swtich parentheses are used as a basis for judgment, and their values are compared with those after case

    If num equals 1, then reward summer camps

    If num equals 2, reward notebook execution

    If num equals 3, execute reward U disk

    If they are not equal, there is no reward for execution

  2. case break default

    Case: the variable is compared to the value after the case

    Break: is to suggest that you must write something on it. If you do not write a break, it will continue after the current conditions have been implemented.

    default: default means code that executes when all the above conditions are not met

    switch was not supported before jkd1.7 and after 1.8

  3. Compare if with switch

    Same point: Both are used as branch structures (selection statements)

    The difference: switch can only be used for comparison of equivalents, if can do range comparison

3. while

  1. Basic Format

    While {

    //Code Block

    }

    public class Test1 {
        public static void main(String[] args) {
            //1 Define the initial variable of the loop
            int i = 1;
            while(i<=10000){//2. Cyclic Conditions
                System.out.println("No."+i+"All over, study hard");
                //3. Initial variable change
                i++;
            }
        }
    }

    Note: When using loops, as long as you want to write a normal loop, you must not forget to write the three elements of the loop

    1. Initial variables

    2. Cyclic Conditions

    3. Initial variable increases spontaneously

4. do-while

  1. Basic structure

    do{

    //Code Block

    } while (condition);

    public class Test1 {
        public static void main(String[] args) {
            //1 Define the initial variable of the loop
            int i = 1;
            do{
                System.out.println("No."+i+"All over, study hard");
                //2. Initial variable change
                i++; 
            } while(i<=10000);//3. Cyclic Conditions
        }
    }

  2. Compare while with do-while

    They are all identical in function

    The difference: while judges execution first

    do-while is executed first and judged, so do-while is executed once whether our conditions are met or not

V. for

  1. Effect

    Whole or do can be used for all loop structures. while is represented, Java provides another language, the for loop, to make some looping structures simpler.

  2. for Basic Format

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

    //Code Block

    }

    public class Test1 {
        public static void main(String[] args) {
            //Cycle 3 elements are all written in parentheses
            for(int i = 1;i<=10;i++){
             System.out.println("No."+i+"All over, study hard");
            }
        }
    }

    Note: All adjustments can be written in parentheses or not. If you do not write any conditions, you must write two semicolons in parentheses

  3. Instructions for use

    • Initialization steps are performed first. A type can be declared, but one or more loop control variables can be initialized, or an empty statement can be made.

    • Then, the value of the Boolean expression is detected. If true, the loop body is executed. If false, the loop terminates and the statement following the loop body begins to execute.

    • After a loop is executed, the loop control variable is updated.

    • Detect the Boolean expression again. The loop executes the above procedure.

  4. Enhance for

    Role: Used primarily for enhanced for loops in arrays.

    Basic Format

    For (declaration statement: expression) {

    //Code Block

    }

    public class Test1 {
        public static void main(String[] args) {
            int [] numbers = {10, 20, 30, 40, 50};
            //Cycle 3 elements are all written in parentheses
            for(int x : numbers){
                 //Output 10,20,30,40,50,
                System.out.print(x + ",");
               
            }
        }
    }

    Declare statement: Declare a new local variable whose type must match the type of the array element. Its field of action is limited to the loop statement block, and its value is equal to that of the array element at this time.

    Expression: An expression is the name of an array to access or a method that returns an array.

6. Flow charts for three loops

7. Keyword

  1. break keyword

    Role: break is mainly used in looping or switch ing statements to jump out of the entire statement block

    If the loop is nested, break jumps out of the innermost loop and continues executing the statement below the outer loop

    Example:

    public class Test1 {
        public static void main(String[] args) {
            for(int i =1;i<5;i++){
                if(i == 3){
                    break;
                }
                //Output 1 2
                System.out.print(i + " ");
            }
        }
    }

  2. continue keyword

    Role: continue applies to any loop statement, allowing the program like to jump to the next loop

    Example

    public class Test1 {
        public static void main(String[] args) {
            for(int i =1;i<=5;i++){
                if(i == 3){
                    continue;
                }
                //Output 1 2 4 5 
                System.out.print(i + " ");
            }
        }
    }

8. Note 2 Exercise Code (Operator)

 
import java.util.Scanner;
public class Test1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter 4-digit membership card number");
        int number = input.nextInt();
        int thousand = number / 1000;//Thousands
        int hundred = number / 100 % 10;//100 digits
        int tenDigit = number / 10 % 10;//Ten digits
        int singleDigit = number % 10;//Bits
        int sum = thousand + hundred + tenDigit + singleDigit;
        String happy = sum>20?"You won the prize. The prize is MP3":"I'm sorry you didn't win the prize";
        System.out.println("Membership card number is: " + number);
        System.out.println("Thousands: " + thousand + ", Hundred digit number: " + hundred +", Ten digit number: " + tenDigit + ", Your numbers: " + singleDigit);
        System.out.println("Membership Card Number" + number +"Your sum is: " + sum);
        System.out.println("Membership Card Number" + number +"Members, " + happy);
    }
}

 

Tags: Java server servlet

Posted by CherryT on Thu, 14 Jul 2022 06:05:15 +0530