• 4 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 6/15/21

Follow a sequence with conditions

Just like logical operators, conditions also use true/false to make decisions. As you saw in the video, they do this with if/else statements.

If checks a condition.  If the condition is true, it performs a specified action.  If the condition is false, it performs a different action. This second action is specified by else. It looks a little something like this: 

IF/ELSE statement
If/else statement

You can apply if/else statements to your everyday life.  Imagine you’ve got a hankering for some fancy chocolates - you know, the kind that come in a box.

Delicious, delicious chocolate
Delicious, delicious chocolate

You’ve started to notice that you’re making a lot of trips to the store to buy chocolate, which isn’t the most efficient use of your time.  That said, fancy chocolate is expensive, so you can’t buy too much of it at once. As you’re heading to the store, you can come up with a game plan for your chocolate purchases:

IF/ELSE for buying chocolate
If/else buying chocolate

The same logic can be applied to all sorts of situations, like getting a new pet at your local animal shelter:

IF/ELSE for adopting a pet
If/else adopting a pet

Greater than, less than, equal to

You may remember these from math class. You can safely say that the following examples are all true:

  • 30 > 15
    Thirty is greater than fifteen.

  • 5 < 10
    Five is less than ten.

  • 3 = 3
    Three is equal to three.

Still with me?  You can evaluate these kinds of expressions as either true or false.  You can also add them to if/else statements. Let's start with greater than:

IF/ELSE for respecting the speed limit
If/else respecting the speed limit

What about a less than?

IF/ELSE for eating out
If/else eating out

You could also do an equal than, but you get the idea. 😉

Summary

  • Use if/else statements for conditions.

  • You can compare elements using greater than, less than, and equal to.

Example of certificate of achievement
Example of certificate of achievement