java basic loop for, while, do while

Keywords: Java

catalogue

1. while loop

2. do while loop

3. for loop

4. Practice

5. Knowledge points: ternary operator, ternary operator

1. while loop

While (condition){

          Code to be executed when the condition is true;

          Make the condition close to false operation;

}

Logic: when the condition is true, execute the code in {} and judge the condition after the code is executed. If the condition is still true, continue to execute the code in {},... Until the condition is not true at a certain time

Case: use the while loop to print all integers between 1-10  

       //Use the while loop to print all integers between 1 and 10  
        int n = 1;
        while (n<=10){
            System.out.println(n);
            n++;
        }

       //Print all integers between 10-1
        int m = 10;
        while (m>=1){
            System.out.println(m);
            m--;
        }

while loop  

while(true){

}

/**
 *   When you don't know how many times to cycle, it is recommended to use an infinite loop
 *   while Dead cycle of
 *   while(true){
 *
 *   }
 */
//Case: ATM password verification is related. Write a program to ask the user to enter the password and verify whether the password is correct
//If it is not correct, keep typing until it is correct

public class H_while {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int pwd;
        while (true){
            System.out.println("Please input a password:");
            pwd = scanner.nextInt();//Read password
            if (pwd==123456){
                break;//End the loop and do not allow input to continue
            }
        }
    }

}

2. do while loop

do{

          Code to execute;

          Make the condition close to false operation;

}While (condition)

Logic: execute the code in {} first, and then judge whether the condition is true. If it is true, continue to execute until the condition is not true

        //do while
        int i = 10;
        do {
            System.out.println(i);
            i--;
        }while (i>=1);

do while loop

do{

}while(true) 

3. for loop

For (initialization of variable ①; condition ②; operation to make the condition close to false ④){

            Code to execute ③;

}

Logic: first ① -- > judge ② -- > execute ③ -- > execute ④ -- > judge ②

        for (int i = 10; i >= 1; i--) {
            System.out.println(i);
        }

for loop

 for( ; true ; ){

}

4. Practice

ATM password verification is realized by looping: verify whether the password entered by the user is correct, and the number of errors can be up to three times. If the password is successful within three times, you should enter the main interface (login successful). If it fails three times, the printing account has been frozen

break: jump out of loop

Exercise 1: find all numbers between 1 and 100 that can be divided by 3 and 7 at the same time

        int x = 1;
        while (x<=100){
            if (x%3 == 0 && x%7 == 0) {
                System.out.println(x);
            }
            x++;
        }

Exercise 2: calculate the sum of all numbers between 1 and 100

        int y = 1;
        int total = 0;
        while (y<=100){
            total = total + y;
            y++;
        }
        System.out.println(total);

Exercise 3: find the least common multiple of two numbers

        //Least common multiple > = the greater number
        //Least common mu lt iple < = the larger number * the smaller number
        //[the larger number, big * small]

        int x = 6;
        int y = 2;
        //Identify larger numbers
        int max = 0;
        if (x>=y){
            max = x;
        }else {
            max = y;
        }

      //for loop
      for (int i=max;i<=x*y;i++){
          if (i%x==0 && i%y==0){  //The first number found is the least common multiple
              System.out.println(i);
              break;  //You shouldn't keep looking after it
          }
      }

      //while Loop 
      int j = max;
      while (j<=x*y){
          if (j%x==0 && j%y==0){
              System.out.println(j);
              break;
          }
          j++;
      }

Exercise 4: find the greatest common divisor of two numbers

      int p = 6;
      int q = 3;
      int min = p<=q ? p : q;

      int z = min;     //Look back and forward
      while (z>=1){
          if (p%z==0 && q%z==0){
              System.out.println(z);
              break;
          }
          z--;
      }

Exercise 5: guess numbers  

        //Randomly generated number
        //1 create a digital random generator
        Random random = new Random();
        //2 use the generator to generate numbers
        int num = random.nextInt(100)+1;//Generate numbers between [1-100]
        int guess;
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("Please enter a number");
            guess = scanner.nextInt();
            if (guess>num){
                System.out.println("Guess big");
            }else if (guess<num){
                System.out.println("Guess it's small");
            }else {
                System.out.println("You guessed right");
                break;//No, keep guessing
            }
        }

5. Knowledge points: ternary operator, ternary operator

  Target: only the number of data operated by the operator

unary operator

++  --  a++   b--   ~c    ~5

binary operator

+   -     *    /     a+b

ternary operator

?:

Condition / expression? Value 1: value 2;

Does the condition / expression hold? If it holds, you get the value 1, otherwise you get the value 2

Posted by asmith on Wed, 24 Nov 2021 02:11:07 -0800