Goals

The goal of this lab is to learn the following:

Computer Science Principles Curriculum

Common Core Standards

Vocabulary

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!

Activity:

Use conditional statements to find out users’ computer preferences.

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’ phone 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.

Activity:

Use conditional statements to find out users' game console preference. Use these 5 choices in your activity: PlayStation, Xbox, Wii, PC, or other option. Run your program and see if it works properly. Show your work to your instructor.

Next Tutorial

In the next tutorial, we will discuss Lab 3.2, which describes how work Loops in Quorum..