Northcentral Technical College Introduction To Programming
10-152-310
Introduction to Programming with JavaScript NTC 10-152-310

Learning Plan 3 Operators That Test Relationships

# Operators That Test Relationships

#### File: /Learning Plan 3/relationshipOperators.html

> Is it bigger, smaller, or the same?

### The Relational Operators

Operator | Function
`<` | Less Than test
`>` | Greater Than test
`<=` | Less Than or Equal To test
`>=` | Greater Than or Equal To test

### Using the Relational Operators

> These operators are also used in "if" and "while" statements.

### Relational Boolean Expressions

> Relational operators also make boolean expressions that are either true or false.

- Testing Relationships

- Is 10 less than 10?

- This means "Is 10 less than OR equal to 10", which is true.

- What about strings? Can we use this for strings and how would it make any sense?

- Yes, it makes sense to compare the relationships of strings. Everything in the computer is a number, even characters like "A". Here's a chart showing the numbers in the computer for characters: What Are Characters?

- This is why "Fred" with an upper case "F" is smaller than "fred". All upper case letters are smaller than all lower case letters.

### Less Than (`<`)

> Is the first value smaller than the second?

> Relationship testing is done very often in our code, especially in repetition.

- Numbers. Most of the time we compare numbers with "less than".

- This one catches people sometimes

- We can also use `<` with strings. This will compare the numeric values of the strings. This can be very useful in some circumstances.

- It works with larger strings, too. It will compare each character from left to right until it finds a difference or it reaches the end. Since the upper case "R" has a lower numeric value than the "r" this returns "true".

- Comparing string and numbers? Yes! It does type conversion for us.
- Isn't that a problem? (Yes, it is.)

### Greater Than (`>`)

> Is the first value bigger than the second value?

- Numbers. We frequently want to compare if some value is bigger than another. For example, is the new salary bigger than the maximum salary for that position?

- Another example showing floating point numbers.

- This also works with strings

### Less Than or Equal To (`<=`)

> Is the first value smaller than or equal to the first value?

> Adding an equal sign changes the meaning subtly. This can cause some subtle bugs, too. We now have two questions to ask when we read a statement. First: "Is the first value smaller than the second value?". Second: "Is the first value the same as the second value?". If either of the questions is true then the result will be true.

- Here's the same one from the less than section with the added equal sign. It's true now because 10 is equal to 10. A way to read this would be, "Is 10 the same or smaller than 10?". Yes, it is.

- This one has the same result as above.

- It works with strings, just like we thought it would.

- One situation that can be tricky is when using variables. What if we really meant that when "count" was 10 the result should be false? It is not immediately obvious that we've made a mistake. This becomes an issue when we get to repetition and looping.

### Greater Than or Equal To (`>=` )

> Is the first value bigger than or equal to the second value?

- Just like the opposite operator this ones work like `>` with another question added to the test. With this example, the first question is false but the second question is true. First: "Is 10 greater than 10?". Second: "Is 10 equal to 10?". If either of the questions if true then the results is true.

- Do either of these need the second question?

- Strings too.

### How do we avoid auto-conversion?

- Um, well, you see, there's no relational operators that don't do type auto-conversion.
- We have to use discipline and always convert entered data from the `prompt` dialog to numbers.

### Yup, the lab is next

- Practicing Relationship Operators 3.3 Lab 2
  - 3.3 Assignment 2