Team LiB
Previous Section Next Section

6.11. The continue Statement

6.11. continue 语句

A continue statement causes the current iteration of the nearest enclosing loop to terminate. Execution resumes with the evaluation of the condition in the case of a while or do while statement. In a for loop, execution continues by evaluating the expression inside the for header.

continue 语句导致最近的循环语句的当次迭代提前结束。对于 whiledo while 语句,继续求解循环条件。而对于 for 循环,程序流程接着求解 for 语句头中的 expression 表达式。

For example, the following loop reads the standard input one word at a time. Only words that begin with an underscore will be processed. For any other value, we terminate the current iteration and get the next input:

例如,下面的循环每次从标准输入中读入一个单词,只有以下划线开头的单词才做处理。如果是其他的值,终止当前循环,接着读取下一个单词:

     string inBuf;
     while (cin >> inBuf && !inBuf.empty()) {
             if (inBuf[0] != '_')
                  continue; // get another input
             // still here? process string ...
     }

A continue can appear only inside a for, while, or do while loop, including inside blocks nested inside such loops.

continue 语句只能出现在 forwhile 或者 do while 循环中,包括嵌套在这些循环内部的块语句中。

Exercises Section 6.11

Exercise 6.21:

Revise the program from the last exercise in Section 6.10 (p. 213) so that it looks only for duplicated words that start with an uppercase letter.

修改第 6.10 节最后一个习题的程序,使得它只寻找以大写字母开头的连续出现的单词。


Team LiB
Previous Section Next Section