Selection Using if statements

CS133JS Beginning Programming: JavaScript

Topics by Week 
1. Intro to JavaScript programming6. Arrays
2. Functions, Operators and Expressions7. Objects
3. Selection Statements: if and switch8. Methods
4. Loops9. DOM
5. Midterm10. Final

 

Table of Contents


Introduction

What's Due this Week

Q and A

 

Review

Scope

 

Comparison Operators

SymbolCondition for truthExamples of true expressions
where: a = 7, b = 8;
==Operands are equal to each othera == "7" or a == 7
===Operand values and types are equal to each othera === 7
!=Operands are not equal to each othera != b
>Left operand is greater than the right operandb > a
<Left operand is less than the right operanda < b
>=Left operand is greater than or equal to the right operandb >= b, or b >= a
<=Left operand is less than or equal to the right operandb <= b, or a <= b

 

Logical Operators

Logical operators and expressions

These are also called Boolean3 operators. They are used to form Boolean expressions that evaluate to a value of either true or false.

SymbolNameCondition for truthExamples of true expressions where: a = true, b = false;
&&andBoth operands are truea && a
||orEither operand is truea || b, b || a
!notThe operand is false!b

 

Truth tables

AND  
ABQ
FFF
FTF
TFF
TTT

 

OR  
ABQ
FFF
FTT
TFT
TTT

 

NOT 
AQ
FT
TF

Control Structures

What are the three control structures? These are things in the programming language that control the flow of execution in a program.

This is the end of the review. This was a long review!


Selection Expressions

There are three ways to do selection in JavaScript:

  1. ternary expressions using ? and :

  2. if statements

  3. switch statements

In this session we will learn to use the ternary oprator and if statements. Next time we will learn to use switch statements.

Ternary Operator

The ternary operator is the question mark, ?

Try this:

Write an expression using a ternary operator that determines whether a person is old enough to get a driver's license in Oregon4.

 

if Statements

An if statement is used to conditionally determine whether or not one or more other statements are executed. Note that it does not form an expressionit doesn't evaluate to a value.

Types of if statements

In the examples below, assume this line of code was executed first:

Designing a Selection Algorithm

Let's take a break from talking about coding and talk about designing an algorithm so that we know what it is we need to code.

Conditional expressions using logical operators

For these examples, assume this additional variable has been declared and initialized :

Nested if statements

Another way to combine multiple conditional expressions is to nest the if statements.

Nesting that is equivalent to ANDing two conditions:

 

Conditionally Executing Multiple Statements

So far, we've only executed one statement based on the condition in our if statement. We can execute multiple statements by enclosing them in curly braces, { }.

When to use curly braces

This example checks an user's input and prompts for input again if it the input wasn't valid:

 

Scope of variables inside curly braces

When we declare variables inside curly braces, they have local scope... well, var is an exception.

Example using let:

Lab Assignment

Take a look at Part 1 of the lab assignment now. You know enough to do the first 7 problems which use if statements. The last two problems use switch statements which we will cover in the next class session.

We'll do a few of these in class and we'll start by writing algorithms and/or making IPO tables.

 

Examples

An if statement can be used to check the validity of an argument passed to a function and return a status value:

Note that it is best practice to not put a return statement inside an if...else statement.

More Examples/Exercises

There are three examples posted on Moodle. The first two use if else statements. Lets do one of them together.

Best Practices

Reference

W3Schools

JavaScript Comparison and Logical Operators

JavaScript if else and else if

JavaScript Switch Statement

 


Creative Commons License Beginning JavaScript Lecture Notes by Brian Bird, 2018, revised are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

 


1 Their values can be re-assigned from anywhere in the program, in any function, and it's easy for the dev to loose track of what should be stored in it.
2 JavaScript will treat an undeclared variable as a global variable even if you first assign a value to it in a local scope.
3 Logical operators are a part of Boolean algebra, a mathematical system developed by George Boole in the mid-ninteenth centurylong before the invention of digital computers which use Boolean logic.
4 To get a driver's license in Oregon you must be: 18 years of age or older. Or at least 16 year's of age, have had a permit for 6 months, have had a driver's ed class and 50 hours of supervised driving time, or no driver driver's ed and 100 hours of supervised driving time.