注意这个程序中控制变量x在for结构首部以外定义。这是由于咱们要在循环体中和循环执行完毕以后使用这个控制变量。ios
continue语句在while、for或do/while结构中执行时跳过该结构体的其他语句,进入下一轮循环。在while和do/while结构中,循环条件测试在执行continue语句以后当即求值。在for结构中,执行递增表达式,而后进行循环条件测试。前面曾介绍过.while结构能够在大多数状况下取代for结构。但若是while结构中的递增表达式在continue语句以后,则会出现例外。这时,在测试循环条件以前没有执行递增,而且while与for的执行方式是不一样的。图2.27在for结构中用continue语句跳过该结构的输出语句,进入下一轮循环。程序员
// Fig.2.26:fig02 26.cpp // Usinq the break statement in a for structure #include< ostream.h> int main() { // x declared here so it can be used after the loop int x; for ( x = 1; x <= 10; x++ ) { if { x == 5 ) break; // break loop only if x is 5 cout << x <<" "; } cout << " Broke out of loop at x of" << x << endl; return O; }
输出结果:编程
l 2 3 4oop
Broke out of loop at x of 5性能
图2.26 for重复结构中的break语句测试
// Fig. 2.27: figO2_OT.cpp // Using the continue statement in a for structure #include < iostream.h> int main() { for ( iht x = 1; x <- 10; x++ } { if {x==5) continue; // skip remaining code in loop // only if x is 5 cout << x <<" "; } cout << "\nUsed continue to skip printing the value 5" << endl; return O; }
l 2 3 4 5 6 7 9 9 10spa
USed continue to skip printing the value 5code
图 2.27 在for结构用continue语句ip
有些程序员认为break和continue会破坏结构化编程。因为这些语句能够经过后面要介绍的结构化编程方法实现,所以这些程序员不用break和continue。rem
正确使用break和continue语句能比后面要介绍的经过结构化编程方法的实现速度更快。
达到高质量软件工程与实现最佳性能软件之间有必定冲突。一般,要达到一个目标,就要牺牲另外一个目标。
2016考研复试技巧http://www.kyjxy.com/fushi/zhinan/
考研专硕备考资料http://www.kyjxy.com/zhuanshuo/
考研院校政策http://www.kyjxy.com/yuanxiao/zhengce/