CPS100 • Introduction to Computers


Lakeland College • Japan Campus

Programs

"Programs," also called "applications" or "apps," are a category of software which are opened directly by users and act within the operating system software.

Simply put, programs, like all software, are a list of instructions for the computer to follow, similar in nature to a cooking recipe.


Dumb Computer

It has been famously said that computers do what you tell them to do, not what you want them to do. This means that computers are not like people—they do not understand what your intentions are, and they will not correct your mistakes. Computers are fast, but they are stupid; they only follow instructions, and no more. They are no "smarter" than the person who programmed them.

For example, if you tell your friend to ask someone a question where the only possible answers are "yes" or "no," you do not spend time explaining to your friend what to do if the person answers "bathtub." You know that the friend will be smart enough to say, "bathtub is not a possible answer, please try again."

If you tell a computer the same thing, however, the computer will not be smart enough to know what to do if the answer is not a "yes" or a "no." Therefore, when you program a computer, you have to foresee all possible conditions, and write instructions on what to do in every possible case.

Because of this, programs must be designed very carefully; the programmer must think of all possibilities, and account for them.

Programming Languages

As noted in an earlier unit, computers operate using binary code. Human beings, however, do not; it would be extremely difficult for us to work entirely with 0s and 1s.

As a result, there must be two different languages: one for humans to use, and one for machines to use. A language which is easy for humans to use is called a high-level programming language. A language which is easier for computers to use is called a low-level language.

A high-level language must be close enough to human language so we can use it easily, but it must be a specially-designed language. If a normal human language, like English, were used, it would never work. Human languages are not exact enough.

Therefore, we need to create languages that can be translated precisely to low-level machine languages. These are called programming languages. There are and have been many: Cobol, Fortran, Pascal, Java, Perl, C, C++, C#, etc.

A popular programming language from a few decades ago is BASIC.

Whenever a student learns a new programming language, the first program they make is always the same: the "Hello World" program. That program is very simple: print out the words "Hello World" and then end. In BASIC:

10 PRINT "Hello World!"
20 END

As you can see, it is indeed very plain—but it is a program which the computer will execute. In this case, it simply does a pre-assigned task and then ends.

Some commonly-used commands from BASIC:

PRINT Will cause text to be printed on the screen.
INPUT Will print text on the screen, then wait for the user to type something. Once the "Enter" key is pressed, whatever the user typed will be saved as the variable.
GOTO Causes the flow of the program to jump to a specific numbered line.
IF ... THEN ... Conditional; allows the program to make a choice depending on a value
END Ends the program

Input is an important command. This collects data from the user. It makes the program interactive, which is a very important quality in most programs. If the program is not interactive, then the user never does anything. Software would be very boring if you just sat and watched it.

Input requires the use of variables. You know variables: for example, in the statement x = 3, "x" is the variable. A variable is a placeholder, a container for a value. "x" could equal "57," or it could equal "Thursday." Variables can and do change as a program goes on.

When a program asks the user for input, it needs to save the input somewhere. It puts it into a variable. For example, if a computer asks a user for their name, the user will type their name and hit "Enter." What they typed before hitting "Enter" is saved as a variable.

Something similar to a variable is a constant. A constant is a set value, one which does not change. For example, pi (π) is a constant. Constants are set by the programmer, and cannot be changed by the user.

Here is another, slightly more complex BASIC program:

10 INPUT "What is your name?", N$
20 PRINT "Hello "; N$
30 INPUT "Would you like me to ask you a question? (Y/N)", A$
40 IF A$ = "N" OR A$ = "n" THEN GOTO 80
50 IF A$ = "Y" OR A$ = "y" THEN GOTO 10
60 PRINT "Sorry, I did not understand your answer. Please type Y or N."
70 GOTO 30
80 PRINT "Okay, Bye!"
90 END

In the above program, the following steps are taken:

  1. The computer asks the user for a name, and waits for input;
  2. When the user types something and hits "Enter," the computer assigns the input to the variable N$;
  3. The computer then greets the user with "Hello" and the user's name;
  4. The computer asks if it should ask a question, and waits for input;
  5. The computer then checks the answer:
  6. If the answer is "N" or "n" (for "No"), then it jumps to the end; otherwise it continues;
  7. If the answer is "Y" or "y" (for "Yes"), then it jumps back to the beginning question; otherwise it continues;
  8. If the answer is neither "Y" nor "N", then line 50 will catch the error, tell the user it does not understand the answer, and sends the user back to line 30 to ask the question again.

Conditionals and Loops

Conditionals and loops are extremely important, basic rules of programming.




You have seen a conditional above: the IF statement. A conditional tests some kind of data, usually the value of a variable. If the data passes the test, one set of actions happens. If the data fails the test, those actions are not taken, and the program continues to the next step.

In the program above, the conditionals test the answer given by the user to the Input question, "Would you like me to ask you a question?" IF the answer is "yes," then the program asks a question. IF the answer is "no," the program ends. Notice that after the two "IF" statements, there is a safety net: if both tests fail, the program "knows" that the answer given was not a correct one, and sends the user back to the question.

Programmer's Joke

A computer programmer's wife told the programmer to go to the store. "Get a loaf of bread," she told him. "If they have eggs, get a dozen."

The programmer goes to the store, and returns with several bags of groceries. "They had eggs," the programmer said, "so I got a dozen loaves of bread."

A more complex version of the conditional is the "if ... else if ... else" command set. Let's say you want to test a number, with various outcomes. For example, if a person is (roughly) between 6 and 11, they will be in elementary school; between 12 and 14, junior high school; between 15 and 17, senior high school; and 18 to 22, college.

Here is that test, using the programming language PHP (used to make web page apps):

if ($age < 6) {
print "You are too young for school.";
}

else if ($age < 12) {
print "You are in elementary school.";
}

else if ($age < 15) {
print "You are in junior high school.";
}

else if ($age < 18) {
print "You are in senior high school.";
}

else if ($age < 23) {
print "You are probably in college.";
}

else {
print "You are older than normal school age.";
}

This set of commands tests one value several times. If it fails one test, it goes to the next. At the end, the "else" statement catches any value that failed all the tests.

To see how the above conditional works, I put that into this mini-web app; type an age into the box, and then hit "Enter":




A loop is when an action is repeated many times until a desired condition is met. For example, you might want a set of commands to repeat exactly 17 times. Or you might want the commands to repeat until a variable is exactly right.

For example, I made a Bingo game for the school using PHP. The game creates a random number between 1 and 75 each time you press a button. The program saves each number in a list. However, no number should ever be repeated. Therefore, I made a loop: I told the program to come up with a random number until a new number (not in the list) is found.

Here is an example of a simple loop, which counts from 1 to 10:

$count = 1;
while ($count <= 10)
{
print "The number is: " . $count;
$count++;
}

In the above code:

I first set the variable "$count" to equal 1;
Then I create a "while" loop, which tests $count until it reaches 10;
Then I print the value in $count;
Then I add one to count (the "++" means "add 1").

The commands inside the "while" loop repeat, again and again, until the "$count" variable gets to the right value, then it stops. That is a loop: something that repeats until a set condition is reached.

Notice that I use symbols such as =, >=, and ++; these are called operators. These include plus, minus, multiply, divide, greater than, lesser than, and equal to.

Here is another mini-web app. It will count to 10. However, you can decide what number it begins with!

Translating

As stated above, these languages I have been explaining (BASIC, PHP, etc.) are "high-level" languages. However, the computer needs to translate this into a low-level language.

Most programming languages are translated fully into low-level languages; if you do this, we say that you compile the program.

However, it is also possible to have the computer run the high-level language, as it does with PHP; in this case, the high-level language is "interpreted." Interpreted programs often run more slowly.


Terms to Know

programming languagea language used to create programs.
high-level languagea programming language more similar to human language.
low-level languagea programming language more similar to machine language.
BASICa high-level programming language.
PHPa high-level programming language used in web pages, which is interpreted rather than compiled.
printa command telling the computer to print something on the screen.
inputa command which asks the user to input some data, which is then saved as a variable.
variablea text string (for example, x or $x) which holds a value (number or text) which can change while using a program.
constanta text string which always holds the same value within one program.
conditionalwhen a program tests if something is true or not, in order to decide which command(s) will be followed.
loopa set of actions which is repeated until a certain condition is reached.
compileto translate a high-level language into a low-level language.
interpretwhen a computer executes a program directly from a high-level language, without first compiling it.
Previous Chapter Chapter Quiz Next Chapter