Java flow control 04 (While loop detailed explanation, For loop, break, continue)

While loop
structure:
while(Boolean expression){ //loop content}1. As long as the Boolean expression is true, the loop will continue to execute2. In most cases, the loop is stopped, so a way to invalidate the expression is needed To end the loop3. In a few cases, the loop needs to be executed all the time, such as the server's request response monitoring, etc.4. The loop condition is always true, which will cause an infinite loop (endless loop) code example (output 1-5)






package com.hao.struct;

public class WhileDemo01 {
    public static void main(String[] args) {
        int i=0;
        //输出1-5
        while(i<5){
            i++;
            System.out.println(i);
        }

    }
}

Output sample
Insert picture description here
code example (1-100 summation)

package com.hao.struct;

public class WhileDemo01 {
    public static void main(String[] args) {
        int i=0;
        int sum=0;
        //1-100求和
        while(i<100){
            i++;
            sum+=i;
        }
        System.out.println(sum);

    }
}

Output example
Insert picture description here
do...while loop
Structure:
do{ //code statement }while(Boolean expression); 1. The do...while loop is similar to the while loop, except that the do...while loop will execute the code example at least once (1- 100 sum)



package com.hao.struct;

public class WhileDemo01 {
    public static void main(String[] args) {
        int i=0;
        int sum=0;
        do{
            i++;
            sum+=i;
        }while(i<100);
        System.out.println(sum);

    }
}

Output example
Insert picture description here
For loop
structure:
for (initialization; Boolean expression; update) { //code statement } 1. The for loop statement is a general structure that supports iteration, and is the most effective and flexible loop structure 2. For loop execution The number of times is determined before execution. Code example (1-100 summation)




package com.hao.struct;

public class ForDemo01 {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum+=i;
        }
        System.out.println(sum);
    }
}

Output sample
Insert picture description here
code example (1-100 odd and even numbers separately)

package com.hao.struct;

public class ForDemo02 {
    public static void main(String[] args) {
        int oddsum=0;//奇数
        int evensum=0;//偶数
        for (int i = 0; i <= 100; i++) {
            if(i%2==0){
                evensum+=i;
            }else{
                oddsum+=i;
            }
        }
        System.out.println("偶数和="+evensum);
        System.out.println("奇数和="+oddsum);
    }
}

Output sample
Insert picture description here
code example (output a number divisible by 5 in 1-100, and change the line every third number)

package com.hao.struct;

public class ForDemo03 {
    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            if(i%5==0){
                System.out.print(i+"\t");
            }
            if(i%(15)==0){
                System.out.println();
            }
        }
    }
}

Sample output
Insert picture description here

Gains and discoveries
1. Line will wrap after println output 2. Line
will not wrap after print output
Code example (99 multiplication table)

package com.hao.struct;

public class ForDemo04 {
    public static void main(String[] args) {
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j <=i; j++) {
                System.out.print(j+"*"+i+"="+j*i+"\t");
            }
            System.out.println();
        }
    }
}

Sample output
Insert picture description here

Enhanced For loop
structure:
for (declaration statement: expression){ //code statement}1. Enhanced for loop code example (traversing array)mainly used for arrays or collections



package com.hao.struct;

public class ForDemo05 {
    public static void main(String[] args) {
        int [] numbers = {10,20,30,40,50};
        //遍历数组中的元素
        for(int x:numbers){
            System.out.println(x);
        }
    }
}

Output example
Insert picture description here
break
1. In the main part of any loop statement, break can be used to control the flow of the loop. Break is used to forcibly exit the loop without executing the remaining statements in the loop. (The break statement is also used in the switch statement)
Code example (output 1-10, abort at 3)

package com.hao.struct;

public class BreakDemo {
    public static void main(String[] args) {
        int i=0;
        while(i<10){
            i++;
            System.out.println(i);
            if(i==3){
                break;
            }
        }
    }
}

Sample output
Insert picture description here


Example of continue code (output 1-100, can be divisible by 10 to skip)

package com.hao.struct;

public class ContinueDemo {
    public static void main(String[] args) {
        int i=0;
        while(i<100){
            i++;
            if(i%10==0){
                System.out.println();
                continue;
            }
            System.out.print(i);
            System.out.print(" ");
        }

    }
}

Sample output
Insert picture description here

1. The continue statement is used in the loop statement body to terminate a loop process, that is, skip the statement that has not been executed in the loop body, and then proceed to determine whether to execute the loop next time.
Contact (print triangle)
code example

package com.hao.struct;

public class TextDemo01 {
    public static void main(String[] args) {
        //打印三角形
        for (int i = 0; i < 5; i++) {
            for (int j = 5; j >=i; j--) {
                System.out.print(" ");
            }
            for(int j=0;j<=i;j++){
                System.out.print("*");
            }
            for(int j=0;j<i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Sample output
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_51224492/article/details/111276351