Friday, December 18, 2009

Operators


This post is part of my ongoing effort to familiarize the readers with the commonalities & variabilities of C++/Java/C# programming languages. A tentative outline of this series can be found at Contents.

An Operator is a syntactic element which specifies that certain arithmetic or logical manipulation must be performed on some data. C++/Java/C# all support the basic operators of C.

Following are some lists of the operators in these languages grouped by their types. The empty cell means the operator is not directly supported by some particular language and the name in bracket besides it shows what can be used as a replacement.

Arithmetic Operators

C++
Java
C#
Meaning
Example
++
++
++
increment by 1
i++; or ++i;
--
--
--
decrement by 1
i--; or --i;
+
+
+
addition
z = x +y;
-
-
-
subtraction
z=x-y
-
-
-
negation
-x or -5
*
*
*
multiplication
z = x*y
/
/
/
division
z = x/y
%
%
%
modulus (remainder)
z = x % y
Conditional Operators

C++
Java
C#
Meaning
Example
&&
&&
&&
conditional AND
if (x && y)
||
||
||
conditional OR
if (x || y)
!
!
!
conditional NOT
 if (!x)
Logical (bitwise) Operators

C++
Java
C#
Meaning
Example
&
&
&
logical AND
z = (x & y);
| |
|
|
logical OR
z = (x | y);
^
^
^
logical XOR
 z = (x ^ y);
~
~
~
logical complement
 z = ~x;
Relational (conditional) Operators

C++
Java
C#
Meaning
Example
>
>
>
greater than
if (x > y);
>=
>=
>=
greater than or equal to
if (x >= y);
<
<
<
less than
if (x < y);
<=
<=
<=
less than or equal to
if (x > y);
==
==
==
equal to
if (x == y);
!=
!=
!=
not equal to
if (x != y);

/*Add two integers and return the result*/
int
 doSum(int x, int y)
{

    int
 x = 10;
    int
 y = 12;
  
    x++; // increment x by 1
    y--; // decrement y by 1

    return
 x + y; // return 22
}

References:

No comments:

Post a Comment