An operator, in a programming language, is a tool used to modify, manipulate, or work with individual values or variables. It is almost always represented using some form of word or symbol put before, after, or between one or more variables, constants, or literals.
Modern programming languages have MANY operators. We will define the most important ones below - a lot of them will seem obvious, but having the reference could be valuable.
I have bolded the ones that might have something kind of surprising or that you might not have seen before - if you only read a few of them, read those!
The operators you are most familar with: these do math.
The examples below represent the results from java. The final column shows the symbol used for this operator in IB Pseudocode (more on that later!)
Operator | Code Sample | Result | IB |
---|---|---|---|
Addition (+ ) |
int a = 2;int b = 7; a + b
|
9
|
+ |
Subtraction (- ) |
a - b |
-5 |
- |
Multiplication (* ) |
a * b |
14 |
* |
Integer Division (/ ) |
a / b b / a
|
0 3
|
div |
Modulus or Remainder (% ) |
a % b b % a
|
2 (remainder of 2 / 7) 1 (remainder of 7 / 2) |
mod |
Floating point division (/ ) |
double a = 2.0, b = 7.0 ;a / b b / a
|
0.285714285714 3.5
|
/ |
The arithmetic operators follow the standard mathematical order of operations - multiplication, modulus, and division are evluated left-to-right first, addition and subtraction second, but parentheses can be used to overcome the order.
A common error comes if you try to divide (or modulus) by 0. Be careful to check to make sure you aren’t doing that if you ever divide by a variable!
Concatenation is the process of combining two strings into one longer string.
Operator | Code Sample | Result | IB |
---|---|---|---|
Concatenation (+ ) |
String b = "bob" ;b + " " + b + "mary";
|
bob bobmary |
N/A |
There are many other thing
Note that since the same operator, +
is used for strings and numbers, you can get some odd results if you aren’t careful.
1
2
3
4
5
6
7
8
"a" + 3; // result: "a3" - converts 3 to a string then concatentes
3 + "a"; // result: "3a" - same as above
3 + 2 + "a" // result: "5a" - does 3 + 2 first, then concatenates with a
3 + (2 + "a") // result: 32a. Concatenates 2 and a to "2a" then concatenates the 3.
"a" + 3 + 2 // result: a32
"a" + (3 + 2) // result: a5
"a" + (3 - 2) // result: a1
"a" + 3 - 2 // error! tries to do "a3" - 2, which is not possible.
At the end of the code block below, what will be printed?
1
2
3
4
5
6
7
int a = 7;
int b = 3;
a = a - b;
b = a * a;
a = a + 1;
b = b - 1;
System.out.println(a+b);
20
What would be printed below?
1
2
3
String s = "23";
int n = 44;
System.out.println(s + n);
2344
What would be printed below?
1
2
3
int b = 381;
int c = 19;
System.out.println((b / c) + " r " + (b % c));
20 r 1
What would be printed below?
1
2
3
4
5
6
7
double d1 = 7.0;
double d2 = 5.0;
int i1 = 1;
int i2 = 4;
System.out.println(i2 + i1 / i2);
System.out.println(i2 - i1 % i2);
System.out.println(d1 / d2 + i1);
4
3
2.4
Evaluate each expression
33 % 5 + 7 % 3;
50 / 2 / 2 * 2;
3 * 6 + 10 / 5 + 5;
50 / (2 / 2) % 2;
2 + 2 * 3;
a. 3 + 1
= 4
b. 25 / 2 * 2
= 12 * 2
= 24
c. 18 + 2 + 5
= 25
d. 50 / 1 % 2
= 50 % 2
= 0
e. 2 + 6
= 8
These operators change the value stored inside a variable. By far the most important one is the first one; we will mostly avoid using the others, but they exist.
Operator | Code | Result | Note | IB |
---|---|---|---|---|
Assignment (= ) |
a = 3; a = a + 1;
|
a has value 3now a has value 4 |
= |
|
Increment (++ ) increases variable value by 1. Used AFTER the variable. |
a = 3; a++; ++a
|
a is 3a is 4a is 5 |
equivalent to a = a + 1
|
N/A |
Decrement (-- ) decreases variable value by 1. Used AFTER the variable. |
a = 3; a--; --a
|
a is 3a is 2a is 1 |
equivalent to a = a - 1
|
N/A |
Addition assignment (+= ) increases variable on left by amount on right. |
a = 3; a += 4;
|
a is 3now a is 7 |
equivalent to a = a + 4
|
N/A |
There are also versions of the last one for subtraction -=
, division /=
, multiplication *=
, and division /=
You should not use ANY of these, generally, as they can be hard to read since they look too much like ==
. But you should be able to understand them.
a
, b
, and c
after each line below?
1
2
3
4
5
6
7
8
9
10
int a = 7;
int b = 3;
int c = 12;
a += 2;
b -= 2;
c *= a % b;
b = b - a;
c++;
--a;
b++;
1
2
3
4
5
6
7
8
9
10
int a = 7; // a is 7
int b = 3; // b is 3
int c = 12; // c is 12
a += 2; // a is now 9
b -= 2; // b is now 1
c *= a % b; // c is still 12
b = b - a; // b is now -8
c++; // c is now 13
--a; // a is now 8
b++; // b is now -7
Logical operators return and work with boolean values - either true
or false
These operators return boolean values, based on the relationship between numbers.
Operator | Code Sample | Result | IB |
---|---|---|---|
Greater than (> ) |
2 > 3 3 > 3 4 > 3
|
false false true
|
> |
Greater than or equal (>= ) |
2 >= 3 3 >= 3 4 >= 3
|
false true true
|
≥ |
Less than (< ) |
2 < 3 3 < 3 4 < 3
|
true false false
|
< |
Less than or equal (<= ) |
2 <= 3 3 <= 3 4 <= 3
|
true true false
|
≤ |
Equal to (== ) |
2 == 3 3 == 3 4 == 3
|
false true false
|
= |
Not Equal (!= ) |
2 != 3 3 != 3 4 != 3
|
true false true
|
≠ |
These are used when working with boolean statements, to combine multiple statments together or change them around. You will commonly see them in if
statements.
Operator | Code Sample | Result | IB |
---|---|---|---|
And (&& ) True only if both sides are true |
(3 > 2) && (2 > 1) (3 > 2) && (2 == 3)
|
true false
|
AND |
Or (|| ) True if EITHER or BOTH sides are true |
(3 > 2) || (2 > 1) (3 > 2) || (2 == 3)
|
true true
|
OR |
Not (! ) Comes BEFORE a boolean statement, and changes it from true to false or vice versa. |
!(3 > 2) !(2 == 3)
|
false true
|
NOT |