Operators and Expressions in Programming Languages
Programming languages provide a set of tools that allow programmers to write complex instructions that evaluate to a value. For eg. if you wish to add two numbers you would ask the computer to evaluate the expression x + y.
You may want to assign this value to another variable say z. This new expression would look like z= x+y. In this equation you have already used two operators(+ and =) and three data stores (x,y,z).
Expressions are nothing but the combination of these operators and data stores. These data stores are often referred to as operands. Languages generally have the following types of operators:
Let's consider a=4 and b=2 in all cases
Arithmetic Operator | Description | Example | Result | Notes |
---|---|---|---|---|
+ | Addition | a+b | 6 | |
- | Subtraction | a-b | 2 | |
* | Multiplication | a*b | 8 | |
/ | Division | a/b | 2 | |
% | Modulo | a%b | 2 | Remainder of a/b |
++ | Increment | ++a | 5 | Increment by 1 |
-- | Decrement | --b | 1 | Decrement by 1 |
Another type of operators called relational operators help check whether expressions are either true or false. This is the backbone of all decision making concepts in programming. For eg. you want to check whether a number is a positive or a negative number or check whether your bank account has enough funds to make a transfer, etc.
Relational Operator | Description | Example | Result | Notes |
---|---|---|---|---|
== | Equivalence | a==a | True | is a equal to a? |
> | Greater than | a>b | True | is a bigger than b? |
< | Lesser than | a<b | False | is a smaller than b? |
>= | Greater than or equal to | a>=a | True | is a either greater or equal to a? |
<= | Lesser than or equal to | a<=b | False | is a either lesser or equal to b? |
!= | Not equal to | a!=b | True | is a not equal to b? |
Assignment operators assign results of an expression or declared values to data stores. '=' is an assignment operator.
a=4; // 4 is assigned to the data store a.
b=2; // 2 is assigned to the data store b.
c=a+b; // 6 is assigned to the data store c.
z= a==b; //False is assigned to the data store z.
Logical operators bind operands and expressions to help programmers come up with decision flow based on certain conditions. For example think of a system that prints out whether a number is positive or negative.
How might a programmer think of breaking the problem down? We know that numbers that are greater than 0 are positive and the values lesser than 0 are negative.
A positive number should satisfy this particular condition (number>0) that is this expression number>0 should equate to True.
number>0==True // means it is a positive number
number<0==True //means it is a negative number
What if we want to check if a number is positive and also greater than 100. We would have to write subsequent checks that both evaluate to true that is:
number>0==True and number >100==True
Logical Operator | Description | Example | Result | Notes |
---|---|---|---|---|
&& | And | a == b && b == a | False | is a equal to b? and also is b==a? |
OR | Or | a == a OR b == a | False | either is a equal to a? or is b==a? |
! | Not | !(a==a) | False | Inverts the Truth value to False |
OR operator is often written as || in many languages.
these operators help us write complex expressions:
Expressions to check whether a number is a positive odd number:
number>0 && number%2==1
we use the modulo operator since all odd numbers divided by 2 give us the remainder 1.
We can also write this expression as:
isOddNumber= !(number<0 && number!=0) && number%2==1;
Here the not operator inverts the value of number < 0 and number not zero which simply translates to number is not negative. Brackets are used to prioritize evaluation of expressions. Try evaluating the above expression with number =9. What will be assigned to isOddNumber ? True or False?