Basic notes of C + + operator

Introduction notes to C + + Basics (III) operator Basics

Whether you learn or not, C + + is there, seducing you all the time

The content is based on the dark horse programmer c + + course handout. I added a small part and put it here as a reference for my review in the future. (this introductory course is highly recommended. It's available on website B)

Dark horse handout original file point here Learn from yourself and make progress together.

3 operator

Function: used to perform code operations

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

Operator type effect
Arithmetic operator Used to process four operations
Assignment Operators 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 operator Used to return true or false values based on the value of an expression

3.1 arithmetic operators

Function: used to process four operations

Arithmetic operators include the following symbols:

operator term Examples result
+ Plus sign +3 3
- minus sign -3 -3
+ plus 10 + 5 15
- reduce 10 - 5 5
* ride 10 * 5 50
/ except 10 / 5 2
% Mold taking (residual) 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;

Example 1:

#include<iostream>
using namespace std;

//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;  //The output is 3 because the result of dividing two integers is still an integer

	int a2 = 10;
	int b2 = 20;
	cout << a2 / b2 << endl; //The output is 0, also because the result of dividing two integers is still an integer

	int a3 = 10;
	int b3 = 0;
	//cout << a3 / b3 << endl; // An error is reported. The divisor cannot be 0


	//Two decimals can be divided
	double d1 = 0.5;
	double d2 = 0.25;
	cout << d1 / d2 << endl; //The output is 2 because it is exactly divisible. Non divisible decimals will be decimals

	system("pause");

	return 0;
}

Summary:

  • In a division operation, the divisor cannot be 0
  • The result of dividing two integers is still an integer. Remove the decimal part

Example 2:

#include<iostream>
using namespace std;

//Take the mold, that is, take the remainder
int main() {

	int a1 = 10;
	int b1 = 3;

	cout << 10 % 3 << endl;  //The result is 1

	int a2 = 10;
	int b2 = 20;

	cout << a2 % b2 << endl; //The result is 10

	int a3 = 10;
	int b3 = 0;

	//cout << a3 % b3 << endl; // An error is reported because the divisor cannot be 0 during modulo operation

	//Two decimals cannot be modulo
	double d1 = 3.14;
	double d2 = 1.1;

	//cout << d1 % d2 << endl; // An error is reported because modulo operation can only be performed on integers

	system("pause");

	return 0;
}

Summary:

  • Only integer variables can perform modulo operations, which is essentially the remainder
  • Two decimals cannot be modulo

Example 3:

#include<iostream>
using namespace std;

//Increasing
int main() 
{
	//1. Pre increment
	int b = 10;
	++b; //Add 1 to the variable
	cout << b << endl; 
    //The result is: 11
    
    //2. Post increment
	int a = 10;
	a++; //Equivalent to a = a + 1
	cout << a << endl; 
    //The result is: 11

	//3. Difference between pre and post
	//Pre increment first + + the variable, and then calculate the expression
	int a2 = 10;
	int b2 = ++a2 * 10;
	cout << b2 << endl; 
    //The result is 110

	//Post increment calculates the expression first, and then calculates the variable++
	int a3 = 10;
	int b3 = a3++ * 10;
	cout << b3 << endl; 
    //The result is: 100

	system("pause");

	return 0;
}

Summary:

  • Pre increment first + + the variable, and then calculate the expression
  • In contrast to post increment, the expression is calculated first and then++

3.2 assignment operator

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

The assignment operator includes the following symbols:

operator term Examples result
= assignment a=2; b=3; a=2; b=3;
+= Plus equals a=0; a+=2; a=2;
-= Minus equals a=5; a-=3; a=2;
*= Multiply equal a=2; a*=2; a=4;
/= Equal to divide a=4; a/=2; a=2;
%= Modulo equal a=3; a%=2; a=1;

Example:

#include<iostream>
using namespace std;

int main() 
{
	//Assignment Operators 

	// Equals=
	int a = 10;
	a = 100; //Reassign a
	cout << "a = " << a << endl;
    //The result is 100

	// Plus equals+=
	a = 10; //Reassign a
	a += 2; // Equivalent to a = a + 2;
	cout << "a = " << a << endl;
    //The result is 12

	// Minus equals-=
	a = 10; //Reassign a
	a -= 2; // Equivalent to a = a - 2
	cout << "a = " << a << endl;
    //The result is 8

	// Multiply equal*=
	a = 10; //Reassign a
	a *= 2; // Equivalent to a = a * 2
	cout << "a = " << a << endl;
    //The result is 20

	// Division equals/=
	a = 10; //Reassign a
	a /= 2;  // Equivalent to a = a / 2;
	cout << "a = " << a << endl;
    //The result is 5

	// Modulus equal to%=
	a = 10; //Reassign a
	a %= 2;  // A = 2;
	cout << "a = " << a << endl;
    //The result is 0

	system("pause");

	return 0;
}

3.3 comparison operator

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

The comparison operator has the following symbols:

operator term Examples result
== Equal to 4 == 3 0
!= Not equal to 4 != 3 1
< less than 4 < 3 0
> greater than 4 > 3 1
<= Less than or equal to 4 <= 3 0
>= Greater than or equal to 4 >= 1 1

Example:

#include<iostream>
using namespace std;

int main() 
{
	int a = 10;
	int b = 20;

	cout << (a == b) << endl; //The expression should be enclosed in parentheses, otherwise the output symbol will be regarded as two less than signs
    // 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 comparison operation of C and C + + languages, "true" is represented by the number "1", and "false" is represented by the number "0".

3.4 logical operators

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

Logical operators have the following symbols:

operator term Examples result
! wrong !a If a is false, then! A is true; If a is true, then! A is false.
&& And 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. If both are false, the result is false.

Example 1: logical non

//Logical operator --- not
int main() {

	int a = 10;

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

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

	system("pause");

	return 0;
}

Summary: reverse operation, true to false, false to true

Example 2: logic and

//Logical operators --- and
int main() {

	int a = 10;
	int b = 10;

	cout << (a && b) << endl; //Be careful to have parentheses
    // 1

	a = 10;
	b = 0;

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

	a = 0;
	b = 0;

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

	system("pause");

	return 0;
}

Summary: logic and operator summary: 8-word truth = = the same truth is true, and the rest are false==

Example 3: logical or

//Logical operator --- or
int main() {

	int a = 10;
	int b = 10;

	cout << (a || b) << endl; //Be careful to have parentheses
    // 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: 8-character true words are false if they are the same as false, and the rest are true

Tags: C++ Programming

Posted by andy75180 on Tue, 24 May 2022 03:17:52 +0300