Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

CS1PR16 PERFORMING OPERATIONS

Arithmetic operations:
 Addition +
 Subtraction -
 Multiplication *
 Division /
 Modulus %
Precedence – mathematical rules apply here: Parenthesis -> division- > multiplication -> addition ->
subtraction.

Integer and floating points operations may give different results. Floating points should be used it you
want anything with decimal points.

Incrementing and decrementing


To add 1 to a variable x you would write: x = x + 1. You can increment or decrement variables by any
number: x = x – 3.
++ and -- can be used to add or subtract 1. If the operator is in front of the variable, then it is
incremented before being used (preincrement). If the operator is after the variable, it is incremented
after being used (postincrement).

Logical operations – they are used with values true/false. In C, 0 represents false, any non zero integer
represents true.
 Logical AND &&
 Logical OR ||
 Logical inversion !

Assignment operator - "=" means becomes, not equals. It evaluates the expression on its right hand side
and assigns its value to the variable on the left hand side. == is used to test for equality.
Shorthand assignment operators:
o x = x + 5 can be written as x += 5.
o x = x – 3 can be written as x -= 3.
o x = x * 4 becomes x*=4.
o It is the same with /= and %=.

Relational operators – used for making comparisons and decisions.


 Equality == - compares 2 variables (does not change them)
 Inequality !=
 Greater or smaller: <, >, <=, >=

You might also like