1.逻辑运算符
#include <iostream>
using namespace std;
int main()
{
int a = 10;
cout << !a << endl;
cout << !!a << endl; //这也是一种简单的非零数字转换成1的方法
cout << (a && b) << endl; //不打括号报错
cout << (a || b) << endl; //不打括号报错
system("pause");
return 0;
}
2.选择语句
#include <iostream>
using namespace std;
int main()
{
while (1)
{
int score;
cout << "请输入一个分数" << endl;
cin >> score;
cout << "你输入的分数是" << score << endl;
if (100<score && score<150) //不能写为if(100<score<150);这样设置条件不合规
{
cout << "恭喜你考试重本" << endl;
}
else if(150<score && score<200)
{
cout << "成功被清华录取" << endl;
}
else if (score > 200)
{
cout << "你狗日的作弊被捕" << endl;
}
else
{
cout << "很遗憾你没考上重本" << endl;
}
}
system("pause");
return 0;
}
3.switch语句
#include <iostream>
using namespace std;
int main()
{
int score = 0;
cout << "请给电影打个合适的分数" << endl;
cin >> score;
cout << "你打的分数是" << score << endl;
switch (score) //只能是整形或字符型
{
case 10: cout << "经典电影" << endl;
break;
case 9: cout << "非常好看" << endl;
break;
case 8: cout << "好看" << endl;
break;
default:cout << "一般" << endl;
break;
}
system("pause");
return 0;
}