Lesson 7: Conditionals Practice

App Lab

Overview

In this lesson students spend most of their time practicing using the skills and processes they have learned about conditionals. At the conclusion of the lesson students discuss the main things they realized and still have questions about at the conclusion of this lesson.

Purpose

This lesson is students primary opportunity to get hands on with conditionals in code prior to the Make activity in the following lesson. Give students as much class time as you can to work through these. For this lesson it's recommended that you place students in pairs as a support and to encourage discussion about the challenges or concepts they're seeing. In the following lesson students are encouraged to work independently.

Agenda

Lesson Modifications

Warm Up (5 mins)

Activity (35 mins)

Wrap Up (5 mins)

View on Code Studio

Objectives

Students will be able to:

  • Write programs that use boolean expressions and conditional statements with the support of sample code.
  • Debug programs that use boolean expressions and conditional statements

Preparation

  • Review the programming challenges students will be completing
  • Review the Debugging Guide for ideas on how to support your students during the lesson

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the Teachers

Teaching Guide

Lesson Modifications

Attention, teachers! If you are teaching virtually or in a socially-distanced classroom, please read the full lesson plan below, then click here to access the modifications.

Warm Up (5 mins)

Quick Warm Up

Teaching Tip

Move Quickly to the Activity: There's a lot in the main activity of today's lesson. You may optionally wish to do a quick vocabulary review or address any questions that came up in the last lesson. Otherwise, give students more time to get hands on with some code.

Remarks

Today we're going to have a chance to practice programming with a lot of the concepts and patterns we've explored over the last two lessons. I encourage you to go through these with a partner, but pay close attention to what each other is doing. In our next lesson you're going to have to use a lot of these on an independent project, and these activities are good practice for what you'll find there! Alright, let's get to it!

Activity (35 mins)

Practice Time

Group: It is recommended that students work in pairs for this lesson and a number of the activities feature discussion prompts. Consider using pair programming, having drivers and navigators switch every 3 minutes, not every level.

Do This: Direct students to Code Studio, Lesson 3 Level 2. Then briefly remind students about debugging skills that they will be using in today's activity.

Remarks

Today you're mostly going to practice what we've learned about programming with conditionals. As always you should be using the debugging process to help you as you work on issues. Today we're also going to be working on finding two types of errors

  1. Syntax errors show up when you type code that breaks the rules of the programming language. You can check for errors and warnings
  2. Logic errors show up when you type valid code but it works incorrectly. Today you're going to focus on testing your code to make sure you don't have logic errors.

Other errors you may encounter include:

  • Run-time error - a mistake in the program that shows when running the program. These are defined by the programming language.
  • Overflow error - an error that occurs when a computer tries to handle a number outside of the defined range of values.

Teaching Tip

Providing Support: Circulate around the room through the lesson encouraging students to use the strategies introduced at the beginning of the lesson. Students have a number of supports at their fingertips, so a big part of your role is helping build their independence in using those resources.

Levels 2-4: These levels only use the console.log() command which prints commands in the debug console. Here are a few things to keep an eye out for

  • Levels 2-3 ask students to write Boolean expressions using comparison operators. Students may need to quickly review the comparison operators <, >, <=, >=, ==, !=
  • Level 4 asks students to write Boolean expressions with logical operators &&, ||, !

Levels 5-9: These levels practice if-statements while working with a star color-changing app.

  • Levels 5-6 involve setting up an if-statement that becomes an if-else statement.
  • In Level 7 students follow a pattern to create a lengthy if-else-if statement.
  • For Level 8, make sure students slow down the running of the code to understand what's happening. It's suggested that students use the slider to slow down the code.
  • Level 9 demonstrates that Boolean expressions can be written as conditional statements, and vice versa

Levels 10-11: The levels return to the "Can I Adopt a Cat?" flowchart from the Conditionals Explore activity. Students will use the flowchart to work out the logic of the if-statements in a their program.

  • A new block appears in these levels: getNumber() . This is different than getText(). getNumber() gets a number from a user input that can be used mathematically.
  • Level 11 can be completed many different ways. There are different combinations of Boolean expressions using && and ||. Students should regularly test their apps to see if their Boolean expressions are working properly.

Extension Opportunities:

  • Level 4: Students can add more variables and create complex Boolean expressions. One challenge might be to assign a String to a variable and compare that string to another.
  • Level 10: There are multiple solutions. If students build their if-statement using only && encourage them to figure out how to build it using only ||. They may need to switch the content of the if and else branches.
  • Level 11: Create another input (i.e. How many cats do you already own?). Students use this information to craft more complex if-statements.

Wrap Up (5 mins)

Discussion Goal

Goal: Use this opportunity to address any lingering questions or misconceptions in the room. You can also use this as a source of discussion topics to kick off the following lesson. As you lead the discussion, call out the many resources students have access to help when they're getting stuck.

Prompt: What aspects of working with conditionals do you feel like clicked today? What do you still feel like you have trouble with?

Remarks

Conditionals can be a little bit tricky, but I saw a lot of good progress today in nailing down this concept. We may have a few lingering questions, but you also have a lot of resources available. Next time you'll have a chance to put all this together by programming an app that starts with "the blank screen"!


Assessment: Check For Understanding: AP Practice

Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.

Question: What will be displayed after this code segment is run?

Question: The program below asks a user to type in a number and then will output a message. What number will a user need to input for the message "COLD" to be displayed?

number <- INPUT()

IF (number >= 10)
{
  IF (number <= 20)
  {
    DISPLAY("MEDIUM")
  }
  ELSE
  {
    DISPLAY("HOT")
  }
}
ELSE
{
  DISPLAY("COLD")
}

Standards Alignment

View full course alignment

CSTA K-12 Computer Science Standards (2017)

AP - Algorithms & Programming
  • 2-AP-10 - Use flowcharts and/or pseudocode to address complex problems as algorithms.
  • 2-AP-12 - Design and iteratively develop programs that combine control structures, including nested loops and compound conditionals.

CSP2021

AAP-2 - The way statements are sequenced and combined in a program determines the computed result
AAP-2.E - For relationships between two variables, expressions, or values: a. Represent using relational operators b. Evaluate expressions that use relational operators.
  • AAP-2.E.2 - The exam reference sheet provides the following relational operators: =, ≠, >, <, ≥, and ≤. ●       a = b●       a ≠ b●       a > b●       a < b●       a ≥ b●  a ≤ b These are used to test the relationship between two variables, expressions, o
AAP-2.F - For relationships between Boolean values: a. Represent using logical operators. b. Evaluate expressions that use logic operators.
  • AAP-2.F.1 - The exam reference sheet provides the logical operators NOT, AND, and OR, which evaluate to a Boolean value.
  • AAP-2.F.2 - The exam reference sheet provides the NOT condition, which evaluates to true if condition is false; otherwise it evaluates to false.
  • AAP-2.F.3 - The exam reference sheet provides condition1 AND condition2, which evaluates to true if both condition1 and condition2 are true; otherwise it evaluates to false.
  • AAP-2.F.4 - The exam reference sheet provides condition1 OR condition2, which evaluates to true if condition1 is true or if condition2 is true or if both condition1 and condition2 are true; otherwise it evaluates to false.
AAP-2.H - For selection: a. Represent using conditional statements. b. Determine the result of conditional statements.
  • AAP-2.H.2 - The exam reference sheet providesIF(condition){<block of statements>}in which the code in block of statements is executed if the Boolean expression condition evaluates to true; no action is taken if condition evaluates to false.
  • AAP-2.H.3 - The exam reference sheet providesIF(condition){ <first block of statements>}ELSE{ <second block of statements>}in which the code in first block of statements is executed if the Boolean expression condition evaluates to true; otherwise, the code i
AAP-2.L - Compare multiple algorithms to determine if they yield the same side effect or result.
  • AAP-2.L.3 - Some conditional statements can be written as equivalent Boolean expressions.
  • AAP-2.L.4 - Some Boolean expressions can be written as equivalent conditional statements.
AAP-3 - Programmers break down problems into smaller and more manageable pieces
AAP-3.A - For procedure calls: a. Write statements to call procedures. b. Determine the result or effect of a procedure call.`
  • AAP-3.A.9 - The exam reference sheet provides procedure INPUT(), which accepts a value from the user and returns the input value.
CRD-2 - Developers create and innovate using an iterative design process
CRD-2.I - For errors in an algorithm or program: a. Identify the error. b. Correct the error.
  • CRD-2.I.1 - A logic error is a mistake in the algorithm or program that causes it to behave incorrectly or unexpectedly.
  • CRD-2.I.2 - A syntax error is a mistake in the program where the rules of the programming language are not followed.
  • CRD-2.I.3 - A run-time error is a mistake in the program that occurs during the execution of a program. Programming languages define their own run-time errors.
  • CRD-2.I.4 - An overflow error is an error that occurs when a computer attempts to handle a number that is outside of the defined range of values.
CRD-2.J - Identify inputs and corresponding expected output or behaviors that can be used to check the correctness of an algorithm or program.
  • CRD-2.J.2 - Defined inputs used to test a program should demonstrate the different expected outcomes that are at or just beyond the extremes (minimum and maximum) of input data.