Logical and And Assignment Operators in cpp
Logical Operators
A. Logical AND (&&)
The logical AND operator returns true if both operands are true.
bool result = (5 > 3) && (10 < 20);
// result is true
bool result = (5 > 3) && (10 > 20);
// result is false
B. Logical OR (||)
The logical OR operator returns true if at least one of the operands is true.
bool result = (5 > 3) || (10 < 20);
// result is true
C. Logical NOT (!)
The logical NOT operator negates the result and returns the opposite value.
bool result = !(5 > 3);
// result is false
Logical operators are commonly used in conditional statements, loops, and boolean expressions to make decisions and control the flow of the program based on specific conditions. They allow you to combine multiple conditions and create more complex logical expressions to achieve desired outcomes in your program.
Assignment Operators
Assignment operators are used to assign values to variables in C++. They can also perform arithmetic operations simultaneously with the assignment. The following assignment operators are commonly used:
A. Simple assignment (=)
The simple assignment operator assigns the value on the right-hand side to the variable on the left-hand side.
int num = 5;
// num is assigned the value 5
B. Addition assignment (+=)
The addition assignment operator adds the value on the right-hand side to the variable on the left-hand side and assigns the result to the left-hand side variable.
int num = 5;
num += 3;
// num is now 8 (5 + 3)
C. Subtraction assignment (-=)
The subtraction assignment operator subtracts the value on the right-hand side from the variable on the left-hand side and assigns the result to the left-hand side variable.
int num = 10;
num -= 4 ;
// num is now 6 (10 - 4)
D. Multiplication assignment (*=)
The multiplication assignment operator multiplies the variable on the left-hand side by the value on the right-hand side and assigns the result to the left-hand side variable.
int num = 2;
num *= 6 ;
// num is now 12 (2 * 6)
E. Division assignment (/=)
The division assignment operator divides the variable on the left-hand side by the value on the right-hand side and assigns the result to the left-hand side variable.
double num = 15.0;
num /= 4.0;
// num is now 3.75 (15.0 / 4.0)
F. Modulo assignment (%=)
The modulo assignment operator performs the modulo operation on the variable on the left-hand side using the value on the right-hand side and assigns the result to the left-hand side variable.
int num = 20;
num %= 3;
// num is now 2 (20 % 3)
Assignment operators provide a convenient way to modify variables by combining assignment with arithmetic operations in a single statement. They can be used to simplify and optimize code by performing calculations and assignments simultaneously.