CH03_运算符
算术运算符
作用:用于处理四则运算
示例:
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 3;
cout << a + b << endl;//13
cout << a - b << endl;//7
cout << a * b << endl;//30
cout << a / b << endl;//3
cout << a % b << endl;//1
cout << a++ << endl;//10
cout << ++a << endl;//12
cout << a << endl;//12
cout << b-- << endl;//3
cout << --b << endl;//1
cout << b << endl;//1
system("pause");
return 0;
}
赋值运算符
作用:用于将表达式的值赋给变量
示例:
#include <iostream>
using namespace std;
int main() {
int num1 = 10, num2 = 3;
num1 += 5;
cout << num1 << endl;//15
num1 -= 3;
cout << num1 << endl;//12
num1 *= 2;
cout << num1 << endl;//24
num1 /= 3;
cout << num1 << endl;//8
num1 %= 2;
cout << num1 << endl;//0
system("pause");
return 0;
}
比较运算符
作用:用于表达式的比较,并返回一个真值或假值
示例:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "请输入你的年龄:" << endl;
cin >> num;
cout << (num > 18) << endl;
cout << (num < 18) << endl;
cout << (num >= 18) << endl;
cout << (num <= 18) << endl;
cout << (num == 18) << endl;
cout << (num != 18) << endl;
system("pause");
return 0;
}
逻辑运算符
作用:用于判断表达式,返回bool型的值
示例:
#include <iostream>
using namespace std;
int main() {
int num1=10,num2=5,num3=8;
cout << (num1 > num2 && num1 > num3) << endl;
cout << (num1 > num2 || num2 > num3) << endl;
cout << (!num1 > num2) << endl;
system("pause");
return 0;
}
标签:cout,num2,示例,int,CH03,运算符,num1
From: https://www.cnblogs.com/kaige-chen/p/18242667