You are on page 1of 17

Lecture #3:

Operators & Conditions


Lecture Over view

F-strings Conditional Logic


Mathematical Operators If/Else
Parsing & Type Conversion Arrays
Arrays
Arrays are how to store several items in a single variable
name.
They are declared using square brackets [].
students = ["Jack", "Jill", “Matilda"];
You can access a particular value as follows: students[0].
0 will access the first value, 1 will return the second, etc.
Array Operations
Function Does
You can perform the shift() Remove from start
following operations by unshift(42) Add to start
typing student.function() push(42) Add to end
pop() Remove from end
Function is the one of the Combine all the values
listed operations and student join(" ") into a string with given
string in bet ween them
is the variable name. The number of items in
length the array.
Exercise #1

Create an array with five numbers in it.

Add the value 42 to the start.

Remove the last value.

Print out the second value in the array.


F-strings
F-strings are a way to easily include code and variables
in strings.

They are declared using back-ticks ``.

Example: `The value of x is ${x}`

Anything inside the squiggly brackets {} is treated as


code.
Mathematical Operators
Operator Operation
As discussed, programming is all
about manipulating data.
+ Addition
We manipulate numbers by
performing mathematical - Subtraction
operations using the symbols on
the right. / Division

Performing a mathematical * Multiplication


operation does not store the result. Modulo
% (Remainder After
Division)
Exercise #2

Declare a variable and set its value to 7.

Perform all of the listed mathematical operations on it.

Make sure that the start & end value is the same.

Use an f-string to print the value after every operation.


Compound Operators
Operator Operation

+= Addition
You can perform the
operation and assign the -= Subtraction
value at the same time using
/= Division
a compound operator.
*= Multiplication
Modulo
%= (Remainder After
Division)
String Operations

You join t wo strings together using the + operator.

You can also add a number to a string with the +


operator.

The order of operands (things being added together)


matters.
Type Conversion
When you use the + on t wo different types one is always
converted to the other.

You can convert a string to a number by using the


parseInt() and parseFloat() methods.

Experiment with different strings to see how the


functions work (or don’t).
Formal Logic
The statement x is less than 18 can either be true or
false depending on the value of x.

The above is an example of a statement with a truth


value. In programming we use statements like this to
determine what a program will do.

For example, x might represent a user’s age and if they


are under 18 I will say “sorry you can’t vote”.
Logical Operators
Operator Operation
Logical operators are the == Equals
symbols we us to === Equals exactly
compare t wo variables. != Not Equal
See table for list. Not Exactly
!==
Equal
The comparison will > Greater than
always be either true or < Less than
false. <= Less or Equals
>= Greater or Equals
If, Else, and Else If
If statements are how we evaluate conditions.

The following line will check if a number is greater than 25.

if(x>25) {
console.log(`${x} is greater than 25`)
} else {
console.log(`${x} is not greater than 25`)
}

The else part isn’t need, but it will give our user feedback either way.

You can also write else if (CONDITION) with some other condition, as many times as you’d like.

The conditions are checked until one of them is considered true and then it stops.
Exercise #3

Create a variable with a string value.

Convert that string to a number.

Check if that number is greater than or equal 5 and also


even.
AND OR
We can also link logical Operator Operation
statements. Using the
operators in the table.
&& AND
For an AND to be true, all
statements must be true. || OR

For an OR to be true, at least


one statement must be true.
AND OR (Cont.)

Will if(x % 2 == 0 && x % 2 == 1) ever be false?

Will if(x % 2 == 0 || x % 2 == 1) ever be false?

You might also like