Arithmetic operator +-x/%++/assignment operator=/comparison operator><=/logic operator!&&||

Arithmetic operator +-x/%++/assignment operator=/comparison operator><=/logic operator!&&||

Table of contents

1. Brief introduction

2. Arithmetic operators

1. + - x / Code:

2. Modulo% code:

3. Auto-increment ++ code:

3. Assignment operator

Fourth, the comparison operator

5. Logical operators

1. Logical NOT! Code:

2. Logical AND && Code:

3. Logical OR || Code:


1. Brief introduction

Organize some knowledge developed in C++, so that you can consult and use it in time when you encounter similar problems later.

This section introduces the operators used to execute code operations, including arithmetic operator +-x/%++/assignment operator=/comparison operator><=/logical operator !&&|| and precautions. If there are deficiencies, welcome to point out, or if you have a better method, please leave a message.
 

2. Arithmetic operators

Function: used to process four operations

Arithmetic operators include the following symbols:

operator the term example result
+ plus sign +3 3
- negative -3 -3
+ add 10 + 5 15
- reduce 10 - 5 5
* take 10 * 5 50
/ remove 10 / 5 2
% modulo (remainder) 10 % 3 1
++ Pre-increment a=2; b=++a; a=3; b=3;
++ post-increment a=2; b=a++; a=3; b=2;
pre-decrement a=2; b=–a; a=1; b=1;
post-decrement a=2; b=a–; a=1; b=2;

1. + - x / Code:

Note: In the division operation, the divisor cannot be 0

#include <iostream>
using namespace std;

int main() {

	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; //报错,除数不可以为0


	//两个小数可以相除
	double d1 = 0.5;
	double d2 = 0.25;
	cout << d1 / d2 << endl;

	system("pause");

	return 0;
}

2. Modulo% code:

Note: 1) Only integer variables can perform modulo operations; 2) The divisor cannot be 0

#include <iostream>
using namespace std;

int main() {

	int a1 = 10;
	int b1 = 3;

	cout << 10 % 3 << endl;

	int a2 = 10;
	int b2 = 20;

	cout << a2 % b2 << endl;

	int a3 = 10;
	int b3 = 0;

	//cout << a3 % b3 << endl; //取模运算时,除数也不能为0

	//两个小数不可以取模
	double d1 = 3.14;
	double d2 = 1.1;

	//cout << d1 % d2 << endl;

	system("pause");

	return 0;
}

3. Auto-increment ++ code:

Note: The pre-increment first performs ++ on the variable, and then calculates the expression, and the post-increment is the opposite

#include <iostream>
using namespace std;

int main() {

	//后置递增
	int a = 10;
	a++; //等价于a = a + 1
	cout << a << endl; // 11

	//前置递增
	int b = 10;
	++b;
	cout << b << endl; // 11

	//区别
	//前置递增先对变量进行++,再计算表达式
	int a2 = 10;
	int b2 = ++a2 * 10;
	cout << b2 << endl;

	//后置递增先计算表达式,后对变量进行++
	int a3 = 10;
	int b3 = a3++ * 10;
	cout << b3 << endl;

	system("pause");

	return 0;
}

3. Assignment operator

Role: used to assign the value of an expression to a variable

Assignment operators include the following symbols:

operator the term example result
= assignment a=2; b=3; a=2; b=3;
+= add equal to a=0; a+=2; a=2;
-= minus equal to a=5; a-=3; a=2;
*= multiply equal to a=2; a*=2; a=4;
/= divide equal to a=4; a/=2; a=2;
%= Modulus is equal to a=3; a%2; a=1;

code:

#include <iostream>
using namespace std;

int main() {

	//赋值运算符

	// =
	int a = 10;
	a = 100;
	cout << "a = " << a << endl;

	// +=
	a = 10;
	a += 2; // a = a + 2;
	cout << "a = " << a << endl;

	// -=
	a = 10;
	a -= 2; // a = a - 2
	cout << "a = " << a << endl;

	// *=
	a = 10;
	a *= 2; // a = a * 2
	cout << "a = " << a << endl;

	// /=
	a = 10;
	a /= 2;  // a = a / 2;
	cout << "a = " << a << endl;

	// %=
	a = 10;
	a %= 2;  // a = a % 2;
	cout << "a = " << a << endl;

	system("pause");

	return 0;
}

Fourth, the comparison operator

Function: used to compare expressions and return a true or false value

Comparison operators have the following symbols:

operator the term example result
== equal to 4 == 3 0
!= not equal to 4 != 3 1
< less than 4 < 3 0
> more than the 4 > 3 1
<= less than or equal to 4 <= 3 0
>= greater or equal to 4 >= 1 1

code:

Tip: In the comparison operation of C and C++ languages, "true" is represented by the number "1", and "false" is represented by the number "0".

#include <iostream>
using namespace std;

int main() {

	int a = 10;
	int b = 20;

	cout << (a == b) << endl; // 0 

	cout << (a != b) << endl; // 1

	cout << (a > b) << endl; // 0

	cout << (a < b) << endl; // 1

	cout << (a >= b) << endl; // 0

	cout << (a <= b) << endl; // 1

	system("pause");

	return 0;
}

5. Logical operators

Function: used to return a true value or a false value according to the value of the expression

Logical operators have the following symbols:

operator the term example result
! No !a If a is false, !a is true; if a is true, !a is false.
&& and a && b The result is true if both a and b are true, otherwise false.
|| or a || b If either a or b is true, the result is true, and if both are false, the result is false.

1. Logical NOT! Code:

Motto: True becomes false, false becomes true

#include <iostream>
using namespace std;

int main() {

	int a = 10;

	cout << !a << endl; // 0

	cout << !!a << endl; // 1

	system("pause");

	return 0;
}

2. Logical AND && Code:

Motto: The same truth is true, the rest are false

#include <iostream>
using namespace std;

int main() {

	int a = 10;
	int b = 10;

	cout << (a && b) << endl;// 1

	a = 10;
	b = 0;

	cout << (a && b) << endl;// 0 

	a = 0;
	b = 0;

	cout << (a && b) << endl;// 0

	system("pause");

	return 0;
}

3. Logical OR || Code:

Motto: the same false is false, the rest are true

#include <iostream>
using namespace std;

int main() {

	int a = 10;
	int b = 10;

	cout << (a || b) << endl;// 1

	a = 10;
	b = 0;

	cout << (a || b) << endl;// 1 

	a = 0;
	b = 0;

	cout << (a || b) << endl;// 0

	system("pause");

	return 0;
}

Guess you like

Origin blog.csdn.net/u014361280/article/details/127583020