No. | Contents |
1 | 【C++】基础知识 - HelloWorld,注释,变量,常量,关键字,标识符 |
2 | 【C++】基础知识 - 数据类型,sizeof,转义字符,数据输入 |
3 | 【C++】基础知识 - 算术运算符,赋值运算符,比较运算符,逻辑运算符 |
4 | 【C++】基础知识 - 顺序结构,选择结构,循环结构 |
文章目录
- 1. 顺序结构
- 2. 选择结构
- 2.1 单行格式 if 语句
- 2.2 多行格式 if 语句
- 2.3 多条件的 if 语句
- 2.4 嵌套 if 语句
1. 顺序结构
程序默认就是顺序结构执行。
2. 选择结构
2.1 单行格式 if 语句
# include <iostream>
using namespace std;
int main()
{
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
if (score > 600) {
cout << "恭喜您,考上一本大学!" << endl;
}
system("pause");
return 0;
}
2.2 多行格式 if 语句
# include <iostream>
using namespace std;
int main()
{
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
if (score > 600) {
cout << "恭喜您,考上一本大学!" << endl;
}
else {
cout << "很遗憾,没有考上一本大学!" << endl;
}
system("pause");
return 0;
}
2.3 多条件的 if 语句
# include <iostream>
using namespace std;
int main()
{
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
if (score > 600) {
cout << "恭喜您,考上一本大学!" << endl;
}
else if (score > 500) {
cout << "恭喜您,考上二本大学!" << endl;
}
else if (score > 400) {
cout << "恭喜您,考上三本大学!" << endl;
}
else {
cout << "很遗憾,没有考上本科大学!" << endl;
}
system("pause");
return 0;
}
2.4 嵌套 if 语句
# include <iostream>
using namespace std;
int main()
{
int score = 0;
cout << "请输入一个分数:" << endl;
cin >> score;
if (score > 600) {
cout << "恭喜您,考上一本大学!" << endl;
if (score > 700) {
cout << "您能考入北京大学!" << endl;
}
else if (score > 650) {
cout << "您能考入清华大学!" << endl;
}
else {
cout << "您能考入人民大学!" << endl;
}
}
else if (score > 500) {
cout << "恭喜您,考上二本大学!" << endl;
}
else if (score > 400) {
cout << "恭喜您,考上三本大学!" << endl;
}
else {
cout << "很遗憾,没有考上本科大学!" << endl;
}
system("pause");
return 0;
}