C Programming
Assignment Operators
Assignment Operators
When the first variable on the right side of the assignment is the same as the one on the
left side, we can implement assignment operators.
The following table clear the idea of the assignment operators
Assignment Statement Assignment Operator
x = x + y; x += y;
x = x – y; x -= y;
x = x * y; x *= y;
x = x / y; x /= y;
x = x % y; x %= y;
M. Rana Bader 3
PreIncrement & PostIncrement Operator (++)
PreIncrement : Add one to the variable and then use the new value in the expression.
Example:
int x =3, y; 1. Add 1 to x x= 4
y = ++x * 5; 2. Evaluate: x * 5 4 * 5 = 20
3. Store 20 into y
printf(“y = %d”,y); // y = 20
PostIncrement: Use the old value of the variable in the expression then add one to it.
Example:
int x =3, y; 1. Evaluate: x * 5 3 * 5 = 15
y = x++ * 5; 2. Store 15 into y
3. Add 1 to x x= 4
printf(“y = %d”,y); // y = 15
M. Rana Bader 4
PreDecrement & PostDecrement Operator (--)
PreDecrement : Subtract one from the variable and then use the new value in the
expression. Example:
int x =3, y; 1. Subtract 1 from x x= 2
y = --x * 5; 2. Evaluate: x * 5 2 * 5 = 10
3. Store 10 into y
printf(“y = %d”, y); // y = 10
PostDecrement: Use the old value of the variable in the expression then subtract one
from it.
Example:
1. Evaluate: x * 5 3 * 5 = 15
int x =3, y;
2. Store 15 into y
y = x-- * 5; 3. Subtract 1 from x x= 2
printf(“y = %d”, y); // y = 15
M. Rana Bader 5
Java Operator
Operator Classification Description
Arithmetic operations
(+, -, *, /, %)
Assignment statement (=)
Binary Operators Required two operands
Assignment operators
(+=, -=, *=, /=, %=)
Pre/Post Increment (++)
Unary Operators Required one operand
Pre/Post decrement (--)
M. Rana Bader 6
Java Operator Precedence
Java has the following operator precedence from left to right as follows:
1. ( …)
2. ++, --
3. *, /, %
4. +, -
5. =, +=, -=, *=, /=, %=
M. Rana Bader 7
Constant Variable
const data _type cosnt_name = value;
Ex:
const float pi = 3.14;
Foreach constant variable
1- It cant not be changed or modified .
2- We can use it on the right side variable in the assignment statement.
3- We can print out its value.