Home >Backend Development >PHP Tutorial >The importance of break, continue and return in programs_PHP Tutorial
First: The break statement is usually used in loop statements and switch statements. When the break statement is used in do-while, for, while loop statements, it can cause the program to terminate the loop and execute the statements after the loop. Usually the break statement It is always connected with the if statement, that is, it will break out of the loop when the condition is met.
For example: main() { int i=0; char c; while(1) /*Set loop*/ { c='
Note:
1) The break statement does not work on if-else conditional statements. www.2cto.com
2) In a multi-level loop, a break statement only jumps one level outward.
Second: The function of the continue statement is to skip the remaining statements in the loop and force the execution of the next loop. The continue statement is only used in loop bodies such as for, while, do-while, etc., and is often used together with if conditional statements to speed up loops. For example: main() { char c; while(c!=13) /*Loop if it is not a carriage return*/ { c=getch(); if(c==0X1B) continue; /*No output if the Esc key is pressed Then proceed to the next loop */ printf("%cn", c); } } In fact, continue skips one loop and the subsequent statements and proceeds to the next loop.
Third: The return statement returns the value of the function to the calling function. For example: int max(int a,int b) { if(a>b)return a; else return b; } This function returns the maximum value between a and b; the general form of the return statement is: return expression or : return (expression)
Excerpted from chaojie2009’s column