The difference between break, return and continue in switch and function

1、break

        break is to exit the switch statement directly. If there is a switch statement in the loop, the break statement just jumps out of the switch statement and has no effect on the loop body. break directly ends a loop, and the code in the loop body is no longer executed, and the code outside the loop body will be executed. break is used to end a loop, that is, jump out of the loop body and execute the code after the loop body. But only the closest layer of loop can be ended.

 Use break in while;

 

2、return

        return is to exit the function, that is, the statement after the switch statement block is not executed, the return keyword is not specially used to end the loop, the function of return is to end a method. When a method encounters a return statement, the method will be terminated. When the method ends, the loop will naturally end. No matter how deep the return is nested, the execution right returns to the place where the method is called.

3、continue 

        Terminate the current loop and continue to the next loop. The statement after continue will not be executed in the current loop. The next loop will continue to execute, and the statement outside the loop body will also be executed.

Guess you like

Origin blog.csdn.net/frelly01/article/details/128113565