| |
6. Arithmetic Operators
|
| |
The binary arithmetic operators are +, -, *, /, and the modulus operator %. |
| operator |
meaning |
examples |
| + |
addition |
x = 3 + 2; /* constants */
y + z; /* variables */
x + y + 2; /* both */ |
| - |
subtraction |
3 - 2; /* constants */
int x = y - z; /* variables */
y - 2 - z; /* both */ |
| * |
multiplication |
int x = 3 * 2; /* constants */
int x = y * z; /* variables */
x * y * z; /* both */ |
| / |
division |
float x = 3 / 2; /*produces x = 1*/
float x = 3.0 / 2; /* produces x = 1.5 */
int x = 3.0 / 2; /* produces x = 1 */ |
| % |
modulus
(remainder) |
int x = 3 % 2; /* produces x = 1 */
int y = 7; int x= y % 4; /* produces 3 */
int y = 7; int x = y % 10; /* produces 7 */ |
|
| |
| |
| |
| |
|
|
|