Home > Article > Web Front-end > What are the functions and differences between break and continue?
Function: break and continue are used to control the loop structure, mainly to stop the loop. The difference: the continue statement only ends this loop, not the entire loop, and can only be used in a loop statement; the break statement ends the entire loop process and no longer determines whether the conditions for executing the loop are established.
The functions of break and continue
break and continue are used to control loops Structural, mainly to stop the cycle.
The difference between break and continue
break is used to completely end a loop and jump out of the loop body. No matter what kind of loop, once a break is encountered in the loop body, the system will completely end the loop and start executing the code after the loop.
break can not only end the loop it is in, but also end its outer loop. At this time, a label needs to be followed immediately after break. This label is used to identify an outer loop.
var str = "hello"; for (var item of str){ if(item ==="l"){ break } console.log(item); // h e }
The function of continue is somewhat similar to break. The difference is that continue only terminates this cycle and then starts the next cycle.
It can be understood that continue means to skip the remaining statements in the current loop and execute the next loop.
var str = "hello"; for (var item of str){ if(item ==="l"){ continue } console.log(item); // h e o }
Summary of the difference:
The difference between the continue statement and the break statement is that the continue statement only ends this loop, rather than terminating the entire loop. The break statement ends the entire loop process, no longer determines whether the conditions for executing the loop are true, and executes the statements after the current loop. Moreover, continue can only be used in loop statements, that is, it can only be used in for, while and do...while. In addition, continue cannot be used in any statement.
So, again: continue cannot be used in switch unless switch is in the loop body. At this time, continue also represents the current loop that ends the loop body, and has nothing to do with switch.
For more programming-related knowledge, please visit: Programming Courses! !
The above is the detailed content of What are the functions and differences between break and continue?. For more information, please follow other related articles on the PHP Chinese website!