Briefly describe the simple loop in Java

JAVA loop structure

循环,字面理解就是重复的做某件事,比如循环使用环保袋子,循环听一首歌等等。所谓循环,就是反复执行一段代码,直到满足终止循环的条件为止。
循环结构的的特点:
         循环条件          //10圈
         循环起始值      //1,2,3.......10圈   开始计数的值
         循环自增量(每次自增的量)//  迭代   对重复执行动作的一个反馈
         循环操作    //跑步

1, switch loop

1, switch (expression) { case constant 1; statement; break; case constant 2; statement; break; default: statement; } 2, diagram:











Insert picture description here

Note : The expression in switch can only put byte short int char String

Comparison of if and switch:
all
if can be realized by switch can be realized. On the contrary, if can be realized, switch may not be realized.
Switch can only do equivalent judgment, if can do interval judgment

3. Code display:

package test;

import java.util.Scanner;

public class test01 {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("请输入星期数:");
        Scanner scanner = new Scanner(System.in);
        int day = scanner.nextInt();

        switch (day){
    
    
            case 1:
                System.out.println("好好工作");
                break;
            case 2:
                System.out.println("好好工作");
                break;
            case 3:
                System.out.println("好好工作");
                break;
            case 4:
                System.out.println("好好工作");
                break;
            case 5:
                System.out.println("好好工作");
                break;
            case 6:
                System.out.println("休息");
                break;
            case 7:
                System.out.println("准备工作");
                break;

            default:
                System.out.println("输入的时间不对");
                break;
        }
    }
}

Two, while loop
1, syntax:
loop start value, initial part // where the variable is defined, declare the variable
while (loop condition) { loop operation iterative part // always return to the executed operation } 2, the order of execution: 3, Code display:




Insert picture description here

public class lianxi02 {
    
    
    public static void main(String[] args) {
    
    
        int i=0,sum=0;
        while (i<=100) {
    
    
            if (i%2==0){
    
    
                sum=sum+i;
            }
            i++;
        }
        System.out.println("100内的偶数之和是:"+sum);
    }
}

Three, do...while loop
1. Features: execute first, then judge.
2. Syntax:
do{ loop operation }while (loop condition); 3. Execution order (when the initial value does not meet the loop condition, the while loop will not operate once, the do while loop will be executed at least once under any circumstances .) 4. Code display:



Insert picture description here

public class lianxi03 {
    
    
    public static void main(String[] args) {
    
    
        int i=0,sum=0;
        do {
    
    
            if (i%2==0){
    
    
                sum=sum+i;
            }
            i++;
        }while (i<=100);
        System.out.println("100内的偶数和:"+sum);
    }
}

Four, for loop
1, syntax
for (initial value of the loop; loop condition; self-increment of the loop ) { loop operation; }

当我们知道循环次数的时候,使用for循环比较简约。

2. Code display

public class lianxi01 {
    
    
    public static void main(String[] args) {
    
    
        int sum=0;
        for (int i=0;i<=100;i++){
    
    
            if (i%2==0){
    
    
                sum=sum+i;
            }
        }
        System.out.println("100内偶数之和是:"+sum);
    }
}

Five, the difference between break and continue.
1, break: break terminates the loop.
Break features
①. The break statement is used to terminate a loop and make the program jump to the next statement outside the loop block
②. The statement after the break in the loop will no longer be executed
③. Not only can be used in the loop, but also can be used In other statements
2, continue: skip the remaining statements in the loop body and execute the next loop. It
can only be used in the loop.

Guess you like

Origin blog.csdn.net/tan1024/article/details/109800125