Overview

In this lesson, students write their own if and if-else statements in the Quorum Programming Language for the first time. The concepts of conditional execution should carry over from the previous lesson, leaving this lesson to get into the nitty gritty details of writing working code. Students will write code using the online IDE for a series of problems that require them to do everything from debugging common problems, writing simple programs that output to the console, and implementing the conditional logic into an existing app or game, like "Password Checker" or a simple Dice Game.

Vocabulary

Goals

Students will be able to:

Purpose

The main purpose here is Practice, Practice, Practice. The lesson asks us to write if-statements in a variety of contexts and across a variety of program types and problem solving scenarios.

Resources

Getting Started

In everyday conversation, it is common to interchange the words "when" and "if"� as in "If the user presses the button, execute this function." The English language is tricky. We often say "if" the button is clicked when really we mean "when" a button is clicked. This can cause confusion because "if" has a well-defined meaning in programming.

How are conditionals (if-statements) different from events?

As we have already seen in prior lessons, an if-statement is evaluated when the code reaches a particular line and uses a true/false condition (like a comparison between values e.g., score = 5), to decide whether to execute a block of code.

Optional: Flow Charts

Some people find flow-charting a useful exercise for thinking about if-statements. You can work through the "(Optional) Flowcharts" activity guide with your students to warm up on paper. Alternatively, you might revisit this activity after students have had some experience writing if-statements to solidify their understanding.

Activity

Students will be introduced to conditionals by solving different types of small puzzles and writing small programs in different contexts with different kinds of output. Read the student instructions for more info.

Student Instructions

In this lesson we move from pseudocode to real code as you write if-statements. You'll learn more about the Quorum Programming Language syntax for if-statements and boolean expressions as you get practice with several small programs.

Points to pay attention to:

As you saw in the video there are a bunch of terms that are all generally talking about the same thing.

Boolean, Boolean values, Boolean expressions:

Condition, Conditionals, Conditional Statements:

Comparison Operators

A common type of condition to check is a comparison of two values. Here are 6 common comparison operators. Each compares a value on the left with a value on the right and returns a Boolean value - true or false. Most of these do what you would expect.

"=" (is equal to): Compares two values - numbers, strings, or other booleans - and returns true if they are equal, otherwise returns false.
"Hello" = "hello" returns false -- because the strings are slightly different (uppercase H and lowercase h).
"3" = 3 returns false -- because one is a text value and the other is an integer value.
(2 + 1) = 3 returns true -- because the arithmetic expression evaluates to 3.
x = 7 returns true -- when the variable x has the value 7.

"not=" (is not equal to): Compares two values - numbers, strings, or other booleans - and returns true if they are not equal, otherwise returns false.
"Hello" not= "hello" returns true -- because the strings are slightly different (uppercase H and lowercase h).
"3" not= 3 returns true -- because one is a text value and the other is an integer value.
(2 + 1) not= 3 returns false -- because the arithmetic expression evaluates to 3.
x not= 7 returns true -- when the variable x is any value other than 7.

">" (is greater than): Compares two values to see if the number on the left is greater than the number on the right.
4 > 3 returns true
3 > 7 returns false
age > 17 returns true -- when the value of the variable "age" is strictly greater than 17, otherwise false

"<" (is less than): Compares two values to see if the number on the left is less than the number on the right.
4 < 3 returns false
3 < 7 returns true
age < 17 returns true -- when the value of the variable "age" is strictly less than 17, otherwise false

"<=" (is less than or equal to): Compares two values to see if the number on the left is less than or equal to the number on the right.
4 <= 3 returns false
3 <= 7 returns true
age <= 18 returns true -- when the value of the variable "age" is 18 or less

">=" (is greater than or equal to): Compares two values to see if the number on the left is greater than or equal to the number on the right.
4 >= 3 returns true
3 >= 7 returns false
age >= 18 returns true -- when the value of the variable "age" is 18 or greater

How If-Statements Work

Explore Comparison Operators with "output"

Write simple "boolean expressions" with a single if- structure. Have the computer output something if the "boolean expression" is true. Change the value of the variables or change the comparison operator to test the boolean expression. You can try with different data types as well. The code below is an example:

integer age = 15
if age >= 13
   output "You can see a PG 13 movie alone."
end

Testing the Boolean Expression

How If-Else Statements Work

With an if - else structure you are giving an either-or command. Either the line of code after the if will execute or the lines after the else will execute. Those are the options. This statement is useful when the choice the user needs to make is only one or the other. In the code section below we modified the code from the prior example to either output one message if the boolean expression evaluates to true or output a different message if the boolean expression evaluates to false.

integer age = 9
if age >= 13
   output "You can see a PG 13 movie alone."
else
   output "You are not old enough to see a PG 13 movie alone."
end

Using If-Statements with Event-Driven Programming

So far, we have learned the concept of an if-statement in the context of console programming. Now we will learn how to apply the same concept to Event-Driven Programming by first writing a very simple console program, then writing the same program in the Event-Driven Programming. The fact is that Event-Driven Programming is not that different from the Console Programming. Rather, they are the same type of programming with different way of accepting the input data and displaying the output result.

Coding Challenge - Password Checker

Write a program that does the following:

  1. Asks the user to "Enter their password."
  2. Evaluates whether the password is either correct, or incorrect.
  3. Outputs either "Access Granted!", or "Access Denied."

A few things to remember:

Password Checker

Coding Challenge - Dice Game

Write a program that does the following:

  1. Has the computer roll the die (Randomly generating an integer between 1 and 6 and storing the value in a variable for the computer's roll).
  2. Asks the user to "Press 1" to roll their die.
  3. If the user pressed 1, Randomly generate another integer between 1 and 6 and store it in a variable for the user's roll.
  4. Compares the user roll value to the computer roll value.
  5. If the user roll is equal to the computer roll, say the value of both the computer roll and the user roll and "Tie Game."
  6. elseif the user roll is greater than the computer roll, say the value of both the computer roll and the user roll and "You Won."
  7. else, say the value of both the computer roll and the user roll and "You Lost."
  8. Use the say command when displaying the instructions and game results to make this game accesible.

First, think about the flow for this program (refer to the "Flowchart" worksheet), then write the code. The following hints may help you with the coding process:

Dice Game

Wrap Up

Compare and Contrast - easy/hard

You've now had experience reasoning about if-statements on paper with the "Will it Crash?" activity, and now actually writing if-statements in working code. Compare and Contrast these experiences.

Nested if-statements

What if we need to check more than one condition for an if-statement at the same time? For instance it is the weekend if it is Saturday or Sunday. One solution is to nest if-statements. Nesting if-statements is one way to check more than one condition at a time. There are other ways to check more than one condition at a time that we will learn about in the next lesson.

Standards Alignment