C++ 06 语句

简单语句

分号结束

复合语句

代码块,判断、循环等待

语句作用域

语句内部

if、switch、default、while、for、do-while、break、continue、goto

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main()
{
int num;
while (cin >> num)
{
if (num == 886) break;
if (num == 520) continue;
if (num == 69) goto ok;
if (num % 2 == 0)
cout << num << " is 偶数 " << endl;
else
cout << num << " is 奇数 " << endl;

}
ok:
cout << num << " is ok " << endl;
return 0;
}

异常处理 throw、try、catch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

double divide(int n1, int n2);

int main()
{
try
{
double n3 = divide(1, 0);
cout << "相除得:" << n3 << endl;
}
catch (std::runtime_error err)
{
cout << "相除异常:" << err.what() << endl;
}
}

double divide(int n1, int n2)
{
if (n2 == 0) throw std::runtime_error("被除数不能为0!");
return (double)n1 / n2;
}

推荐文章