C++ stage 01 notes 03 [Operators (arithmetic operators, assignment operators, comparison operators, logical operators)]

C++| Ingenious work from 0 to 1 introductory programming [video + courseware + notes + source code]

table of Contents

3 operator

3.1 Arithmetic operators

Example 1

Example 2

Example 3

3.2 Assignment operator

Example

3.3 Comparison operators

Example

3.4 Logical operators

Example 1: Logical NOT

Example 2: Logical AND

Example 3: Logical OR


3 operator

Role: used to execute the operation of the code.

In this chapter, we mainly explain the following types of operators:

Operator type effect
Arithmetic Operator Used to handle the four arithmetic operations
Assignment operator Used to assign the value of an expression to a variable
Comparison operator Used to compare expressions and return a true or false value
Logical Operators Used to return true or false based on the value of the expression

3.1 Arithmetic operators

Role : Used to process four arithmetic operations.

Arithmetic operators include the following symbols:

Operator the term Example result
+ Positive sign +3 3
- negative -3 -3
+ plus 10 + 5 15
- Less 10 - 5 5
* Multiply 10 * 5 50
/ except 10 / 5 2
% Modulus (take 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-decreasing a=2; b=a--; a=1; b=2;

Example 1

#include <iostream>
using namespace std;

int main()
{
	int a1 = 10;
	int b1 = 3;

	cout << a1 + b1 << endl; // 13
	cout << a1 - b1 << endl; // 7
	cout << a1 * b1 << endl; // 30
	cout << a1 / b1 << endl; // 3 两个整数相除,结果依然是整数,将小数部分去除

	int a2 = 10;
	int b2 = 20;
	cout << a2 / b2 << endl; // 0

	int a3 = 10;
	int b3 = 0;
	//错误!两个数相除,除数不可以为0!
	//cout << a3 / b3 << endl; //报错,除数不可以为0。
	

	//两个小数可以相除
	double d1 = 0.5;
	double d2 = 0.25;
	cout << d1 / d2 << endl; // 2 运算结果可以是整数

	d1 = 0.5;
	d2 = 0.22;
	cout << d1 / d2 << endl; // 2.27273 运算结果可以是小数

	system("pause");

	return 0;
}

Summary: In the division operation, the divisor cannot be 0.

Example 2

#include <iostream>
using namespace std;

int main()
{
	int a1 = 10;
	int b1 = 3;
	cout << a1 % b1 << endl; // 1

	int a2 = 10;
	int b2 = 20;
	cout << a2 % b2 << endl; // 10

	int a3 = 10;
	int b3 = 0;
	//两个数相除,除数不可以为0,所以做不了取模运算
	// cout << a3 % b3 << endl; // 报错。取模运算时,除数不能为0!

	//两个小数是不可以做取模运算的
	double d1 = 3.14;
	double d2 = 1.1;
	// cout << d1 % d2 << endl; // 报错

	system("pause");

	return 0;
}

Summary: Only integer variables can perform modulo operation.

Example 3

#include <iostream>
using namespace std;

int main() //递增
{
	//1、前置递增
	int a = 10;
	++a;						 //让变量+1
	cout << "a = " << a << endl; // 11

	//2、后置递增
	int b = 10;
	b++;			   //让变量+1,等价于b = b + 1
	cout << b << endl; // 11

	//3、前值和后置的区别
	//前置递增:先对变量进行++,再计算表达式
	int a2 = 10;
	int b2 = ++a2 * 10;
	cout << "a2 = " << a2 << endl; // 11
	cout << "b2 = " << b2 << endl; // 11 * 10 -> 110

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

	system("pause");

	return 0;
}

Summary: Pre-increment ++ is performed on the variable first, and then the expression is calculated, and post-increment is the opposite.

3.2 Assignment operator

Role: Used to assign the value of an expression to a variable .

The assignment operator includes the following symbols:

Operator the term Example result
= Assignment a=2; b=3; a=2; b=3;
+= Plus equals a=0; a+=2; a=2;
-= Minus is equal to a=5; a-=3; a=2;
*= Multiply equals a=2; a*=2; a=4;
/= Divide equal to a=4; a/=2; a=2;
%= Modulo equal a=3; a%2; a=1;

Example

#include <iostream>
using namespace std;

int main() // 赋值运算符
{
	// =
	int a = 10;
	a = 100;
	cout << "a = " << a << endl; // a = 100

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

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

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

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

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

	return 0;
}

3.3 Comparison operators

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

Example

#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

	return 0;
}

Note: In C and C++ language comparison operations, "true" is represented by the number "1", and "false" is represented by the number "0" .

3.4 Logical operators

Function: It is used to return true or false value according to the value of the expression.

Logical operators have the following symbols:

Operator the term Example result
! non- !a If a is false, then !a is true; if a is true, then !a is false.
&& versus a && b If both a and b are true, the result is true, otherwise it is false.
|| or a || b If one of a and b is true, the result is true, and when both are false, the result is false.

Example 1: Logical NOT

#include <iostream>
using namespace std;

int main()
{
	//逻辑运算符 --- 逻辑非
	int a = 10;
	//在C++中,除了0都为真
	cout << !a << endl; // 0

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

	system("pause");

	return 0;
}

Summary: True becomes false, and false becomes true.

Example 2: Logical AND

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

Summary: logical AND operator summary: the same truth is true, the rest are false .

Example 3: Logical OR

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

Logical OR operator summary: the same false is false, the rest are true .

Guess you like

Origin blog.csdn.net/weixin_44949135/article/details/115130430