Comp 110 Expressions, Operators, and PEMDAS

Expressions, Operators and PEMDAS

Expressions

Expressions are statements made up of variables, literals, operators, or functions that can be evaluated to produce a result. 

For example:

let z = y - x;
let torf = 2 > 5;
let six = 2 * 3;

Operators

Operators are special symbols to perform mathematical functions, comparisons, and assignments.

Most mathematical operators are exactly the same as they are in math! For example: +, -, / and * for addition, subtraction, division and multiplication.

However there are a few you may not be as familiar with. To raise a number to a power we use the double star (**).

let x = 3**2; 

In the example above x would evaluate to 9.

Another less familiar operator is the modulus (%). The modulus divides one number by another and tells us the remainder.

let x = 3%2;

In the example above x would evaluate to 1, since two can go into three once, leaving a remainder of one.

For assigning variables, use the = operator, and the + for string concatenation. 

let firstName = "Creed";
let lastName = "Bratton";
let name = firstName + " " + lastName;

In the above example we use the = operator to assign the variables firstName, lastName, and name values and use the + operator to concatenate firstName and lastName.

PEMDAS

Feeling nostalgic for those 5th grade math classes? Well then Christmas has come early... PEDMAS is back!

It works the same as when you learnt it all that time ago, but as a quick reminder:

P - Parentheses

E - Exponents

M - Multiplication

D - Division

A - Addition

S - Subtraction 

So, for example, 

4 + 5 * (18 / 3) - 4 could be broken down step by step:

parentheses (18 / 3) = 6, 

followed by the multiplication, 5 * 6 = 30, 

ending with addition and subtraction from left to right, 4 + 30 - 4 = 30.

Boolean Expressions

Boolean Expressions

Booleans are either true or false, but we can also have expressions that are true or false. This means we can have whole expressions which are actually just booleans?

What is this sorcery?! Actually it's quite simple, and unfortunately involves no sorcery at all; they're called conditional operators. There are several operators that help compare data, and we'll discuss them below.

Relational Operators

A lot of these symbols come to us from math and are pretty recognizable:

>

  • This is the 'greater than' operator
  • "Is the number on the left of the > larger than the one on the right?"
    • If yes -> true
    • If no -> false

<

  • This is the 'less than' operator
  • "Is the number on the left of the < smaller than the one on the right?"
    • If yes -> true
    • If no -> false

>=

  • This is the 'is at least' operator, and looks like ≥ in maths
  • "Is the number on the left of the  >= greater than or equal to the one on the right?"
    • If yes -> true
    • If no -> false

<= 

  • This is the 'is at most' operator, and looks like ≤ in maths
  • "Is the number on the left of the <= less than or equal to the one on the right?
    • If yes -> true
    • If no -> false

Now that's all well and good, but let's look at them in some code!

let x = 31;
let y = 41;
let z = 41;

// below we'll test each of the operators and assign a boolean to the outcome

let booleanGreater = x > y;
print(booleanGreater); //prints true, because x > y in this case is 31 > 41, which is false, because 31 is less, not greater than 41

let booleanLess = x < y;
print(booleanLess); // prints true, because x < y in this case is 31 < 41, which is true. 31 is less than 41

let booleanAtLeast = y >= z
print(booleanAtLeast) // prints true, because even though y is not greater than z, it is equal to it, and therefore the expression will be true

let booleanAtMost = z <= x;
print(booleanAtMost); // prints false, because z is not less than or equal to x

Useful Operators for Loops

There are two special operators that are especially useful when writing loops. These are the ++ and -- operators. 

The ++ operator is used to add 1 to a number variable, and the -- subtracts one from a number variable.

i++ is the same as writing i = i + 1

i-- is the same as writing i = i - 1

An example using a while loop is shown below

while(i < 10) {
      // loop body
  i++;
}

Equality Operators

As it turns out, the = symbol does more fancy tricks than just assign variables! The following operators are used to check whether something is equal to something else:

===

  • This is the equal to operator. It has THREE equals symbols in a row.
  • It should only be used with simple data types (number, string, and boolean)
    • eg 
      • 1066 === 1066 would be true
      • 21 === 753 would be false

!==

  • This is the not equal to operator. It uses the ! symbol to mean "NOT." 
  • It is the ! symbol followed by TWO equals symbols.
    • eg 
      • 1066 !== 1066 would be false
      •  21 !== 753 would be true


We'll see them in practice below

let x = 12;
let y = 13;

let b = x === y;
print(b); // prints false, because x (12) is not equal to y (13)

let d = x !== y;
print(d); // prints true, because x (12) is not equal to y (13)

Logical Operators

Logical operators and negation are useful for creating compound logical statements.

  • &&
    • AND operator
    • double ampersand
    • Using the AND operator with boolean expressions results in true only if both expressions are true.
  • ||
    • OR operator
    • double vertical bar
    • Using the OR operator with boolean expressions results in true if either expression is true or if both expressions are true.
  • !
    • NOT operator
    • This operator gives us the flipped meaning of a boolean - so if something normally evaluates to true, prefacing it with the ! operator will result in a false value, and if something is normally false, using the ! operator will result in true.

Here are some examples putting everything we've learned together!

let x: number = 7;
let y: number = 3;

let a: boolean = x > y; 
print(a); //prints true
print(!a); //prints false - the inverse of a

let b: boolean = y >= x; 
print(b); //prints false

let c: boolean = a && b; 
print(c); //prints false - remember for && both a and b must return true

let d: boolean = a || b;
print(d); //prints true - for || only one must be true for the statement to return true

Useful Tables

operator table

truth tableand or not