Learn

Every day, we need to make decisions in our lives. We make these decisions based on the current conditions. If we are hungry, then we will make food. If we have lettuce and dressing, then we will make a salad. If we are feeling lazy, we may eat at a restaurant.

In a computer program, all the decisions we could make based on a question (such as, “am I hungry?”) are called a control structure.

In a social media application, a control structure might ask “Did the user sign in properly?”. If they did, the program shows the user their account. If they don’t, the program asks them to try again.

Control structures are made up of these conditional statements. A conditional statement is a piece of code that follows the pattern If a condition is met, then do X.

In a social media application, this statement could be “if the user sign-in is correct, then open the user’s account”.

Conditional statements can also be chained together to ask followup questions, covering any scenario that might come up. If the user did NOT sign in correctly, the program can have them try again. However, if the user signs incorrectly 5 times, it will require the user to reset their password.

This lesson will teach us how to set up control structures with Lua and write conditional statements using variables and logical operators.

Instructions

Look at the picture on the right. The first condition asks the questions “Is it a weekday?” with follow-up questions asking about the time.

Information like the current day and time are likely to be stored in a program using variables as we’ll see in this lesson.

Take a moment and follow this control structure. What will you be doing today?

  1. Stay in PJs
  2. Get dressed
  3. Don’t wear a raincoat
  4. Wear a raincoat

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?