## 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

• Boolean: A single value of either TRUE or FALSE
• Boolean Expression: in programming, an expression that evaluates to TRUE or FALSE.

## Goals

Students will be able to:

• Write and test conditional expressions using comparison operations
• Given an English description, write code (if-statements) to create desired program logic
• Use the comparison operators (<, >, <=, >=, =, not=) to implement decision logic in a program.
• Use if, if-else, or nested if-statements to express desired program logic

## 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.

## 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?

• Events are setup by a programmer, but triggered by the computer at any moment in time.
• If-statements are a way a programmer can have their code make a decision during the normal flow of execution to do one thing or another.

As we have already learned 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:

• What's a Boolean Expression?
• Why is it called a "Boolean"?
• Is `10 > 25` a legal expression?

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:

• A Boolean value is simply a computer science term that means a true/false value.
• A Boolean expression is a statement that evaluates to a Boolean value (a single true/false).

#### Condition, Conditionals, Conditional Statements:

• "Conditional" is simply a generic term for code that alters program flow based on true/false values (like an if-statement)

### 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 determine 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 determine 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 determine 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 determine 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

### 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 go to 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 go to a PG 13 movie alone."
else
output "You are not old enough to go to 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:

2. Evaluates whether the password is either correct, or incorrect.

A few things to remember:

• To compare two values they must be the same type. For instance, if your "password" is an integer type, then you will have to cast the user input to an integer type as well. "735" is not the same thing as 735.
• If your password is a text value type, then the case must match as well. "Password" is not the same as "password."

#### 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:

• You will need to declare variables to store the random integer and use the RandomIntegerBetween() action to generate the values to store in those variables.
• You may need to use the casting technique to cast the user input into a "calculable" or "comparable" variable type depending on how what type you will use when declaring the userInput variable.
• You will need to use the if, elseif, else statement structure that we have provided to determine which message will be displayed to the screen. (Is the Game Tied, Did the User Win, or Did the Computer Win?)
• Refer to past lessons if you need help with declaring variables, "casting" user input, or calling the RandomIntegerBetween() action.

## 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.

• For "Will it Crash" - what was easy, what was hard?
• For this lesson, writing if-statements - what was easy, what was hard?
• If there was one thing you wish you understood better at this point, what would it be?

#### 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

• CSTA K-12 Computer Science Standards (2011): CL.L2:3
• CSTA K-12 Computer Science Standards (2011): CPP.L2:5
• CSTA K-12 Computer Science Standards (2011): CT.L2:1, CT.L2:14, CT.L2:3, CT.L2:6, CT.L2:8
• Computer Science Principles: 4.1.1(A, B, C)
• Computer Science Principles: 5.5.1(E, G)