Variables and Operators-1
Variables and Operators-1
Simple Program:
Printing a Line of Text
Escape Sequence Description
Arithmetic Operators
C ++ operation Arithmetic Algebraic C ++ expression
operator expression
Addition + f+7 f + 7
Subtraction - p–c p - c
Multiplication * bm b * m
Division / x/y x / y
Modulus % r mod s r % s
11
Increment Decrement Operators
Increment Operators:
Decrement Operators:
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
>= x >= y x is greater than or equal to y
<= x <= y x is less than or equal to y
Equality operators
= == x == y x is equal to y
!= x != y x is not equal to y
13
Logical Operators:
Example: x = 5
1. Addition Assignment (+=): Adds the right operand to the left operand.
Example: x += 5 (equivalent to x = x + 5)
1. Subtraction Assignment (-=): Subtracts the right operand from the left operand.
Example: x -= 5 (equivalent to x = x - 5)
1. Multiplication Assignment (*=): Multiplies the left operand by the right operand.
Example: x *= 5 (equivalent to x = x * 5)
1. Division Assignment (/=): Divides the left operand by the right operand.
Example: x /= 5 (equivalent to x = x / 5)
1. Modulus Assignment (%=): Computes the remainder of the left operand divided by the right
operand.
15
Operator Precedence Rules:
1. Parentheses () have highest precedence.
2. Prefix operators (++, --, !, ~, etc.) are evaluated next.
3. Multiplicative operators (*, /, %) are evaluated after prefix operators.
4. Additive operators (+, -) follow multiplicative operators.
5. Comparison operators (==, !=, <, >, etc.) come next.
6. Logical operators (&&, ||) are evaluated after comparison operators.
7. Assignment operators (=, +=, -=, etc.) have lowest precedence.
16
Example 1:
#include <iostream>
int main() {
return 0;
}
#include <iostream>
int main() {
return 0;
}
17
Example 2:
#include <iostream>
int main() {
return 0;
}
18
Example 3:
#include <iostream>
int main() {
int result = 16 / 2 + 3 * 2 - 5 % 2 + 11 / 3;
return 0;
}
The End