C++的运算符你真的了解吗
短信预约 -IT技能 免费直播动态提醒
前言
运算符的作用:用于执行代码的运算
主要有:
1 算术运算符
用于处理四则运算
对于前置递增:将递增运算前置,使变量先加一,再进行表达式运算。
对于后置递增:将递增运算后置,使变量先进行表达式运算,再加一。
#include<iostream>
using namespace std;
int main()
{
//1.前置递增:先加一,再进行表达式运算
int a = 10;
int b = ++a * 10;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
//2.后置递增:先进行表达式运算,再加一
int c = 10;
int d = c++ * 10;
cout << "c = " << c << endl;
cout << "d = " << d << endl;
system("pause");
return 0;
}
2 赋值运算符
#include<iostream>
using namespace std;
int main1()
{
//赋值运算符
int a = 10;
int b = 2;
cout << "a = " << a << endl;
//+=
a = 10;
a += b;
cout << "a = " << a << endl;
//-=
a = 10;
a -= b;
cout << "a = " << a << endl;
//*=
a = 10;
a *= b;
cout << "a = " << a << endl;
// /=
a = 10;
a /= b;
cout << "a = " << a << endl;
// %=
a = 10;
a %= b;
cout << "a = " << a << endl;
system("pause");
return 0;
}
3 比较运算符
#include<iostream>
using namespace std;
int main()
{
cout << (4 == 3) << endl;
cout << (4 != 3) << endl;
cout << (4 < 3) << endl;
cout << (4 > 3) << endl;
cout << (4 >= 3) << endl;
cout << (4 <= 3) << endl;
system("pause");
return 0;
}
4 逻辑运算符
#include<iostream>using namespace std;int main(){int a = 5;// 逻辑运算符 非cout << !a << endl;cout << !!a << endl;// 逻辑运算符 与int b = 0;int c = 3;cout << (a && b) << endl;cout << (a && c) << endl;//逻辑运算符 或cout << (!a || b) << endl;cout << (a || c) << endl;system("pause");return 0;}#include<iostream>
using namespace std;
int main()
{
int a = 5;
// 逻辑运算符 非
cout << !a << endl;
cout << !!a << endl;
// 逻辑运算符 与
int b = 0;
int c = 3;
cout << (a && b) << endl;
cout << (a && c) << endl;
//逻辑运算符 或
cout << (!a || b) << endl;
cout << (a || c) << endl;
system("pause");
return 0;
}
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341