2.4 逻辑运算符

使用逻辑运算符

运算符 描述 示例
逻辑非 !expression
&& 逻辑与 expression1&&expression2
|| 逻辑或 expression1||expression2
 1 // Designers Network
 2 // Demonstrates logical operators
 3 
 4 #include <iostream>
 5 #include <string>
 6 using namespace std;
 7 
 8 int main() 
 9 {
10     string username;
11     string password;
12     bool success;
13 
14     cout << "	Game Designer's Network
";
15 
16     do
17     {
18         cout << "
Username: ";
19         cin >> username; 
20 
21         cout << "Password: ";
22         cin >> password; 
23 
24         if (username == "S.Meier" && password == "civilization")
25         {
26             cout << "
Hey, Sid.";
27             success = true;
28         }
29 
30         else if (username == "S.Miyamoto" && password == "mariobros")
31         {
32             cout << "
What's up, Shigeru?";
33             success = true;
34         }
35 
36         else if (username == "W.Wright" && password == "thesims")
37         {
38             cout << "
How goes it, Will?";
39             success = true;
40         }
41 
42         else if (username == "guest" || password == "guest")
43         {
44             cout << "
Welcome, guest.";
45             success = true;
46         }
47 
48         else
49         {
50             cout << "
Your login failed.";
51             success = false;
52         }
53     } while (!success);
54 
55     return 0;
56 }

每个成员必须输入用户名和密码来登录,如果登录成功,成员将收到对其个人的问候。若要作为游客访问等罗,用户只需要在用户名或密码提示的后面输入guest即可。

 不是成员或访客则无法登录!

 访客guest登录!

 看来今天有精英登陆了!!!

运算符的优先级

逻辑非!比逻辑与&&优先级高。逻辑与&&比逻辑或||优先级高。

如果希望对低优先级的运算符进行计算,可以使用括号。

使用冗余括号是一门艺术。

原文地址:https://www.cnblogs.com/wlyperfect/p/12400493.html