Basic Operators

Posted by gintjack on Fri, 28 Jan 2022 21:16:13 +0100

operator

Role: Operations used to execute code

In this chapter, we will focus on the following types of operators:

Operator typeEffect
Arithmetic OperatorsFor four operations
Assignment OperatorsUsed to assign the value of an expression to a variable
Comparison operatorUsed for comparison of expressions and returns a true or false value
Logical operatorsUsed to return true or false values based on the value of an expression

1. Arithmetic Operators

Role: For four operations

Arithmetic operators include the following symbols:

operatortermExampleResult
+Positive sign+33
-Minus sign-3-3
+plus10 + 515
-reduce10 - 55
*ride10 * 550
/except10 / 52
%Modular (Remaining)10 % 31
++Pre-incrementa=2; b=++a;a=3; b=3;
++Postincrementa=2; b=a++;a=3; b=2;
Pre-Decreasinga=2; b=–a;a=1; b=1;
Posterior Decreasea=2; b=a–;a=1; b=2;

Example 1:

//Add, subtract, multiply and divide
int main() {

	int a1 = 10;
	int b1 = 3;

	cout << a1 + b1 << endl;
	cout << a1 - b1 << endl;
	cout << a1 * b1 << endl;
	cout << a1 / b1 << endl;  //Divide two integers and the result is still an integer

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

	int a3 = 10;
	int b3 = 0;
	//Cout << A3 / B3 << endl; // Error, divisor cannot be 0


	//Two decimals can be divided
	double d1 = 0.5;
	double d2 = 0.25;
	cout << d1 / d2 << endl;

	system("pause");

	return 0;
}

Summary: In a division operation, the divisor cannot be zero

Example 2:

//Modeling
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; // Divider cannot be 0 in modulo operation

	//Two decimal numbers are not modular
	double d1 = 3.14;
	double d2 = 1.1;

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

	system("pause");

	return 0;
}

Summary: Only integer variables can be modulated

Example 3:

//Incremental
int main() {

	//Postincrement
	int a = 10;
	a++; //Equivalent to a = a + 1
	cout << a << endl; // 11

	//Pre-increment
	int b = 10;
	++b;
	cout << b << endl; // 11

	//Difference
	//Pre-increment adds ++ to the variable before calculating the expression
	int a2 = 10;
	int b2 = ++a2 * 10;
	cout << b2 << endl;

	//Post-increment first evaluates the expression, then ++ the variable
	int a3 = 10;
	int b3 = a3++ * 10;
	cout << b3 << endl;

	system("pause");

	return 0;
}

Summary: Pre-increment adds ++ to the variable before calculating the expression, and then adds up the opposite

Return to top

2. Assignment Operators

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

Assignment operators include the following symbols:

operatortermExampleResult
=assignmenta=2; b=3;a=2; b=3;
+=Add equalsa=0; a+=2;a=2;
-=Subtract equalsa=5; a-=3;a=2;
*=Multiply equalsa=2; a*=2;a=4;
/=Divide equalsa=4; a/=2;a=2;
%=Modulo equalsa=3; a%2;a=1;

Example:

int main() {

	//Assignment Operators

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

Return to top

3. Comparison Operators

Role: Used to compare expressions and return a true or false value

The comparison operator has the following symbols:

operatortermExampleResult
==Equal to4 == 30
!=Not equal to4 != 31
<less than4 < 30
>greater than4 > 31
<=Less than or equal to4 <= 30
>=Greater than or equal to4 >= 11

Example:

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

Note: In the comparative operation between C and C++, True is represented by the number 1 and False is represented by the number 0.

Return to top

4. Logical Operators

Role: Used to return true or false values based on the value of an expression

Logical operators have the following symbols:

operatortermExampleResult
!wrong!aIf a is false, then! a is true; If a is true, then! a is false.
&&anda && bIf both a and b are true, the result is true, otherwise it is false.
||ora || bIf one of a and b is true, the result is true, and if both are false, the result is false.

Example 1: Logical NOT

//Logical Operator - Not
int main() {

	int a = 10;

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

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

	system("pause");

	return 0;
}

Summary: True to False, False to True

Example 2: Logical and

//Logical Operator--And
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: Summary of Logic and Operators: True as True, False as Others

Example 3: Logical or

//Logical Operator--or
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;
}

Summary of logic or operator: duplicate false, remaining true

Return to top

Reference resources: https://www.bilibili.com/video/BV1et411b73Z?p=15&t=271

Topics: C++