Goals

The goal of this lab is to learn the following:

  • Understanding conditionals
  • Using conditionals

Computer Science Principles Curriculum

  • Big Idea: Programming: EK 5.1.2B, EK 5.1.2E, EK 5.2.1F, EK 5.1.2J, EK 5.2.1C, EK 5.3.1D, EK 5.3.1I, EK 5.4.1
  • Big Idea: Data and Information: EK 3.1.1B

Common Core Standards

  • English Language Arts Standards » Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.6, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3
  • Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8

Vocabulary

  • Conditional statement
  • If
  • Else if
  • Else
  • End
  • Lexical scope

Overview

The goal of this lab is to learn the fundamentals of conditional statements and learn how to write Quorum programs using them.

Goal 1: Learn about conditionals.

Conditionals are one of the most important aspects of programming. Conditional statements allow the program to make decisions, changing the behavior of the software during runtime. So far, your code has run from top to bottom, with the same behavior each time. Conditional statements allow your program to alter its behavior depending on any condition you specify. Conditionals can be used for a variety of tasks such as determining if a number given by a user is less than the number 100, or if the battery of your phone is about to die.

To use conditionals you use certain statements: if, elseif, else and end. The if is the first command in your block of decision. If this condition is not true your program will move on to the elseif command if it is available. Otherwise, it will move to the else statement. The elseif is not always used. You only use it when you have more than two decisions that must be made. If the program goes between the if and else statements in the block, and neither condition is met (or true), your program will go to the elseif, if it is available, or directly to a final else, if you choose to do that instead. In this case the else is the final statement in the decision block and must be used, it being the last decision the computer can make if all the other decisions are false. Once your program decides your condition is true it will go to the end of the block and move on.

If you define a variable inside of the if statement, in Quorum, this means it only exists inside the scope of the if statement, between the if and end keywords. This is what is called lexical scoping, and it is very useful to keep a program organized. In the case that you want the variable to be accessible outside of the ifstatement's scope, you have to define the variable above the if statement.


Example: Use conditional statements to find out users' phone preferences.

text answer = input ("Do you use apple or android?")
if answer = "Apple"
   output "I like the iPhone too!"
else
   output "Android is cool!"
end

Run the program and there should not be any errors. Do not forget where you should enter your preference. If you wish, try other questions and answers as well!

Use Mac and Windows as your choices; do not forget to output a phrase for each preference.

Goal 2: Using conditionals

Now that you understand why and how to use conditional statements, let’s have some fun using them. We will start with a program that asks the users’ breakfast preferences.


Example: Use conditional statements to find out users' breakfast preferences.

text choice = input ("What do you prefer for breakfast? Press 1 for eggs, press 2 for pancakes or press 3 for something else.")
if choice = "1"
   output "I like mine over easy!"
elseif choice = "2" 
   output "I love butter and syrup with them!"
else
   output "It is great to have so many choices!"
end

As you can see in the example above, you can insert a number in this case, as a text, but you can only use it in your block of decision if you have quotes.

Next Tutorial

In the next tutorial, we will discuss repetition, which describes how to do operations over and over again.