1 // 30-逻辑表达式(逻辑运算符).cpp: 定义控制台应用程序的入口点。
2 //
3
4 #include "stdafx.h"
5 #include <iostream>
6 #include <climits>
7 #include <array>
8 #include <math.h>
9 #include <string>
10 using namespace std;
11
12 int main()
13 {
14 // || 或
15 int vip = 1;
16 if (vip == 1 || vip == 2)
17 {
18 cout << "获得优惠";
19 }
20
21 bool res1 = true || false;
22 bool res2 = false || true;
23 bool res3 = false || false;
24 bool res4 = true || true;
25 cout << res1 << " "<<res2 << " "<<res3 << " "<<res4 << endl;//1 1 0 1
26
27 //&& 与
28 int age = 20;
29 if (age >= 18 && age <= 30)
30 {
31 cout << "你是青年" << endl;
32 }
33
34 bool res5 = 4 == 4 && 3 == 3; //只有两边同时为true时才为true,其他的都是false
35 bool res6 = 8 == 8 && 6 == 3;
36 bool res7 = 5 > 4 && 3 > 9;
37 bool res8 = 4 > 8 && 5 < 10;
38 bool res9 = 3 < 9 && 2 > 9;
39 cout << res1 << " " << res2 << " " << res3 << " " << res4 << " " << res5 << endl;//1 1 0 1 1
40
41 //! 取反
42 cout << !true << endl; //0
43 cout << !(10 > 9) << endl;//0
44
45 //?: 条件运算符、
46 //表达式1 ? 表达式2 : 表达式3 ; 当表达式1结果为true时,执行表达式2,当表达式1结果为false时,执行表达式3.
47 int i = true ? 100 : 90;
48 cout << i << endl;
49
50 int vvip = 1;
51 string res = vvip == 1 ? "有优惠" : "正常价格";
52 cout << res << endl; //返回 有优惠
53
54 int vvvip = 1;
55 vvvip == 1 ? cout<<"有优惠" : cout << "正常价格"; //除了接语句外还可以接表达式
56
57 int t;
58 cin >> t;
59 return 0;
60 }