Team LiB
Previous Section Next Section

6.10. The break Statement

6.10. break 语句

A break statement terminates the nearest enclosing while, do while, for, or switch statement. Execution resumes at the statement immediately following the terminated statement. For example, the following loop searches a vector for the first occurrence of a particular value. Once it's found, the loop is exited:

break 语句用于结束最近的 whiledo whileforswitch 语句,并将程序的执行权传递给紧接在被终止语句之后的语句。例如,下面的循环在 vector 中搜索某个特殊值的第一次出现。一旦找到,则退出循环:

     vector<int>::iterator iter = vec.begin();
     while (iter != vec.end()) {
        if (value == *iter)
             break; // ok: found it!
        else
             ++iter; // not found: keep looking
     }// end of while
     if (iter != vec.end()) // break to here ...
         // continue processing

In this example, the break terminates the while loop. Execution resumes at the if statement immediately following the while.

本例中,break 终止了 while 循环。执行权交给紧跟在 while 语句后面的 if 语句,程序继续执行。

A break can appear only within a loop or switch statement or in a statement nested inside a loop or switch. A break may appear within an if only when the if is inside a switch or a loop. A break occurring outside a loop or switch is a compile-time error. When a break occurs inside a nested switch or loop statement, the enclosing loop or switch statement is unaffected by the termination of the inner switch or loop:

break 只能出现在循环或 switch 结构中,或者出现在嵌套于循环或 switch 结构中的语句里。对于 if 语句,只有当它嵌套在 switch 或循环里面时,才能使用 breakbreak 出现在循环外或者 switch 外将会导致编译时错误。当 break 出现在嵌套的 switch 或者循环语句中时,将会终止里层的 switch 或循环语句,而外层的 switch 或者循环不受影响:

     string inBuf;
     while (cin >> inBuf && !inBuf.empty()) {
         switch(inBuf[0]) {
         case '-':
             // process up to the first blank
             for (string::size_type ix = 1;
                         ix != inBuf.size(); ++ix) {
                   if (inBuf[ix] == ' ')
                        break; // #1, leaves the for loop
                   // ...
             }
             // remaining '-' processing: break #1 transfers control here
             break; // #2, leaves the switch statement
         case '+':
             // ...
         } // end switch
         // end of switch: break #2 transfers control here
     }  // end while

The break labeled #1 terminates the for loop within the hyphen case label. It does not terminate the enclosing switch statement and in fact does not even terminate the processing for the current case. Processing continues with the first statement following the for, which might be additional code to handle the hyphen case or the break that completes that section.

#1 标记的 break 终止了连字符('-')case 标号内的 for 循环,但并没有终止外层的 switch 语句,而且事实上也并没有结束当前 case 语句的执行。接着程序继续执行 for 语句后面的第一个语句,即处理连字符 case 标号下的其他代码,或者执行结束这个 casebreak 语句。

The break labeled #2 terminates the switch statement after handling the hyphen case but does not terminate the enclosing while loop. Processing continues after that break by executing the condition in the while, which reads the next string from the standard input.

#2 标记的 break 终止了处理连字符情况的 switch 语句,但没有终止 while 循环。程序接着执行 break 后面的语句,即求解 while 的循环条件,从标准输入读入下一个 string 对象。

Exercises Section 6.10

Exercise 6.19:

The first program in this section could be written more succinctly. In fact, its action could be contained entirely in the condition in the while. Rewrite the loop so that it has an empty body and does the work of finding the element in the condition.

本节的第一个程序可以写得更简洁。事实上,该程序的所有工作可以全部包含在 while 的循环条件中。重写这个循环,使得它的循环体为空,并找出满足条件的元素。

Exercise 6.20:

Write a program to read a sequence of strings from standard input until either the same word occurs twice in succession or all the words have been read. Use a while loop to read the text one word at a time. Use the break statement to terminate the loop if a word occurs twice in succession. Print the word if it occurs twice in succession, or else print a message saying that no word was repeated.

编写程序从标准输入读入一系列 string 对象,直到同一个单词连续出现两次,或者所有的单词都已读完,才结束读取。请使用 while 循环,每次循环读入一个单词。如果连续出现相同的单词,便以 break 语句结束循环,此时,请输出这个重复出现的单词;否则输出没有任何单词连续重复出现的信息。


Team LiB
Previous Section Next Section