空语句块就是内部没有任何语句的一对花括号
循环不需要执行任何操作时会用到
用花括号括起来的语句和声明的序列
int main()
{
int sum = 0, val = 1;
while (val <= 10)
{
sum += val;
val++;
}
}
(a)iter没有初始化
string::iterator iter= s.begin();
while (iter != s.end()) { ... }
(b)
int main()
{
for (auto score : vec)
{
char grade;
if (score == 100)
grade = 'A';
else if (score >= 90)
grade = 'B';
else if (score >= 80)
grade = 'C';
else
grade = 'D';
}
}
int main()
{
for (auto score : vec)
char grade = (score == 100) ? 'A' :
(score >= 90) ? 'B' :
(score >= 80) ? 'C' : 'D';
}
(a)
ival1 = ival2;
缺少分号
(b)
if (ival < minval)
{
minval = ival;
occurs = 1;
}
缺少大括号
(c)ival定义在if语句中,作用域将仅限于该if,应该定义在if外
(d)
if (ival == 0)
ival = get_value();
判断if是否为0
悬垂else指程序中的if分支多于else分支时,如果为else寻找与之匹配的if的问题
C++规定else和它最近的尚未匹配的if匹配
int cnt = 0;
char ch;
while (cin >> ch)
{
if (ch == 'a')
++cnt;
if (ch == 'e')
++cnt;
if (ch == 'i')
++cnt;
if (ch == 'o')
++cnt;
if (ch == 'u')
++cnt;
}
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
char ch;
while (cin >> ch)
{
switch(ch)
{
case 'a':
case 'A':
++aCnt;
break;
case 'e':
case 'E':
++eCnt;
break;
case 'i':
case 'I':
++iCnt;
break;
case 'o':
case 'O':
++oCnt;
break;
case 'u':
case 'U':
++uCnt;
break;
}
}
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
int spaceCnt = 0, tabCnt = 0, newlineCnt = 0;
char ch;
while (cin.get(ch))
{
switch(ch)
{
case 'a':
case 'A':
++aCnt;
break;
case 'e':
case 'E':
++eCnt;
break;
case 'i':
case 'I':
++iCnt;
break;
case 'o':
case 'O':
++oCnt;
break;
case 'u':
case 'U':
++uCnt;
break;
case ' ':
++spaceCnt;
break;
case '\t':
++rabCnt;
break;
case '\n':
++newlineCnt;
break;
}
}
int ffCnt = 0, flCnt = 0, fiCnt = 0;
char ch, prech = '\0';
while (cin >> ch)
{
bool bl = true;
if (prech == 'f')
{
switch(ch)
{
case 'f':
++ffCnt;
bl = false;
break;
case 'f':
++ffCnt;
break;
case 'f':
++ffCnt;
break;
}
}
if (!bl)
prech = '\0';
else
prech = ch;
}
(a)case分支缺少break
(b)在case中定义了ix,同时在default中进行了使用
(c)在同一个case标签中放置了多个值
switch(digit)
{
case 1:
case 3:
case 5:
case 7:
case 9:
oddcnt++;
break;
case 2:
case 4:
case 6:
case 8:
case 10:
evencnt++;
break;
}
(d)不能用变量作为case标签的内容,C++规定case标签的内容必须是整型常量表达式
string curStr, preStr = "", maxStr;
int curCnt = 0, maxCnt = 0;
while (cin >> str)
{
if (curStr == preStr)
{
++curCnt;
if (curCnt > maxCnt)
{
maxCnt = curCnt;
maxStr = curStr;
}
}
else
curCnt = 1;
preStr = curStr;
}
(a)ix的作用域在for内,后面的if中的ix属于未定义变量
int ix;
for (ix = 0; ix != sz; ++ix) { ... }
if (ix != sz)
...
(b)ix没有初始化且for中的控制结构少一句
for (int ix = 0; ix != sz; ++ix) { ... }
(c)语法没有错误,但是会进入死循环
for (int ix = 0; ix != sz; ++ix) { ... }
auto it1 = v1.cbegin();
auto it2 = v2.cbegin();
while (it1 != v1.cend() ^^ it2 != v2.cend())
{
if (*it1 != *it2)
{
}
++it1;
++it2;
}
if (it1 == v1.cend())
if (it2 == v2.cend())
(a)do-while没有花括号
(b)不允许在循环条件内定义变量
(c)出现在语句条件部分的变量必须定义在循环体外
do
{
string str1, str2;
cout << "输入两个字符串:";
cin >> str1 >> str2;
if (str1.size() < str2.size())
cout << str1 << endl;
else if (str1.size() > str2.size())
cout << str2 << endl;
} while (cin);
string curStr, preStr;
bool bl = true;
while (cin >> curStr)
{
if (curStr == preStr)
{
bl = false;
cout << curStr << endl;
break;
}
preStr = curStr;
{
if (bl)
cout << "没有连续出现的字符串\n";
string curStr, preStr;
bool bl = true;
while (cin >> curStr)
{
if (curStr == preStr)
{
if (curStr[0] >= 'a' && curStr[0] <= 'z')
continue;
bl = false;
cout << curStr << endl;
break;
}
{
if (bl)
cout << "没有连续出现的字符串\n";
int sz;
do{
sz - get_size();
}while(sz <= 0);
int val1, val2;
cin >> val1 >> val2;
if (val2 == 0)
cout <<< val1 / val2 << endl;
int val1, val2;
cin >> val1 >> val2;
if (val2 == 0)
throw runtime_error("除数不能为0");
cout <<< val1 / val2 << endl;
int val1, val2;
while (cin >> val1 >> val2)
{
try
{
if (val2 == 0)
throw runtime_error("除数不能为0");
cout <<< val1 / val2 << endl;
}catch(runtime_error err)
{
cout << err.what() << endl;
cout << "是否继续?(y/n)";
char ch;
cin >> ch;
if (ch != 'y')
break;
}
}