Helping Kids with Coding For Dummies
Book image
Explore Book Buy On Amazon
Infrastructure is something adults hear a lot in the grown-up world. It also plays a significant role in coding. Infrastructure is the physical plan of the world. It consists of the same components in any city on earth: roads, water pipes, electricity grids, and other items that give structure and organization to designed human habitats. Without infrastructure, daily life would appear chaotic!

Just like cities, computer programs possess infrastructure. At a very basic level, infrastructure consists of three key components:

  • Sequence
  • Selection
  • Repetition
These components provide the general plan for designing and executing a computer program.

Sequence

Sequence is the order in which a process is conducted. Every computer program must be organized so that steps are executed in a logical order. For example, ask your coder to order the steps in a simple algorithm. The steps include answering Rocky; saying Rocky, roll over!; and asking What is the name of your dog?

Your coder should be able to recognize that you must first ask the name of the dog (query); then receive the answer (input); and then, remembering that Rocky is the name, say, Rocky roll over (output).

As a Scratch program, the algorithm looks something like this.

Scratch algorithm coding Steps sequenced in a logical order in a Scratch algorithm.

Understanding the order in which input and output must occur is just one example of sequence in a computer program.

Invent some other sequencing tasks for your kid to try. What about gardening, baking brownies, or washing the car? What are the most important steps of each task, and how must they be sequenced?

Selection

Selection means choosing a path based on certain conditions. For example, when choosing a university to attend, you may decide between West Coast or East Coast. The decision you make then directs you to new sequences — and consequently, other selections — that relate to your choice. If you choose West Coast, then Stanford is on your list of potential universities, but not Harvard. Program commands associated with a decision that you didn’t make aren’t executed.

When coding, selection allows the programmer to create as many paths as needed to respond to the conditions of the program. Selection often involves conditional statements that are structured this way: if <em>[condition occurs]</em> then <em>[execute consequence]</em>. Conditional statements can be structured in different ways, depending on what the program needs to accomplish. The image below shows three conditional command structures your coder can use in Scratch.

conditional commands Scratch Conditional commands in Scratch.

Here are some everyday examples, in standard English, that match the structure of the Scratch conditional commands:

  • if everyone completes homework by dinnertime then we'll eat at Olive Garden
  • if you eat oranges then you are a healthy pirate else you have scurvy
  • wait until countdown equals zero
Conditional statements that use an if-then-else statement evaluate whether the if condition is true or false: true determines the selection of one command sequence, while false determines the selection of a different command sequence. Conditional statements that use a wait until statement run when the condition (the statement following until) is true.

The Choose Your Own Adventure app makes frequent use of conditionals. Can you and your coder think of other examples in which conditional statements are used in everyday life?

Repetition

Repetition is the process of repeating something. You may have said to your kid, “How many times have I told you to take out the trash — I just keep repeating myself again and again, arrghh!” Unlike grownups, computers are happy to repeat themselves over and over. In fact, repetition is an important task when coding, and understanding how to write code blocks that repeat is critical to structuring your computer programs.

Using a repeat command is one way your coder can create a loop in his code. A loop is a structure that tells the computer to run the same commands multiple times, without writing those same commands over and over. Like a traffic circle (which we've all been stuck on many times!), you can continue looping around, again and again until there’s a reason to eventually exit the loop. Creating a loop is similar to creating a chorus in a song: You can provide a notation that indicates a lyric should be sung eight times without writing it eight times:

repeat 8 ["Come on, be my baby, come on"]
Actually writing this bridge from Ed Sheeran’s “Shape of You” requires eight lines, but using code, your kid can produce the same result in a single line. Repetition rocks!

Depending on the programming language your coder is using, loops are handled somewhat differently.

loop commands Scratch Loop commands in Scratch.

Following are some examples, in standard English, that match the structure of the Scratch loop commands:

  • repeat 2 [apply shampoo, lather, rinse]
  • repeat 4 [drive to the end of the block, turn right]
  • forever [move forward 10 steps, wait 1 second, move backward 10 steps, wait 1 second]
  • repeat until [the value of the jackpot exceeds $100 million]
In the first example, the repeat command indicates that the steps, “apply shampoo, lather, rinse” repeat two times. The second example demonstrates how to drive a square-shaped path in your neighborhood, ending up at your starting point. Example three shows a forever command that results in a bouncing motion that continues endlessly, until the program itself terminates or some other commands stop its execution. In the final example, the repeat until command is a hybrid command consisting of a repeat loop and an until conditional. The until conditional triggers a break out of the loop structure.

Find some favorite songs or dances that include repeated sections. Work with your kid to identify the repeated lyrics or steps, as well as how many times the repetition occurs. Can you say, “Heyyyyyy, Macarena!”

About This Article

This article is from the book:

About the book authors:

Camille McCue, PhD, is Director of Curriculum Innovations at the Adelson Educational Campus in Las Vegas where she leads the Startup Incubator, teaches STEM, and kickstarts K-12 learning initiatives. Sarah Guthals, PhD, co-founded an ed-tech company and now continues to build technology for kids to learn, create, and share safely online. She loves to teach teachers how to teach coding in the classroom.

This article can be found in the category: