Overview

We take a whole lesson to learn about if-statements, what they are, the terminology around them, and what they have to do with "selection" in programs. Students trace simple robot programs on paper to develop a sense of how to read and reason about code with if-statements in it. Students will also try writing code by hand to handle a robot situation.

Most of the programs that students have written so far include "event handlers" that get triggered when certain events occur. In Lesson 5, they created a "Counter" program that either incremented or decremented the "count" depending on which key was pressed. In this lesson students will gain an understanding of the "if-statement structures" that were provided in the template of the "Counter" program. We will look into the concept of "if-statements" much closer so that students can construct their own "if-statement structure" for use in their programs.

Vocabulary

Goals

Students will be able to:

Purpose

The activities here get right to many common misconceptions about how if-statements work and how programs execute. Students may have a simple common-sense intuition about how if-statements work, but there are several ways you can get your wires crossed when considering how programs actually execute. There are two main issues: 1) How the flow of program execution works, and 2) How complex logical statements are composed and evaluated. In this lesson we address program flow and tracing execution. We'll look at more complex logical expressions later. Even though Boolean expressions show up in this lesson, we try to avoid using that term until the next lesson. For this lesson it's a condition that is simply true or false.

Resources

Getting Started

When vs. If

if event:keyCode = event:UP
   counter = counter + 1
   .
   .
   .
end

Prompt: I'm going to read two sentences out loud that describe a program. With a partner discuss what the difference is between them, and decide which one is "right." Here are the two sentences:

  1. When the button is clicked add one to the score.
  2. If the button is clicked add one to the score.

Give students a minute to discuss with their partner and then ask which one people think is "right." Get a few opinions out, but don't stop here.

Let's try another one:

  1. When the score reaches 20, output "game over."
  2. If the score reaches 20, output "game over."

Give students some time to discuss with their partner before asking which one people think is "right."

Discuss: Points to raise -

Activity

Today's activity focuses on if-statements. If the distinction between "when" and "if" is still a little fuzzy, that is okay.

For now, the key idea is that if-statements are a new entity that let us do things we could not do with event handlers -- writing code to make decisions about whether or not to run some other piece of code.

Big Picture: If-statements:

Key Idea: If-statements are how programs adapt and respond to conditions on the ground

The whole point is to be able to handle cases where you cannot know ahead of time whether or when certain conditions will exist in your program. So you have to write code to say something like: "Okay, at this point in the program, if such and such is true, then do this, otherwise do that."

For the activity we're going to get our feet wet with if-statements using the AP CS Principles pseudocode language. To start we're going to use the IF/ELSE structures, but to keep things simple we'll only use the Robot conditional expression CAN_MOVE (direction) - which evaluates to true or false. Use the "Annotated Pseudocode" worksheet and "Worked Example" worksheet for reference when writing your own pseudocode for your robot. The purpose of the Worked Example is to show that:

Teaching Tip:

You may want to review the worked example as a class, or you can have students read (individually or in pairs) and then follow up with a partner.

Distribute: Will It Crash? - Activity Guide

Teacher’s Tip: Activity Template

We have included an enlarged template of the diagram below for those who may have difficulty using the template provided in the activity guide. If you have access to a Tactile Image Enhancer, the template could be copied onto the heat sensitive paper. This activity could alternatively be done on any platform that has 5 by 5 grid structure.

Unit 5 Lesson7 - Tactile Grid Template.png

Compare results

Write code for the last problem and exchange

Note:

Each "if structure" has its own "end". Keeping each if-statement at the same indentation level as its end is important for acheiving readability within the code, especially when we begin using "nested if-statements" as shown below:

if (Indentation Level 1)
   if (Indentation Level 2)
       doThis(Indentation Level 3)
   else (Indentation Level 2)
       doThat(Indentation Level 3)
   end (Indentation Level 2)
else (Indentation Level 1)
   doNothing(Indentation Level 2)
end (Indentation Level 1)

Wrap Up

What was trickiest? If there were common problems that students had trouble with be sure to review those.

Prompt:

Discussion:

Extended Learning

Algorithms - Solving Problems

What is an algorithm? An algorithm is a precise sequence of instructions for a process that completes a task. Algorithms can be executed by a computer and are implemented using programming languages.

Automating Physical Tasks

Algorithms and Information Tasks

Algorithms, Programming, and Computer Science

Algorithms and Creativity

Any programming language only provides so many commands. Algorithms are created by combining these instructions in three ways. In fact, using these three methods you can describe ANY algorithm completed by a computer. Those three ways are:

Algorithms, Programming, and Creativity: Even with the limited commands a computer understands and the limited ways you can combine them, there are actually many, conceivably infinite, ways to write a program to complete a task. Some may be more efficient or easier to understand than others, but there is typically no single "right" algorithm to complete a task. There also isn't an "algorithm for writing algorithms." You need to investigate and understand the problem you are trying to solve, and then get creative with how you'll combine the tools the programming language provides you. Computer science is a creative discipline because computers literally require human creativity to do anything at all!

Algorithms, Unit 5, and the AP Exam

Algorithms and AP Computer Science Principles

  1. "Algorithms" is one of the seven big ideas of AP Computer Science Principles.
  2. For the AP Create Performance Task you need to ...
    [identify] a code segment that contains an algorithm you developed...[and]...explain how the algorithm helps achieve the purpose of your program.

Programming is a creative activity. When you are planning a solution to a problem you are thinking about algorithms.

Discussion:

Standards Alignment