1.else if语句
四个条件是互斥的,只能有一个执行
2.代码实践
#include "iostream"
using namespace std;
int main()
{
/*
* if (条件)
* {}
* else if(条件)
* {}
* else if(条件)
* {}
* ..
* ..
* ..
* ..
* else
* {}
*/
bool xiaomei = false; // true 喜欢 false不喜欢
bool xiaotian = false;
bool xiaoqing = true;
if (xiaomei)
{
cout << "小美我也喜欢你!" << endl;
}else if(xiaotian)
{
cout << "小甜我也喜欢你!" << endl;
}else if(xiaoqing){
cout << "小青我也喜欢你!" << endl;
}
else {
cout << "小新我喜欢你!" << endl;
}
return 0;
}
3.总结
4.作业
# include "iostream"
using namespace std;
int main() {
int number = 3;
int guess;
cout << "请输入一个猜想的数字(1-10):" << endl;
cin >> guess;
if (guess > number) {
cout << "猜大了" << endl;
} else if (guess < number) {
cout << "猜小了" << endl;
} else {
cout << "猜对了" << endl;
}
return 0;
}