Home  >  Article  >  Backend Development  >  The importance of break, continue and return in programs_PHP Tutorial

The importance of break, continue and return in programs_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:49:13916browse

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

http://www.bkjia.com/PHPjc/478381.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478381.htmlTechArticleFirst: The break statement is usually used in loop statements and switch statements. When the break statement is used in do-while, When in a for or while loop statement, the program can terminate the loop and execute the statement after the loop, by...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn