Home > php教程 > php手册 > body text

break、continue 和return 在程序中的重要性

WBOY
Release: 2016-06-13 10:47:38
Original
1144 people have browsed it

第一:break语句通常用在循环语句和开关语句中,当break语句用于do-while、for、while循环语句中时,可使程序终止循环而执行循环后面的语句, 通常break语句总是与if语句联在一起,即满足条件时便跳出循环。

例如: main() { int i=0; char c; while(1) /*设置循环*/ { c='\0'; /*变量赋初值*/ while(c!=13&&c!=27) /*键盘接收字符直到按回车或Esc键*/ { c=getch(); printf("%c\n", c); } if(c==27) break; /*判断若按Esc键则退出循环*/ i++; printf("The No. is %d\n", i); } printf("The end"); }

注意:
1) break语句对if-else的条件语句不起作用。www.2cto.com

2) 在多层循环中, 一个break语句只向外跳一层。

第二:continue语句的作用是跳过循环本中剩余的语句而强行执行下一次循环。continue语句只用在for、while、do-while等循环体中,常与if条件语句一起使用,用来加速循环。 例如: main() { char c; while(c!=13) /*不是回车符则循环*/ { c=getch(); if(c==0X1B) continue; /*若按Esc键不输出便进行下次循环*/ printf("%c\n", c); } } 其实就是continue跳过一次循环以及后面的语句,进行下次循环。

第三: return语句是将函数的值返回主调函数。例如: int max(int a,int b) { if(a>b)return a; else return b; } 这个函数是返回a和b中的最大值; return 语句的一般形式为: return 表达式 或者为: return (表达式)

 

摘自 chaojie2009的专栏
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!