程序流转结构
1、选择结构
if语句
选择结构 if语句
#include <iostream>;
using namespace std;
int main()
{
//逻辑运算符 非 !
int a = 10;
cout << !a << endl;
cout << !!a << endl;
//逻辑运算符 && 与
//逻辑运算符 || 或
a = 0;
int b = 11;
cout << (a && b) << endl;
// 选择结构 单行if语句
int score = 100;
if (score > 200)
{
cout << "恭喜您" << endl;
}
//多行if语句
if (score > 200)
{
cout << "恭喜您" << endl;
}
else
{
cout << "非常遗憾" << endl;
}
//多条件if语句
if (score > 300)
{
cout << "恭喜您" << endl;
}
else if (score > 400)
{
cout << "恭喜您" << endl;
}
else
{
cout << "非常遗憾" << endl;
}
//嵌套if
//三目运算符
// 将m和n做比较,将变量大的值赋值给变量q
int m = 10;
int n = 20;
int q = 0;
q = (m > n ? m : n);
//在c++中三目运算符返回的是变量,可以继续赋值
(m > n ? m : n) = 100;
cout << "n=" << n << endl;
cout << "m=" << m << endl;
//switch 语句,执行多条件分支语句
system("pause");
return 0;
}
0
1
0
非常遗憾
非常遗憾
n=100
m=10
请按任意键继续. . .
switch语句
//switch 语句,执行多条件分支语句
// 缺点,判断时候 只能是整型或者字符型,不可以是一个区间
// 优点 结构清晰,执行效率高
//给电影进行打分
//10-9 经典
//8-7 非常好
// 5、6 一般
//5以下,烂片
int test3()
{
int score = 10;
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;
}