What are the factors that are used to give values to the variables? (Assignment Operators) in C++
the name of the employee | his code | Example | Explanation of the code |
---|---|---|---|
Basic Assignment | = | a = b | put a value b in a . |
Add AND Assignment | += | a += b | Add a value a to a value b and store the result in a |
Susbtract AND Assignment | -= | a -= b | Subtract a value a from a value b and store the result in a |
Multiply AND Assignment | *= | a *= b | Multiply a value by its a value b and store the result in a |
Divide AND Assignment | /= | a /= b | Divide a value a by a value b and store the result in a |
Modulo AND Assignment | %= | a %= b | Divide a value a by a value b and store the last remaining number in the division operation a |
Left shift AND Assignment | <<= | a <<= 2 | remove the last two bits And put them in the first and then store the output in a |
Right shift AND Assignment | >>= | a >>= 2 | Slide the first two bits And put them in the other and then store the output in a |
Bitwise AND Assignment | &= | a &= b | Calculate the sum of the bits common to a and b and store the result in a |
Bitwise exclusive OR and Assignment | ^= | a ^= b | Calculate the sum of the bits unshared between a and b and store the result in a |
Bitwise inexclusive OR and Assignment | |= | a |= b | Calculate the sum of the bits Shared and unshared between a and b and store the output in a |
Explain the factors that are used to give values to the variables in C++
worker =
(Basic Assign) in C++
The operator =
is used to assign a value to a variable.
Example
#include <iostream> using namespace std; int main() { int a = 10; cout << "a = " << a; return 0; }
• We will get the following result when running.
a = 10
worker +=
(Add and Assign) in C++
The operator +=
is used to add a value to the value of a variable with less code.
Example
#include <iostream> using namespace std; int main() { int a = 10; int b = 20; a += b; // a = a + b = 10 + 20 = 30 cout << "a = " << a; return 0; }
• We will get the following result when running.
a = 30
worker -=
(Susbtract and Assign) in C++
The operator -=
is used to subtract a value from the value of a variable with a lower code.
Example
#include <iostream> using namespace std; int main() { int a = 10; int b = 20; a -= b; // a = a - b = 10 - 20 = -10 cout << "a = " << a; return 0; }
• We will get the following result when running.
a = -10
worker *=
(Multiply and Assign) in C++
The operator *=
is used to multiply the value of a variable by a value with a lower code.
Example
#include <iostream> using namespace std; int main() { int a = 10; int b = 20; a *= b; // a = a * b = 10 * 20 = 200 cout << "a = " << a; return 0; }
• We will get the following result when running.
a = 200
worker /=
(Divide and Assign) in C++
The operator /=
is used to divide the value of a variable by a value with a lower code.
Example
#include <iostream> using namespace std; int main() { int a = 100; int b = 20; a /= b; // a = a / b = 100 / 20 = 5 cout << "a = " << a; return 0; }
• We will get the following result when running.
a = 5
worker %=
(Modulo and Assign) in C++
The operator %=
is used to store the remainder of a variable's value divided by a value with less code.
Example
#include <iostream> using namespace std; int main() { int a = 10; int b = 7; a %= b; // a = a % b = 10 % 7 = 3 cout << "a = " << a; return 0; }
• We will get the following result when running.
a = 3
worker <<=
(Left shift and Assign) in C++
In the following example, operator <<=
means delete bits from the left side, then switch each bit It was erased from them with a zero and put them on the right and then put the result in the variable again.
Example
#include <iostream> using namespace std; int main() { int a = 10; a <<= 2; // a = a << 2 = 00000000000000000000000000001010 << 2 = 00000000000000000000000000101000 = 40 cout << "a = " << a; return 0; }
• We will get the following result when running.
a = 40
worker >>=
(Right shift and Assign) in C++
In the following example, operator >>=
means delete bits on the right, then switch each bit It was erased from them with zero and put them on the left side, then put the result in the variable again.
Example
#include <iostream> using namespace std; int main() { int a = 10; a >>= 2; // a = a >> 2 = 0000000000000000000000000000001010 >> 2 = 00000000000000000000000000000010 = 2 cout << "a = " << a; return 0; }
• We will get the following result when running.
a = 2
worker &=
(Bitwise and Assign) in C++
The factor &=
means Calculate the product of the sum of bits shared between the variable a
and the variable and b
then store the result in the variable a
.
Example
#include <iostream> using namespace std; int main() { int a = 10; int b = 75; a &= b; // a = a & b = 00000000000000000000000000001010 & 00000000000000000000000001001011 = 00000000000000000000000000001010 = 10 cout << "a = " << a; return 0; }
• We will get the following result when running.
a = 10
worker |=
(Bitwise exclusive OR and Assign) in C++
The factor |=
means Calculate the product of the sum of bits Shared and unshared between the variable a
and the variable and b
then store the result in the variable a
.
Example
#include <iostream> using namespace std; int main() { int a = 10; int b = 75; a |= b; // a = a | b = 0000000000000000000000000000001010 | 00000000000000000000000001001011 = 00000000000000000000000001001011 = 75 cout << "a = " << a; return 0; }
• We will get the following result when running.
a = 75
Other very important factors in C++
the name of the employee | his code | use it |
---|---|---|
Conditional | ?: | It is also called Ternary Operator Because it takes three elements to function. It can be used instead of conditional clauses if and else if you want to give a value to the variable. |
Size of | sizeof() | Used to find out how much space is allocated in memory for anything passed between parentheses. The number returned represents the size with Byte. |
Address | & | Used to find out the address of the space allocated in memory for the object to be placed immediately after it. The returned address is returned as Hexadecimal. |
Pointer | * | It is used to place a pointer to something that has been defined which makes you able to directly access it from anywhere you want by its address in memory. |
Comm | , | The comma is used to perform more than one arithmetic operation and then assign its value to one variable. Noting that a comma must be placed between each of the two currencies you place. |
Other important factors in C++
worker ?:
(Conditional Operator) in C++
The worker ?:
is called Conditional or Ternary Operator Because it takes three elements to function.
It can be used instead of conditional clauses if
and else
if you want to give a value to the variable.
build worker ?:
(Conditional Operator) in C++
expression
: represents the condition we set.value if true
: represents the value that will be given to the variablex
if the condition is met, we put it after the symbol?
.value if false
: represents the value that will be given to the variablex
if the condition is not met, we put it after the symbol:
.
first example
#include <iostream> using namespace std; int main() { int a = 10; // b If not, the value 30 will be placed in the variable b, b is 1 the value 20 will be placed in the variable a if the value of the variable is int b = (a == 1) ?20 :30; cout << "b = " << b; return 0; }
• We will get the following result when running.
b = 30
• We note that the value 30 is placed in b
because the result of the condition was false
.
second example
#include <iostream> using namespace std; int main() { int a = 10; // b If not, the value 30 will be placed in the variable b, b is greater than 1 the value 20 will be placed in the variable a if the value of the variable is int b = (a > 1) ?20 :30; cout << "b = " << b; return 0; }
• We will get the following result when running.
b = 20
• We note that the value 20 is placed in b
because the result of the condition was true
.
worker sizeof()
(Size Of Operator) in C++
Used to find out how much space is allocated in memory for anything passed between parentheses.
The number returned represents the size with Byte.
first example
#include <iostream> using namespace std; int main() { // C++ Here we have printed the size required by each ready type in a language cout << "Size of char: " << sizeof(char) << endl; cout << "Size of int: " << sizeof(int) << endl; cout << "Size of float: " << sizeof(float) << endl; cout << "Size of double: " << sizeof(double) << endl; cout << "Size of short int: " << sizeof(short int) << endl; cout << "Size of long int: " << sizeof(long int) << endl; cout << "Size of long long int: " << sizeof(long long int) << endl; cout << "Size of long double: " << sizeof(long double) << endl; return 0; }
• We will get the following result when running.
Size of char: 1 Size of int: 4 Size of float: 4 Size of double: 8 Size of short int: 2 Size of long int: 4 Size of long long int: 8 Size of long double: 12
second example
#include <iostream> using namespace std; int main() { // int and its type x here we have defined a variable named int x = 5; // x Here we have printed the size that is reserved in memory for the variable cout << "Size of x in memory: " << sizeof(char) << " Byte\n"; return 0; }
• We will get the following result when running.
Size of x in memory: 1 Byte
worker &
(Address) in C++
Used to find out the address of the space allocated in memory for the object to be placed immediately after it.
Example
#include <iostream> using namespace std; int main() { // int and its type x here we have defined a variable named int x = 5; // In memory x here we have printed the address of the space that is allocated to the variable cout << "Address of x in memory: " << &x; return 0; }
• We will get a result similar to the following when running.
Address of x in memory: 0x72feec
worker *
(Pointer) in C++
It is used to place a pointer to an object when it is defined, making it possible to access it directly from anywhere you want by its address in memory.
Example
#include <iostream> using namespace std; int main() { // 5 and its value x here we have defined a variable named int x = 5; // x and we have defined it as a pointer to variable y here we have defined a variable named // they refer to the same memory value y and the variable x, so the variable int *y = &x; // x which is the same as the value of the variable y here we have printed the value put in the variable cout << "y contain x value which is: " << *y; return 0; }
• We will get a result similar to the following when running.
y contain x value which is: 5
note
You will learn about indicators and how to deal with them in detail later in the course; It is enough to understand the meaning of the word indicator.
worker ,
(Comm) in C++
The comma is used to perform more than one arithmetic operation and then assign its value to one variable.
Noting that a comma must be placed between each of the two currencies you place.
Example
#include <iostream> using namespace std; int main() { // x Here we have defined a variable named int x; // s is equal to 1, then we add 2, then multiply it by 5. And in the end we put the result in the variable x here we set the value of int s = (x = 1, x += 2, x *= 5); // s Here we have printed the value of the variable cout << "s = ((1) + 2) * 5 = " << s; return 0; }
• We will get a result similar to the following when running.
s = ((1) + 2) * 5 = 15