By the end of this reading you should be able to answer the following questions:
In the most basic sense, programming means creating a set of instructions for completing some specific task. In this sense, many of our daily activities can be described as programmatic—they involve specific steps that often follow a set order. For instance, if you get home from school and want to make yourself a peanut butter and jelly sandwich, you know that you will have to get two slices of bread, butter each piece, spread peanut butter on one slice and jelly on the other, and finally put the two together. If you leave out a step you might end up with something other than a PB&J sandwich. If you do things out of order—say, you put the slices of bread together before you spread the peanut butter—you'll end up with a mess.
In this general sense, our lives are filled with programs and programming. When you make your bed you follow certain steps in a programmatic fashion. The steps must be the correct ones and they must be in the correct order. If you want to make your grandmother's favorite apple pie, you would ask her how to do it, and she most likely would send you a program—a recipe. A program is therefore also useful for replicating a product even if you are far removed from the original creator of the product.
But programs are not only useful for reproducing products. Humans—even young children—have a remarkable capacity for mastering programmatic processes and behaviors. Take the familiar children's game, One Potato, Two Potato. This chant is a common way for children to choose who is going to be "it" in a game. The children will all stand in a circle around the counter and hold their fists out in front of them. The counter recites the rhyme, striking each fist in the circle in turn for each word of the rhyme. When the counter strikes a fist on the last word of the rhyme—"...seven potato, more."—that fist is knocked out of the game. The counter then repeats the rhyme, each time knocking out another fist. The last person with a fist remaining raised is "it". This simple game has all of the elements of a program—a set sequence of events, actions performed based on the outcome of a process, and repetition.
In the context of computing, programming means creating a set of instructions not for a person but for a computer, in order to accomplish a specific task. To do so you use a set of directives—a programming language—known to both the programmer and the computer operating system. The kind of things we program computers to do is different from what we “program” ourselves to do. Usually a set of instructions, or program, for a computer is intended to complete a task that:
Even though each programming language you use is unique, there are certain concepts common to all languages, including LiveCode's scripting language. Let's look at three of the most common concepts and structures used in programming.
Sequence of commands (The right commands in the right order.)
It is important not only to give the right commands or steps—they must also be given in the correct sequence. We can easily see in some of our mundane examples—making a sandwich, tying one's shoes, following a recipe—that proper order is essential to our success. We might call such obvious sequences task order, because the proper sequence is dictated by the nature of the task.
But there are also procedures in which the order of steps is unimportant. Often in such procedures, a conventional order emerges to avoid confusion. An excellent example in the U.S. context is addressing letters for mailing. Every school child knows that you do it in this order:
First name Last name
House number Street name
City, State Zip code
Oddly enough, this conventional order is exactly the reverse of how the address is examined at the various postal distribution points in the mail system. The post office at the point of origin would look at the country and put the letter in a pile for international mail. The distribution center in the destination country would look at the zip code and perhaps state. The local post office would examine the street address and place it in the proper mail carrier's route, and the mail carrier, upon arriving on that street, would place it into the mailbox bearing that address. Finally, whoever checked the mail at the house would look at the name and give the letter to the person it was addressed to.
In some countries the conventional order follows the logical task order in addressing envelopes. In Russia, for example, letters are addressed in exactly the opposite order to the U.S. convention.
Example: You want to clear your screen of all buttons and fields, show
a field with text, wait for the user to click, then hide the field and show
the former ones.
To work correctly, not only do all the commands have to be there, they have to be in the right order.
Conditional structures (Do certain things based on a true or false,
yes or no decision.)
These provide for one outcome or sequence of events to be executed if a statement is true, and another outcome or sequence of events to be triggered if the statement is false.
In most programming languages these structures take the form if . . . then . . . else.
The One Potato, Two Potato game uses lots of conditional decisions.
If the counter lands on your fist on the word "more" then you must remove your fist from the circle.
If both of your fists are knocked out of the circle then you are out of the game.
If a word exists in a list, then print it out,
Else tell the user that the word does not exist.
If a sentence contains the word "silly" then put that sentence into the silly list.
Else if it doesn't contain the word "silly" then put it into the serious list.
Looping structures (A list of instructions to do more than once.)
Used to make the computer repeat a certain command or sequence of commands. The loop may run for a predetermined number of times, until a certain condition becomes true, or as long as a certain condition remains true.
Here are some ways that looping might be done:
Do the following 20 times.
Do the following once for each word in the list
Repeat the following until the user presses the option key
Repeat the following as long as the option key is depressed.
Again, the One Potato game provides an obvious example of a looping structure. The rhyme is repeated and fists counted for as many times as needed until just one person is left.
Given a list of party guests, assign everyone to one of three groups for "ice-breaker" games.
Programming can range in complexity from solving small problems—like setting an alarm time on your watch or cell phone—to very sophisticated instructional or business applications. For more complex tasks, you can use these strategies to help you think through the logic of your program before starting to write code.
Top-down design is a way of approaching a complex programming task by first mapping out the entire program and identifying the major components that it will require. Then the programmer would use flowcharts and general statements to represent the logical flow of your program. Once the major components are identified, the programmer then focuses on each component in greater detail, finally culminating in writing the actual program code for creating each component.
In the example at right, we have represented the “program” for playing One Potato, Two Potato using a top-down approach. Each shape in the flowchart represents a major step in the game. Combinations of shapes and arrows show conditional “if-then” decision points, as well as looping structures in which segments of the program are repeated, perhaps with slight variations in each iteration.
This term, from the prefix pseudo-, 'false' and the root word code, 'programming instructions', describes a way of representing the detailed steps your program must perform without having to worry about the specific vocabulary or syntax of a specific programming language. You use your knowledge of the basic control structures, common sense and logic to write plain-English statements to explain in detail how you will accomplish each main step. All of the examples shown here and used in class could be considered forms of pseudocode.
Here is an example of pseudocode that describes our One Potato, Two Potato “program”. Notice how it attempts to detail all of the steps, conditional statements and looping segments using simple statements, indents and minimal punctuation brackets.