conditionals
In this module we will introduce conditionals.
File Location
You should be working in the following file:
Description
Hopefully, you've noticed the difference in an application when you are logged in versus when you are logged out. Most times, these values are based on certain conditions being true or false. Conditional statements, using boolean variables, are all over the place in applications.
Conditionals are used to check certain conditions in your application. Conditionals have the power to alter the state of your application based on certain conditions being met or unmet, true or untrue.
In this module, we'll look at if
statements and how they are used with conditionals.
Sample Code
Consider the following:
We declare a variable and initialize it with true.
We use the
if
keyword to check a certain condition.Our condition checks if
isOn
is set totrue
.If the value of
isOn
equalstrue
, then we'll execute a console statement.
Shorter Phrasing
You'll often see the statement written in a shorthand way:
Here, it's implicitly checking if the condition is true. Same as the original statement.
More Practice
Let's write a conditional that checks to see if the weather is greater than 70. If it is, we want to print "Wear shorts today! It's going to be hot!".
More Practice
Practice writing if statements that involve concepts from your everyday life. Keep it simple:
If caveman is hungry, print: "Caveman need food."
If episode number equals ten, print: "I am sad that this season is ending."
Have some fun with things like that, and gain fluency with conditionals using if
.
Last updated