Conditionals - Creating a Test

What are some things in our lives that can be either true or false?

Ex: If I the weather is nice I will go outside

if(niceWeather) {
    console.log("go outside")
}

Common relational expressions

Operator Expression
> Greater than
< Less than
=== Is equal to
>= Greater than or equal to
<= Less than or equal to
!= Not equal to

ex:

if(temp > 90) {
    console.log("stay inside")
}

Common logic expressions

Operator Expression
&& and
|| or

ex:

if(temp > 90 && !rain) {
    console.log("go outside")
}

Single Test, Single Result (The If Statement)

Translated into code, an If Statement looks like this:

if (test) {
  result
}

Pay attention to the syntax:

mouseIsPressed:

See the Pen Example Code by LSU DDEM ( @lsuddem) on CodePen.

https://codepen.io/lsuddem/pen/aKWKmj

Single Test, Two Results (The If/Else Statement)

Translated into code, an If/Else statement looks like this:

if (test) {
  result 1
} else {
  result 2
}

mouseIsPressed if else:

See the Pen Example Code by LSU DDEM ( @lsuddem) on CodePen.

https://codepen.io/lsuddem/pen/YvVvwM

Multiple Tests, Multiple Results (The If/Else If Statement)

Translated into code, an If/Else If statement looks like this:

if (test 1) {
  result 1
} else if (test 2) {
  result 2
} else {
  result 3
}

See the Pen Example Code by LSU DDEM ( @lsuddem) on CodePen.

https://codepen.io/lsuddem/pen/zaGpgp

How could we substantially shorten this?

Building extensive If/Else If Statements can end up making your code look cluttered and hard to debug if certain tests aren’t acting the way they should. In our Graphics Unit, we’ll learn how to nest a conditional test inside of another test in order to make more clean-looking, sensical logic flows for advanced projects.