(ToC) |
[...How about you write a (simple) program that uses input and output. Then turn the page.]
Here's one I like, I got the idea from Vivian, our assistant last summer:
the game rock-paper-scissors:
Repeat as many times as you want
Repeat 3 times
Pound fist on open hand to synchronize timing
Wait one second
Open fist into flat hand (paper), or two-finger "V" (scissors), or else leave it fist (rock) = output
Look at other player's hand = input
Decide who won (rock breaks scissors, scissors cuts paper, paper covers rock)
Count and show the score = output
Go back for another round
The distinctive thing about this program is that it has two iterations,
one inside the other. One of the iterations sort of depends on input (but
I didn't say so) to know when to stop, like when the other player gets
tired of playing (the computer never gets tired), the other one is exactly
three times, no more, no less.
There is only one more concept left to go and you will have everything you need. Actually, you already have it, it was in the very first programs we wrote, both PBJ and Breakfast.
People manipulate stuff -- peanut butter, jam, bread, cereal, plates, bowls, ideas, colors, numbers -- you name it, if you have a name for it, you can manipulate it, if not a physical object, then at least you can manipulate the name!
Computer programs manipulate information: numbers, names, groups of names and numbers representing physical objects or just ideas. In other words, stuff. In the traffic light example the information was what color the traffic light was and what you should do based on that. The information can be very simple, or very complex but it has to go somewhere, in your hand, in the fridge, on the counter, in the bowl, in your head, somewhere. The computer has millions of places to put numbers and names representing ideas and things and stuff. We call those places variables, because what you put there can vary from time to time, like the way the bowl started out with nothing in it, and then it had dry cereal, and then it also had milk, then after a while it had nothing again.
Think of a variable as a container, like a glass of milk or orange juice. When you pour OJ into the glass from the jug, that's like Input:
If you drink the OJ (or pour it down the sink) from the computer's perspective, that would be Output.Let's say we have three glasses, one is one quarter full of OJ (that would be 1), and another is two quarters (half) full of OJ (that would be 2). If you pour the first glass into the second (add it to the second), then it would be three quarters full (3). Computer variables are like that, when they have a quantity that can be added, you can add those numbers and get a sum (or subtract or multiply or divide), except pouring one variable into another doesn't empty the first, like this:
Also, computers don't do orange juice or milk, they do abstract numbers and text. Later you will learn about Objects, which are just collections of numbers and text, plus the programs to work on them.We have now covered everything you need to write any program you want. The ideas aren't new to you but what we've done is give each concept a name and a description of what it is and how it works. Using sequences, iteration, conditions, variables and input/output you can write complex and powerful programs that solve complex and difficult problems. In a moment you will begin writing your first program that a computer, rather than a person, will execute. It will be in a made up computer language that will make it easy to write your own programs and get good breaking down a problem into the 5 components.
There is however a problem. As programs get big, they get hard for humans to read. The computer doesn't have this problem. It just chugs along doing what you told it to do (even when what you told it to do isn't what you really meant, you just told it wrong). But when you tell it wrong, and you will, constantly, you are going to have to figure out what your mistake is. We call this debugging and usually debugging takes way longer than writing the program (especially if the program isn't well organized).
So in order to make is easier for humans to read their own programs programmers invented something where they bundle up a bunch of steps and variables for a task, say "make breakfast" or "make me a PBJ" that you can use. It's like asking somebody to do something for you. Originally in computers we called these subroutines. We use the routine, to describe something we do frequently. In gymnastics they use the same word for the steps that go into a performance. A subroutine is nothing but a whole lot of steps, including sequences, conditionals, variables, I/O that you can use.
So if I was writing a program called Morning it might look like this.
"Morning":
Get up
Breakfast
Go to School
(done)
Each of those would be a subroutine containing all the steps (and
information) necessary to do that routine.
Write program for each one. Include stoplight, watching for cars, what
if it is the weekend. Then turn the page.
<<Previous | ToC | Next >>
[2021 July 1]