User Tools

Site Tools


crawdad:programming:operators

Operators

There are several different types of operations available in C/C++:

Arithmetic:

Operator Action
= Assignment
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo (remainder after division)
+= Add then assign
-= Subtract then assign
*= Multiply then assign
/= Divide then assign
++ Increment
-- Decrement

Examples:

  • y = x : Copy the value of x into y.
  • y = m * x + b : m*x is evaluated first, the result of which is added to the value of b. The final result is assigned to y.
  • y = m * (x + b) : x+b is evaluated first, the result of which is multiplied by m. The final result is assign to y.
  • iter++ : increment the value of iter by one.
  • energy *= 4.0 : Multiply the value of energy by 4.0.
  • memleft = memory % size : The value of memleft is assigned to the remainder after dividing memory by size;

Boolean/Comparision: Return zero (0) if false and any other value if true.

Operator Action
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
Less than or equal to

Example:

  • y == x : Returns true if the values stored in y and x are exactly the same and false otherwise. (Be careful about the distinction between “==” and “=”!)

Logical:

Operator Action
&& And
|| Or
! Not

Examples:

  • x == y && a == b : Returns true if the values stored in x and y are identical AND the values stored in a and b are identical.
  • x == y || a == b : Returns true if the values stored in x and y are identical OR the values stored in a and b are identical.
crawdad/programming/operators.txt · Last modified: 2009/08/05 18:43 by mactay