2.6 - Conditionals

Review

  • We now have the ability to create programs that take input, process it, and potentially give output.

  • Functions can optionally return information for later use.

    • The return statement can be used without giving anything back.

    • The return statement exits the current function.

Comments on 2.5 - Return Statement

  • If you create a round() function for yourself, why not use it?

    • Several students used Math.round() in the other functions instead... why?

2.6 - Conditionals

How do we make decisions based on information?

Friend: "Want to go to the movies?"

You: "Only on the condition that you pay."

What happens if your friend pays? What happens otherwise?


Consider a program that determines if a number is even or odd.
Ask the user for a number.
What if they enter a word instead?
Okay, let's say it's a number... Now what?

[ Slides for today's lesson ] [ Scrim for today's lesson ]

Interesting Notes & Resources:

  • There is an operator that determines the remainder of a division statement.

    • % or "Modulo"

    • Examples: 8 % 3 returns 2, or 9 % 5 returns 4, and 16 % 4 returns 0

    • How could that be used to determine if a number is even or odd?

  • The internet is full of tutorials, more tutorials, and examples about the if-else statement since it is one of the very first constructs people learn in programming.

Today's Task(s) / Homework

Extra

  • Here's something weird - try ("A" < "a") or ("A" > "a")

  • Because the if-else condition is boolean logic, you can combine multiple conditions with AND and OR

    • AND uses the operator && and OR uses the operator ||

    • Example:

let x = 5, y = 3;

if ((x < 10) && (y > 1)) {

console.log("Hooray!");

} else {

console.log("Boooo");

}

What will the above code output to the console?