break语句使程序跳到Switch或者循环后面的语句处执行,意思就是终止整个循环。
continue的作用是使程序跳过循环体中余下的代码,并开始新一轮循环。
#include <iostream>
const int ArSize = 80;
int main()
{
using namespace std;
char line[ArSize];
int spaces = 0;
cout << "Enter a line of text.\n";
cin.get(line, ArSize);
cout << "Complete line.\n" << line << endl;
cout << "Line through first period.\n";
for (int i = 0; line[i] != '\0'; i++)
{
cout << line[i];
if (line[i] == '.')
break;
if (line[i] != ' ')
continue;
spaces++;
//虽然continue语句导致该程序跳过循环体的剩余部分,但是不会跳过循环的更新表达式。
//在for循环中,continue语句使程序直接跳到更新表达式(这里是i++),然后跳到测试表达式处。
//while循环中位于continue之后的更新表达式都将被跳过。
}
cout << "\n" << spaces << " spaces\n";
cout << "Done.\n";
return 0;
}
Enter a line of text.
yang guo hello.
Complete line.
yang guo hello.
Line through first period.
yang guo hello.
2 spaces
Done.