3.1a - Conditionals
Today's Lesson: 3.1 - Conditionals
So you can store a value in a variable, use and manipulate that value, print it, and perform mathematics with it. But how do we make decisions based on a value in a variable?
Friend: "Want to go to the movies?"
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?
Okay, let's say it's a number... Now what?
Important 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
Today's Task(s) / Homework
Working with a partner - on a piece of paper, whiteboard, or similar, write out the steps you would take (in almost plain English) to play the following game:
A secret random number from 1 to 10 is chosen and saved in a variable called secret. The user gets two guesses to find the secret number. If the first guess is incorrect, they are given two hints: too high or low and whether the secret is an even or odd number. Assume you know how to get a random number or exit the game.
Complete the project listed in Replit for lesson 3.1 - Conditionals
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 ||
What will the above code output to the console?