Java basics: control statements break, continue, return

In the last two articles, we introduced the loop statements for, while and do-while and the conditional statements if else and switch case:

loop statement

Conditional statements

breakIn addition to the basic grammar in these two statements, I believe that everyone will encounter , continue, and keywords when using them return. So what are these three keywords? What's the use? This article will give you a detailed introduction.

break

The break statement is the loop control statement used to terminate the loop, once a break statement is encountered in the loop, the loop iteration stops there and control returns from the loop to the first statement after the loop immediately.

In Java, break is mainly used for:

  • Terminated in a switch statement.
  • exit the loop.

The following figure describes the execution flow of the break statement:

Let's demonstrate it in code:

package com.test.javaroads.loop;

/**
 * @author: javaroads
 * @date: 2022/12/11 17:46
 * @description:
 */
public class BreakLoop {

    public static void main(String[] args) {

        int a = 1;

        // while break
        while (a < 5) {
            System.out.println("【while break】a的值为: " + a);
            a++;
            break;
        }

        // do while break
        do {
            System.out.println("【do while break】a的值为: " + a);
            a++;
            break;
        } while (a == 5);

        // for break
        for (int i = 0; i <= 10; i++) {
            if (i == 5) {
                System.out.println("【for break】i的值为 = " + i);
                break;
            }
        }
    }
}

Results of the:

【while break】a的值为: 1
【do while break】a的值为: 2
【for break】i的值为 = 5

Using break, we can force the loop to terminate immediately, bypassing the conditional expression and any remaining code in the loop body.

Note: When break is used in a set of nested loops, only the innermost loop will be broken out.

continue

The continue statement in Java is used to control the flow of code, to terminate one iteration of a loop and start the next iteration by skipping the instruction following the loop, i.e. "continue" allows you to skip a particular iteration of a loop without stopping it completely.

For example, we want to execute a loop in some code, but at a certain step, we want to skip the following code and want to go to the next iteration, we can use the "continue" statement. Our goal in doing this might be overall better complexity or getting the code flow right.

When using a for loop, the "continue" statement forces control to immediately pass to the update statement. Control is quickly transferred to a Boolean expression in a while loop or do-while loop.

The execution flow of the continue statement is shown in the following figure:

Let's demonstrate it in code:

package com.test.javaroads.loop;

/**
 * @author: javaroads
 * @date: 2022/12/11 17:46
 * @description:
 */
public class ContinueLoop {

    public static void main(String[] args) {

        int a = 1;

        // while continue
        while (a < 5) {
            System.out.println("【while continue】a的值为: " + a);
            a++;
            continue;
        }

        // do while continue
        do {
            System.out.println("【do while continue】a的值为: " + a);
            a++;
            continue;
        } while (a == 5);

        // for continue
        for (int i = 0; i <= 10; i++) {
            if (i == 5) {
                System.out.println("【for continue】i的值为 = " + i);
                continue;
            }
        }
    }
}

Results of the:

【while continue】a的值为: 1
【while continue】a的值为: 2
【while continue】a的值为: 3
【while continue】a的值为: 4
【do while continue】a的值为: 5
【for continue】i的值为 = 5

return

In java, the return statement is used to terminate a method with or without a value. The return statement transfers execution control to the calling function. This means that the return statement transfers execution control from the called function to the calling function by carrying a value.

Java allows return statements with and without return type methods.

Similarly, let's demonstrate it with code:

        int a = 1;

        // while return
        while (a < 5) {
            System.out.println("【while return】a的值为: " + a);
            a++;
            return;
        }

        // do while return
        do {
            System.out.println("【do while return】a的值为: " + a);
            a++;
            return;
        } while (a == 5);

        // for return
        for (int i = 0; i <= 10; i++) {
            if (i == 5) {
                System.out.println("【for return】i的值为 = " + i);
                return;
            }
        }

In Java, the return statement is used with methods with and without return types. The return statement is required for methods with a return type, and optional for methods without a return type.

When the return statement is used with a return type, it takes the value of the return type. However, when it's used without a return type, it doesn't carry any value. Instead, just transfer control of the execution.

Guess you like

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