Lesson 4: Using Simple Commands

Overview

This lesson is a student's first exposure to programming in the App Lab environment. App Lab is embedded into Code Studio, and for this lesson the student's view is limited to only a very few simple “turtle” commands to draw graphics on the screen. After a few warm up exercises, using only combinations of four drawing commands, students must figure out the most “efficient” way to draw an image of a 3x3 grid. Though the students’ focus should primarily be on solving the problems at hand, students also get acquainted with the programming environment that they will be using for the rest of the course. The environment is intentionally bare bones at this point, the many features of App Lab will be gradually revealed over time.

Purpose

The main purpose of this lesson is start students on their programming journey in such a way that the focus is more on problem-solving than learning syntax. Like the previous lessons about algorithms, by stripping down the available commands to only a few, these constraints force students to think about the most efficient way to solve the problem and come up with creative ways of doing it.

A reason to learn this way is because at its core, a computer can really only do a few simple things: load a number from memory, store a number in memory, add two numbers together, etc. Everything the computer can do is the result of combining these simple instructions to do more and more sophisticated things. Students in this class will embark on a similar journey, building up complexity from only a few primitive sets of commands. As students are presented with increasingly complex tasks, we will progressively expose more commands as a matter of convenience.

Big picture: when programming at any level of expertise you are always limited by the constraints of the programming language you choose to use. Whether the language gives you thousands of commands and libraries with which to control various things or only a few, you always have to use those as building blocks to get the computer to do what you want. It usually involves being creative and making interesting choices about how to go about doing something. In this lesson, we simplify things dramatically by only providing 4 turtle drawing commands (namely moveForward, turnLeft, penUp and penDown) to produce simple line drawings. In fact, the problems can be solved using only moveForward and turnLeft!

Agenda

Getting Started (5 mins)

Activity (30 mins)

Wrap-up (15 mins)

Extended Learning

Assessment

View on Code Studio

Objectives

Students will be able to:

  • Solve simple programming challenges when the set of allowed commands is constrained.
  • Explain considerations that go into “efficiency” of a program.
  • Use App Lab to write programs that create simple drawings with “turtle graphics.”
  • Work with a partner to program a turtle task that requires about 50 lines of code.
  • Justify or explain choices made when programming a solution to a turtle task.

Preparation

  • Familiarize yourself with opening remarks and comments for background knowledge.

Links

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

For the Students

Vocabulary

  • Turtle Programming - a classic method for learning programming with commands to control movement and drawing of an on-screen robot called a "turtle". The turtle hearkens back to early implementations in which children programmed a physical robot whose dome-like shape was reminiscent of a turtle.

Introduced Code

Teaching Guide

Getting Started (5 mins)

Recall challenges of the Algorithms Activities

Discussion Goal

After the prompt you might say the differences between acting out algorithms with playing cards and programming on the computer are pretty obvious. By focusing on what will be the same, we can reveal the purpose of those activities and set expectations for programming moving forward.

Some things that will be the same to key in on (students might suggest others):

  • Focus on creating processes to do things
  • Multiple ways to solve a problem
  • Some struggles understanding exactly what commands mean
  • Working with partners
  • Reasoning about solutions by testing them repeatedly, acting them out
  • Creatively applying the limited set of commands.

Prompt:

"We have been preparing to learn how to program by doing some activities with LEGO® and playing cards. Today we will start programming for "real." Thinking back to the algorithms activities what do you anticipate will be the same about those activities vs. the 'real' thing?"

  • Give students a moment to think to themselves, perhaps write something down.
  • Share with a partner.
  • Share with the whole group.

Teaching Tip

  • You don’t necessarily have to deliver all of the messages to the left (Transition and On to programming!) as part of getting started.
  • They are important ideas to have in your mind as we move forward in the unit, especially if students ask why we’re doing things in a certain way.
  • The messages are important for setting up the philosophy of how we will learn programming in the course and how the programming unit connects to the prior units.
    • Basically, we’re doing the same thing as we’ve done with other units: start with simple elements, solve problems. Once we’ve solved those problems, assume they are solved, and add complexity and abstraction.
  • It’s also important to set up programming as an empowering, but not intimidating undertaking.

Introduce the challenge and fun of programming

Remarks

Transition

  • We are about to start a unit on computer programming, in which you will write instructions for a computer to execute.
  • Computers are machines. So if we invent an instruction or command for a computer to execute, then it should be unambiguous how the computer will interpret or attempt to execute that instruction; at the very least we can expect that the machine’s behavior is repeatable, so we can run some tests until we understand what the computer is doing and why.
  • So then the challenge - and fun - of programming at its core, is understanding how to use and combine those machine instructions to make the computer do what you want, or to solve a problem.

On to programming! How we will learn.

In this course, and in computer science in general, all of the complexity we see on a computer is actually just the composition (the combining and recombining) of a few simple elements.

  • We saw this in Unit 1 with the Internet, as we built up complex routing and naming protocols, all from solving the initial problem of how to coordinate communication between two people on a shared wire.
  • We saw this in Unit 2 with complex data types, all built up from simple binary codes.
  • We will do the same thing with programming! To learn about programming we are going to solve problems with only a few primitive commands. You will learn how you can build up very complex computer programs from only a few simple elements.

We start this journey today.

Activity (30 mins)

Introduction to App Lab

Remarks

App Lab is the programming environment we’re going to use for the rest of the course to write programs and apps. App Lab is embedded into Code Studio for many lessons and usually presents you with a series of problems to solve to learn the basic concepts. As you get better and better at coding, App Lab will show you more and more things you can do. But to start, we’re going to keep things simple and build up the complexity.

Turtle programming problems in Code Studio (up to 3x3 grid)

Transition to the Using Simple Commands stage on Code Studio

  1. Put students into pairs to work together for the rest of the class.

  2. Have students log in to Code Studio for this lesson and with a partner proceed through the problems.

    • Each person should do her own work, and check in with her partner at the conclusion of each problem to compare solutions before proceeding to the next one.

Wrap-up (15 mins)

Discussion: What does “efficiency” mean when programming?

Compare Solutions and Discuss: Each pair of students should get together with another pair to compare their solutions for each of the three problems to determine which is the most efficient. Once they have shared and compared their solutions they can consider the questions below.

  • What strategies or reasoning did you use to identify possible solutions?
  • Is the solution that you or another group found the most efficient? How do you know?

Thought Prompt: What is the “most efficient” way to program the solution for the 3x3 grid?

Discussion Goal

The point of this discussion is to highlight the fact that "efficiency" can mean different things depending on the context.

Potential answers to this question can include the time they take to run on a computer, the time or number of people they require to create, the number of resources they require to run, etc. Students will likely come up with others.

Don’t fixate on any one solution and encourage students to come up with the most comprehensive list possible.

Discuss: What does “efficiency” mean? Students should discuss the question below briefly in small groups, and then they will be sharing their ideas with the class after they have had a minute to brainstorm. Prompts:

  • Today’s activity challenged us to find the most efficient solution to a problem. We care about efficiency when we don’t want to waste something valuable, like money, time, or space. We measured the efficiency of our programs in terms of lines of code, but there are other ways to think about efficiency when talking about code or programs that run on a computer. When we try to create efficient programs, what other valuable resources might we be concerned about conserving?

  • List the things that students come up with somewhere that can be read by the class. These can be used to motivate portions of the next and future lessons.

What's the point? Why constrain to 4 commands

Teaching Tip

  • There are a lot of points in this “What’s the Point?” section. They all go together and they are not all points you have to make in this lesson right now.
  • You should keep these big ideas in your back pocket, because you may want to refer back them in the future.
  • The short version:
    • Creativity in programming comes from the fact that all programming languages have constraints; your job is to figure out how to use the language to get the computer to do what you want it to do or to solve a problem.
    • Collaboration is a huge help when solving problems with programming. It’s helpful to work through problems with a partner.

Remarks

Why did we constrain you to only working with 4 possible commands? We begin programming with only a few commands for a few main reasons:

  1. What you did today is, in a microcosm, what you always do when programming: use a language to express a solution to a problem. Collaborative problem-solving skills are important factors in programming. Being able to get together with other people to talk about and make reasoned arguments about the best ways to code things is what professionals do.
  2. Learning to program isn’t just a matter of memorizing commands. The art of programming - the creative part - is always about understanding how to use the features of a programming language to solve a problem.
  3. Whether you know 4 commands of a language or hundreds, you will always be constrained by the programming language. The reason you need to learn how to program is because there isn’t a command to do every little thing; you have to understand the set of things that a programming language can do, and then use your creativity and problem-solving skills to get the computer to do what you want.
  4. In this class, we will repeatedly run into the challenges we encountered today. Even seemingly simple problems will require us to creatively apply the tools we have available: the commands or instructions provided by the programming language.
  5. Multiple approaches are always possible to solve a problem. Furthermore, we may never be sure we have found the most efficient one. We may not even agree about how we should measure efficiency.
  6. As we saw today, even when designing programs using four commands to draw with a turtle, the solutions are not always easy. We will definitely need to keep practicing as we start tackling more complex challenges.

Extended Learning

Ask students to propose ways they could prove that their solution to the 3x3 grid problem is the most efficient. Note: An actual proof is very challenging.

Assessment

Criteria:

  • If you want to assess students’ programs (especially 3x3 grid) you can see their work in the answer viewer and you might look for the following criteria:
    • The program draws the desired figure.
    • The turtle is returned to its starting location and is pointed in the correct direction.
    • The figure is located in the correct position, relative to the starting position of the turtle.
    • The program only makes use of the “building block” commands provided.
    • The program is reasonably efficient in its execution.

Questions in Code Studio:

  • This lesson introduced the notion of "efficiency" in programming, and that it might mean different things at different times. Think of an example outside of computer science in which you have heard the term "efficiency" and compare it to the ways we talked about efficiency in programming. In what ways is the meaning of "efficiency" the same? In what ways is it different?
  • Today we solved a series of problems with a limited set of commands (only 4). Give at least one reason why it's useful to learn how to solve, and program solutions to problems with a limited set of commands.
  • Summarize your experiences with this first AppLab lesson by answering the following questions:
    • What surprised you about programming with such a small set of basic commands?
    • Were you able to be creative with such a limited set of tools?
    • What was most frustrating about this activity? If you could add one additional simple command, what would it be, and why?

These assessment questions ask students to draw things and are not in Code Studio:

  • Draw (on paper) the simplest image you can that we would be unable to create with our “building block” commands, and explain why it would be impossible to create.

    • Solution: Anything with a non-90-degree angle, curved, or with different colors, cannot be done with the given set of simple commands.
  • Draw a second image we would be unable to create with the given simple commands, but for a different limiting reason than you cited in the first drawing.

View on Code Studio

Unit 3 - Lesson 4: Using Simple Commands

Lesson

  • Learn to use the App Lab programming environment
  • Write a program that uses basic "building blocks" to draw an image in App Lab
  • Think about what "efficiency" means when programming

Vocab

  • Turtle Programming - a classic method for learning programming with commands to control movement and drawing of an on-screen robot called a "turtle". The turtle hearkens back to early implementations in which children programmed a physical robot whose dome-like shape was reminiscent of a turtle.

Introduced Code

  • Using Simple Turtle Commands
  • 3
  • 4
  • (click tabs to see student view)
View on Code Studio

Teaching Tip

The constraint of working with only 4 commands is important. (See: “What’s the Point?” below.) If students come to the class with prior knowledge of programming they might try to go outside these constraints to reduce the number of lines of code.

If you see students using commands beyond the 4 provided - or using parameters with those commands like: moveForward(150), you should tell them that they are not solving the problem; the problem is to create the images using only the 4 commands provided.

Student Instructions

Given only 4 turtle commands, write code in App Lab to complete the drawings in each of the challenges below. Your goal is to write code that solves the problem as efficiently as possible using only the commands provided.

What does “efficient” mean? In computer science it can actually mean several different things depending on what you’re talking about. Work on these challenges, try to solve them in a way that you think is best and we’ll ask about efficiency at the end.

Do This:

Draw a 1 x 1 square to the front and left of the turtle as efficiently as possible. The program should stop with turtle in its original position, facing its original direction.

Once you think you have a solution, click the Finish button - it will appear once you run the program.

View on Code Studio

Teaching Tip

The insight here is that we can turn right by turning left 3 times.

Student Instructions

Do This:

This time, draw a 1 x 1 square to the front and right of the turtle as efficiently as possible. Notice that there is no command to turn right! You need to figure out how to draw this by only turning left and moving forward. The program should stop with turtle in its original position, facing its original direction.

When you're done click the Finish button to move onto the next problem.

  • Challenge: Draw a 3x3 Grid Using Turtle Commands
  • 5
  • (click tabs to see student view)
View on Code Studio

Teaching Tip

  • Students will need some time to work on the 3x3 grid problem; it is more challenging than it appears at first. * Students will ask you if you know the least possible number of lines of code needed to draw the 3x3 grid. Rather than giving an answer, ask student pairs to compare their solutions with other pairs. If they agree on a solution, see if you get a class consensus.

  • You can also ask: Can you do it with fewer commands? If not, can you prove that it’s impossible to do with fewer?

  • Questions like these are the kinds that programmers ask each other all the time. And programmers usually work with each other to reach a solution.

  • An example of a solution is given in the teacher tab for the problem in Code Studio, but it is just an example; there are millions of ways to do the 3x3 grid.

Student Instructions

Do This:

Draw a 3x3 grid to the front and right of the turtle as efficiently as possible. The turtle should end at its original location, facing its original direction.

HINT: This problem presents some subtle challenges. Work with your partner to discuss possible solutions. Try more than one path. Testing your ideas on paper might be a faster way to brainstorm.

  • Check Your Understanding
  • 6
  • 7
  • 8
  • 9
  • (click tabs to see student view)
View on Code Studio

Student Instructions

This lesson introduced the notion of "efficiency" in programming, and that it might mean different things at different times. Think of an example outside of computer science in which you have heard the term “efficiency” and compare it to the ways we talked about efficiency in programming.

In what ways is the meaning of "efficiency" the same? In what ways is it different?

View on Code Studio

Student Instructions

Today we solved a series of problems with a limited set of commands (only 4). Give at least one reason why it's useful to learn how to solve and program solutions with a limited set of commands.

View on Code Studio

Student Instructions

In this lesson you wrote code to make the turtle draw squares. Briefly describe how the code for drawing a rectangle would be different than drawing a square. (Example rectangle at right)

View on Code Studio

Student Instructions

Summarize your experiences with this first App Lab lesson by answering the following questions:

  • What surprised you about programming with such a small set of basic commands?
  • Were you able to be creative with such a limited set of tools?
  • What was most frustrating about this activity? If you could add one additional simple command, what would it be and why?

Standards Alignment

View full course alignment

CSTA K-12 Computer Science Standards (2011)

CL - Collaboration
  • CL.L2:3 - Collaborate with peers, experts and others using collaborative practices such as pair programming, working in project teams and participating in-group active learning activities.
CPP - Computing Practice & Programming
  • CPP.L2:4 - Demonstrate an understanding of algorithms and their practical application.
  • CPP.L2:8 - Demonstrate dispositions amenable to open-ended problem solving and programming (e.g., comfort with complexity, persistence, brainstorming, adaptability, patience, propensity to tinker, creativity, accepting challenge).
  • CPP.L3A:3 - Use various debugging and testing methods to ensure program correctness (e.g., test cases, unit testing, white box, black box, integration testing)
  • CPP.L3A:4 - Apply analysis, design, and implementation techniques to solve problems (e.g., use one or more software lifecycle models).
CT - Computational Thinking
  • CT.L2:1 - Use the basic steps in algorithmic problem-solving to design solutions (e.g., problem statement and exploration, examination of sample instances, design, implementing a solution, testing and evaluation).
  • CT.L2:3 - Define an algorithm as a sequence of instructions that can be processed by a computer.
  • CT.L2:6 - Describe and analyze a sequence of instructions being followed (e.g., describe a character’s behavior in a video game as driven by rules and algorithms).

Computer Science Principles

5.1 - Programs can be developed for creative expression, to satisfy personal curiosity, to create new knowledge, or to solve problems (to help people, organizations, or society).
5.1.2 - Develop a correct program to solve problems. [P2]
  • 5.1.2A - An iterative process of program development helps in developing a correct program to solve problems.
  • 5.1.2I - A programmer’s knowledge and skill affects how a program is developed and how it is used to solve a problem.
5.1.3 - Collaborate to develop a program. [P6]
  • 5.1.3B - Collaboration facilitates multiple perspectives in developing ideas for solving problems by programming.
  • 5.1.3C - Collaboration in the iterative development of a program requires different skills than developing a program alone.
  • 5.1.3D - Collaboration can make it easier to find and correct errors when developing programs.
  • 5.1.3F - Effective communication between participants is required for successful collaboration when developing programs.
5.2 - People write programs to execute algorithms.
5.2.1 - Explain how programs implement algorithms. [P3]
  • 5.2.1A - Algorithms are implemented using program instructions that are processed during program execution.
  • 5.2.1B - Program instructions are executed sequentially.
5.4 - Programs are developed, maintained, and used by people for different purposes.
5.4.1 - Evaluate the correctness of a program. [P4]
  • 5.4.1A - Program style can affect the determination of program correctness.
  • 5.4.1D - Longer code blocks are harder to reason about than shorter code blocks in a program.
  • 5.4.1E - Locating and correcting errors in a program is called debugging the program.
  • 5.4.1I - Programmers justify and explain a program’s correctness.