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.
Students will be able to:
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.
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.
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.
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.
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.
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:
10 > 25a legal expression?
As you saw in the video there are a bunch of terms that are all generally talking about the same thing.
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
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
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
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.
Write a program that does the following:
A few things to remember:
Write a program that does the following:
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'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.
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.