作用:用于执行代码的运算
运算符类型作用
算术运算符用于处理四则运算赋值运算符用于将表达式的值赋给变量比较运算符用于表达式的比较,并返回一个真值或假值逻辑运算符用于根据表达式的值返回真值或假值#include<iostream>using namespace std;int main1(){//加减乘除int a1 = 10;int b1 = 3;cout << a1 + b1 << endl;cout << a1 - b1 << endl;cout << a1 * b1 << endl;cout << a1 / b1 << endl; //两个整数相除,结果依然是整数,将小数部分去除int a2 = 10;int b2 = 20;cout << a2 / b2 << endl;int a3 = 10;int b3 = 0;//cout << a3 / b3 << endl; //错误,除数不可以为0double x1 = 0.5;double x2 = 0.22;cout << x1 / x2 << endl;//取模int c1 = 10;int d1 = 3;cout << c1 % d1 << endl;int c2 = 10;int d2 = 20;cout << c2 % d2 << endl;int c3 = 10;int d3 = 0;//cout << c3 % d3 << endl; //错误,取模除数不可以为0double x3 = 3.14;double x4 = 1.1;//cout << x3 % x4 << endl; //错误,不支持小数取模运算//1.前置递增int m = 10;++m;cout << "m = " << m << endl;//2.后置递增int n = 10;n++;cout << "n = " << n << endl;//3.前置和后置的区别//前置递增:先让变量+1,后进行表达式运算int m2 = 10;int n2 = ++m2 * 10;cout << "m2 = " << m2 << endl;cout << "n2 = " << n2 << endl;//后置递增:先进行表达式运算,后让变量+1int m3 = 10;int n3 = m3++ * 10;cout << "m3 = " << m3 << endl;cout << "n3 = " << n3 << endl;system("pau");return 0;}
a=2;
*=乘等于a=2;a*=2;a=4;
/=除等于a=4;a/=2;a=2;%=模等于a=3;a%=2;a=1;#include<iostream>using namespace std;int main2(){//赋值运算符int a = 10;cout << "a = " << a << endl; //10// +=a += 2;cout << "a = " << a << endl; //12// -=a = 10;a -= 2;cout << "a = " << a << endl; //8// *=a = 10;a *= 2;cout << "a = " << a << endl; //20// /=a = 10;a /= 2;cout << "a = " << a << endl; //5// %=a = 10;a %= 2;cout << "a = " << a << endl; //0system("pau");return 0;}
运算符术语示例结果==相等于4==30!=不等于4!=31<小于4<30>大于4>31<=小于等于4<=30>=大于等于4>=31
#include<iostream>us秋景诗句ing n五年级语文期末试卷amespace std;int main3(){//比较运算符// ==int a = 10;int b = 20;cout << (a == b) << endl;// !=cout << (a != b) << endl;// >cout << (a > b) << endl;// <cout << (a < b) << endl;// >=co飞机上能带化妆品吗ut << (a >= b) << endl;// <=cout << (a <= b) << en济南世纪园dl;system("pau");return 0;}
运算符术语示例结果!非!a如果a为假,则!a为真;如果a为真,则!a为假。&&与a&&b如果a和b都为真,则结果为真,否则为假。||或a||b如果a或b有一个为真,则结果为真;二者都为假时,结果为假。
#include<iostream>using namespace std;int main4(){//逻辑运算符 非 !int a = 10;cout << !a << endl; //0cout << !!a << endl; //1//逻辑运算符 与 &&int c = 10;int d = 10;cout << (c && d) << endl; // 1c = 0;d = 10;cout << (c && d) << endl; // 0c = 10;d = 0;cout << (c && d) << endl; // 0c = 0;d = 0;cout << (c && d) << endl; // 0//逻辑运算符 或 ||int e = 10;int f = 10;cout << (e || f) << endl; // 1e = 0;f = 10;cout << (e || f) << endl; // 1e = 10;f = 0;cout << (e || f) << endl; // 1e = 0;f = 0;cout << (e || f) << endl; // 0system("青睐的意思pau");return 0;}
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注www.887551.com的更多内容!
本文发布于:2023-04-03 22:12:56,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/a7d693b880df94b0a4f4c75b3e9c2eea.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:手把手带你学习C++的运算符.doc
本文 PDF 下载地址:手把手带你学习C++的运算符.pdf
留言与评论(共有 0 条评论) |