0
点赞
收藏
分享

微信扫一扫

❤️ 万字C++运算符大全讲解❤️


文章目录

  • ​​一、C++ 运算符​​
  • ​​二、算术运算符​​
  • ​​减法​​
  • ​​乘法​​
  • ​​除法​​
  • ​​余数​​
  • ​​自增​​
  • ​​自减​​
  • ​​三、赋值运算符​​
  • ​​加等于​​
  • ​​减等于​​
  • ​​乘等于​​
  • ​​除等于​​
  • ​​四、比较运算符​​
  • ​​等价符​​
  • ​​不等价​​
  • ​​大于​​
  • ​​小于​​
  • ​​大于等于​​
  • ​​小于等于​​
  • ​​五、逻辑运算符​​
  • ​​并且​​
  • ​​或者​​
  • ​​反转​​
  • ​​六.联系川川​​

一、C++ 运算符

运算符用于对变量和值执行操作。在下面的示例中,我们使用 + 运算符将两个值相加

#include <iostream>
using namespace std;

int main() {
int x = 15 + 20;
cout << x;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_c++
尽管+运算符经常用于将两个值相加,如上面的示例,但它也可用于将一个变量和一个值相加,或者将一个变量和另一个变量相加:

#include <iostream>
using namespace std;

int main() {
int sum1 = 10 + 5; // 150 (100 + 50)
int sum2 = sum1 + 25; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
cout <<"sum1和为:" <<sum1 << "\n";
cout << "sum2和为:"<<sum2 << "\n";
cout <<"sum3和为:"<< sum3;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_ios_02

二、算术运算符

上面已经演示了加法,后面开始演示其它的运算符。

减法

比如说计算5-3:

#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 3;
cout << x - y;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_c++_03

乘法

比如说计算5*3:

#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 3;
cout << x * y;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_#include_04

除法

比如说计算5除以3:

#include <iostream>
using namespace std;

int main() {
int x = 12;
int y = 3;
cout << x / y;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_c++_05

余数

比如求5除以2的余数:

#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 2;
cout << x % y;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_运算符_06

自增

对5自增一:

#include <iostream>
using namespace std;

int main() {
int x = 5;
++x;
cout << x;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_运算符_07

自减

对五自减一:

#include <iostream>
using namespace std;

int main() {
int x = 5;
--x;
cout << x;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_ios_08

三、赋值运算符

赋值运算符用于为变量赋值。在下面的示例中,我们使用赋值运算符 ( =) 将值10分配给名为x的变量:

#include <iostream>
using namespace std;

int main() {
int x = 10;
cout << x;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_c++_09
加法赋值运算符(+=)增加了一个值给变量:

#include <iostream>
using namespace std;

int main() {
int x = 10;
x += 5;
cout << x;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_运算符_10
所有赋值运算符的列表:
❤️ 万字C++运算符大全讲解❤️_ios_11
演示一部分常用的符号,其余希望大家自己操作一下。

加等于

#include <iostream>
using namespace std;

int main() {
int x = 5;
x += 3;
cout << x;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_运算符_12

减等于

#include <iostream>
using namespace std;

int main() {
int x = 5;
x -= 3;
cout << x;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_#include_13

乘等于

#include <iostream>
using namespace std;

int main() {
int x = 5;
x *= 3;
cout << x;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_运算符_14

除等于

#include <iostream>
using namespace std;

int main() {
double x = 5;
x /= 3;
cout << x;
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_ios_15

四、比较运算符

等价符

#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 3;
cout << (x == y); //返回0,因为x与y不等价
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_运算符_16

不等价

#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 3;
cout << (x != y); // 返回1,因为不等价
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_ios_17

大于

#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 3;
cout << (x > y); // 返回1因为x大于 y
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_#include_18

小于

与大于相反,不演示了

大于等于

#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 3;
cout << (x >= y); //返回1,因为5大于等于3
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_运算符_19

小于等于

与上面相反大于等于相反,不演示了。

五、逻辑运算符

并且

&& ,如果两个陈述都为真,则返回真。

#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 3;
cout << (x > 3 && y< 10); // 返回1因为x大于三,并且y小于 十
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_#include_20

或者

|| 满足其中一个情况就是真。

#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 3;
cout << (x > 3 || x < 4); // 返回真 (1) 因为其中一个条件为真(5 大于 3,但 5 不小于 4)
return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_运算符_21

反转

这个用得比较少吧,个人认为。反转结果,如果结果为真则返回假。

#include <iostream>
using namespace std;

int main() {
int x = 5;
int y = 3;
cout << (!(x > 3 && x < 10)); // 返回 false (0) 因为! (not) 用于反转结果

return 0;
}

演示:
❤️ 万字C++运算符大全讲解❤️_运算符_22

六.联系川川

群聊:813269919


举报

相关推荐

0 条评论