Java basics: loop statements for, while and do-while

The last few articles introduced you to the operators in Java. From this article, I will bring you the control statements in the Java foundation. Today I will mainly introduce the while, do while, and for statements to you, let us start directly.

overview

The while, do while, and for statements can be collectively referred to as loop statements in Java. Imagine a scenario. If there is a requirement that you print from 1 to 100, would you write 100 statements System.out.println();? Obviously this is impossible, so the loop statement is particularly important.

In Java, we have three basic loops: for, while, and do-while.

  • for loop : Execute a statement a specified number of times
  • while loop : executes a statement an unknown number of times
  • do-while loop : execute statement at least once

Below we will explain these three loop statements in the form of a flowchart combined with code!

for loop

The for loop is a repeating control structure that allows you to efficiently write a loop that needs to be executed a specific number of times, the syntax is:

for (初始化表达式; 条件; 更新表达式) {
    // 循环语句主体
}
  • Initialization expression : That is, variable initialization, which specifies which number to start the cycle from, and is only executed once during the entire cycle.
  • Condition : Conditional logic, the loop body is executed only when the condition is true; if it is false, the looping process jumps out of the loop.
  • Update Expression : After the body of the loop statement is executed, flow goes to this statement and changes the value of the variable declared in the first part of the loop, incrementing the value in case of ++ and decrementing it in case of –.
  • Loop statement body : business logic processing.

Code demo:

package com.test.javaroads.loop;

/**
 * @author: javaroads
 * @date: 2022/12/9 15:21
 * @description: for循环
 */
public class ForLoop {

    public static void main(String[] args) {
        for (int i = 0; i <= 10; i++) {
            System.out.println("i的值为 = " + i);
        }
    }
}

Results of the:

i的值为 = 0
i的值为 = 1
i的值为 = 2
i的值为 = 3
i的值为 = 4
i的值为 = 5
i的值为 = 6
i的值为 = 7
i的值为 = 8
i的值为 = 9
i的值为 = 10

Combined with the above code, we explain the workflow of the for loop in the form of a flowchart:

while loop

A while loop is also a control structure similar to a for loop that allows you to repeat a task multiple times.

The only difference between a while loop and a for loop is that a for loop repeats a task a specific number of times while a while loop repeats a task an unknown number of times.

Syntax of while loop:

while(条件){
     //如果条件为true,将执行这里的代码
}

As you can see from the grammar, the while loop is divided into two parts:

  • Condition : Unlike a for loop, this loop does not have any initialization part, this part uses some conditional logic where a regular expression is evaluated and the body of the loop is executed until the condition is true. If false, the looped flow breaks out of the loop.
  • Body : As above, executes until the expression's condition is true. However, if the condition returns false at the very beginning of the while loop, the body may not be executed once.

Code demo:

package com.test.javaroads.loop;

/**
 * @author: javaroads
 * @date: 2022/12/9 15:33
 * @description: While循环
 */
public class WhileLoop {

    public static void main(String[] args) {

        int a = 1;

        while (a < 10) {
            System.out.println("a的值为: " + a);
            a++;
        }
    }
}

Results of the:

a的值为: 1
a的值为: 2
a的值为: 3
a的值为: 4
a的值为: 5
a的值为: 6
a的值为: 7
a的值为: 8
a的值为: 9

Combined with the above code, we explain the workflow of the while loop in the form of a flowchart:

do-while loop

A do-while loop is exactly similar to a while loop, the only difference between the two is that a do-while loop executes the statement at least once.

Because it starts with the do keyword, the Boolean expression appears at the end of the loop.

The syntax of a do-while loop is:

do{
		//循环体正文
}while(条件表达式);

Note that because the do while statement does not end with braces, it ends with a semicolon.

Let's go directly to the code:

package com.test.javaroads.loop;

/**
 * @author: javaroads
 * @date: 2022/12/9 15:49
 * @description: Do-While循环
 */
public class DoWhileLoop {

    public static void main(String[] args) {

        int a = 1;
        do {
            System.out.println("a的值为: " + a);
            a++;
        } while (a < 10);
    }
}

Results of the:

a的值为: 1
a的值为: 2
a的值为: 3
a的值为: 4
a的值为: 5
a的值为: 6
a的值为: 7
a的值为: 8
a的值为: 9

The process is similar to while.

Summarize

This article explains the loop statements in Java: for, while and do-while, and uses the combination of code and flowchart.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132269485