首页 > 作文

手把手带你学习C++的运算符

更新时间:2023-04-03 22:12:58 阅读: 评论:0

目录
运算符01 算术运算符02 赋值运算符03 比较运算符04 逻辑运算符总结

运算符

作用:用于执行代码的运算

运算符类型

作用

算术运算符用于处理四则运算赋值运算符用于将表达式的值赋给变量比较运算符用于表达式的比较,并返回一个真值或假值逻辑运算符用于根据表达式的值返回真值或假值

01 算术运算符

运算符术语示例结果+正号+33–负号-3-3+加10+515–减10-55*乘10*550/除10/52%取模/取余10%31++前置递增a=2;b=++a;a=3;b=3;++后置递增a=2;b=a++;a=3;b=2;—前置递减a=2;b=–a;a=1;b=1;—后置递增a=2;b=a–;a=1;b=2;
#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;}

02 赋值运算符

运算符术语示例结果=赋值a=2;b=3;a=2;b=3;+=加等于a=0;a+=2;a=2;-=减等于a=5;a-=3;

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;}

03 比较运算符

运算符术语示例结果==相等于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;}

04 逻辑运算符

运算符术语示例结果!非!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 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图