赋值运算符
**作用:**用于将表达式的值赋给变量
赋值运算符包括以下几个符号:
1 int main() { 2 3 //赋值运算符 4 5 // = 6 int a = 10; 7 a = 100; 8 cout << "a = " << a << endl; 9 10 // += 11 a = 10; 12 a += 2; // a = a + 2; 13 cout << "a = " << a << endl; 14 15 // -= 16 a = 10; 17 a -= 2; // a = a - 2 18 cout << "a = " << a << endl; 19 20 // *= 21 a = 10; 22 a *= 2; // a = a * 2 23 cout << "a = " << a << endl; 24 25 // /= 26 a = 10; 27 a /= 2; // a = a / 2; 28 cout << "a = " << a << endl; 29 30 // %= 31 a = 10; 32 a %= 2; // a = a % 2; 33 cout << "a = " << a << endl; 34 35 system("pause"); 36 37 return 0; 38 }
比较运算符
**作用:**用于表达式的比较,并返回一个真值或假值
比较运算符有以下符号:
1 int main() { 2 3 int a = 10; 4 int b = 20; 5 6 cout << (a == b) << endl; // 0 7 8 cout << (a != b) << endl; // 1 9 10 cout << (a > b) << endl; // 0 11 12 cout << (a < b) << endl; // 1 13 14 cout << (a >= b) << endl; // 0 15 16 cout << (a <= b) << endl; // 1 17 18 system("pause"); 19 20 return 0; 21 }
逻辑运算符
**作用:**用于根据表达式的值返回真值或假值
逻辑运算符有以下符号:
1 //逻辑运算符 --- 非 2 int main() { 3 4 int a = 10; 5 6 cout << !a << endl; // 0 7 8 cout << !!a << endl; // 1 9 10 system("pause"); 11 12 return 0; 13 }
1 //逻辑运算符 --- 与 2 int main() { 3 4 int a = 10; 5 int b = 10; 6 7 cout << (a && b) << endl;// 1 8 9 a = 10; 10 b = 0; 11 12 cout << (a && b) << endl;// 0 13 14 a = 0; 15 b = 0; 16 17 cout << (a && b) << endl;// 0 18 19 system("pause"); 20 21 return 0; 22 }
1 //逻辑运算符 --- 或 2 int main() { 3 4 int a = 10; 5 int b = 10; 6 7 cout << (a || b) << endl;// 1 8 9 a = 10; 10 b = 0; 11 12 cout << (a || b) << endl;// 1 13 14 a = 0; 15 b = 0; 16 17 cout << (a || b) << endl;// 0 18 19 system("pause"); 20 21 return 0; 22 }