CPS100 • INTRODUCTION TO COMPUTERS


Spring 2024 Semester • LAKELAND UNIVERSITY

Programs & Programming

Understanding how programs work can give you much better insight into many processes, issues, and problems you may encounter when using a computer.

It might help to think of computer programs as being like a recipe. In both, a list of ingredients is laid out so that the correct parts can be prepared, and then a list of instructions is laid out clearly and specifically, so they may be followed to produce the desired outcome.

The simplest way to define a program is that it is essentially a list of instructions for the computer to follow.

Programming Languages


Programming languages can be separated into low-level and high-level languages.

Low-level languages, usually referred to as machine code, are the language which the computer directly uses when processing. They consist of numbers, expressed in binary (ones and zeroes only). While it is possible for humans to learn to read and write machine code, it is extremely difficult. Another low-level language is assembly language. Assembly language is good for optimizing software processes for a specific type of computer processor in a way that higher-level languages might not allow. After writing code in assembly language, the code must be run through an assembler which translates it into machine code.

Higher-level languages are designed for people to more easily use. While these languages will appear confusing and machine-like to someone who does not understand them, they are laid out in a form which is much easier for people to read, understand, and write than is possible in lower-level languages.

Examples of higher-level programming languages include earlier languages such as Autodesk, FORTRAN, and COBOL; in the 1970's, we saw Pascal, C, and BASIC; in the 1980's, C++, Objective-C, and Perl; in the 1990's, Visual Basic, Python, Java, JavaScript, and PHP; more recently, we have gotten C# (C-Sharp), Visual Basic .NET, and Swift. There are many, many more languages.

Before you can use a program, the higher-level programming language usually must be converted into machine code. This is called compiling, and uses a compiler to do this.

Some higher-level programming languages are interpreted, meaning that they are not compiled, but run as a high-level language. The computer still translates it into machine code, but it does so as the program is run, which takes longer while the application is running. An example of this is PHP, used to create web apps.

Originally, many higher-level languages were procedural—that is, they were mostly step-by-step instructions for the computer to follow. Early languages like FORTRAN, C, COBOL, and BASIC were procedural.

More recently, Object-Oriented Programming (OOP) has been the standard. This is a style of programming which divides the program into objects, which belong to certain categories, or classes, capable of interacting with other objects in the program. Objects have properties, which describe them, and methods, which describe what they can do.

Program Elements


Mathematical

Some elements of programs may already be familiar to you if you have studied algebra. Programs carry out mathematical calculations. They can use number values directly, or the numbers can be represented by variables and constants.

A variable is a name which contains a value which can change. For example, in the expression x = 5, "x" is the variable. A variable can be a number or it can be a text string.

A constant is like a variable, but it does not change. Pi (3.14159) is an example of a constant.

Number values can be manipulated with an operator, such as  + ,   - ,   * , or  / .

Relational operators are used as well—for example,  >  and  <  (greater than and less than), as well as  >=  and  <=  (greater than or equal to, and less than or equal to.

Some operators are dangerously similar, such as  =  (which forces a variable to become something new) and  ==  (which just checks if two things are equal).

Logical operators define relationships between values, such as  &&  (and),  !  (not), as well as  ||  (or).

The Increment operator,  ++ , adds one to a value (for example,  x++  means the value of x plus 1); there is also a decrement operator,  -- .


Input and Output

These are ways to get data into the program, and to send it back to the user.

Input would be where a program asks you to type something, for example. You type it in, and then the program accepts the data and uses it. Output is when the program sends data to you, such as with text printed on the screen.

In C++, the object cin is used to get input, and cout is used to show output:

char name[40];
cout << "What is your name? ";
cin >> name;
cout << "Hello, " << name;

In the above example, the first line sets a variable which can hold characters, called "name," and sets the length of the variable to 40 characters.

The second line creates output, in the form of text of the screen, printing the words, "What is your name?"

The third line tells the computer to wait for input (typing followed by hitting the Enter key), and whatever is input, put that value into the "name" variable.

The fourth line outputs the word "Hello," along with whatever text the user input.


Conditionals

A conditional is when the program tests whether something is true or not; if the result is true, then one action is taken; if the result is false, then a different action is taken.

This is usually expressed by the statements if, else if, and else. Here is an example of a conditional in PHP:

if ($amount < 10) {
print "It is not enough.";
}

This will test if the variable "amount" has a value which is less than 10; if that test is true, then it will print the words "It is not enough"; if the result is false, nothing will happen.

If there is only one alternative result, then you can use else:

if ($amount < 10) {
print "It is not enough.";
}
else {
print "It is enough.";
}

In the above case, any value of 10 or higher will cause the program to announce that "It is enough."

You can add further steps in between with else if statements:

if ($amount < 10) {
print "It is not enough.";
}
else if ($amount < 20) {
print "It is close to being enough.";
}
else {
print "It is enough.";
}

In this case, less than 10 is not enough, less than 20 is close to being enough, and any value 20 or over will be judged as "enough."

Let's look at an example you can test. 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":


Loops

Loops are another essential element of programs. A loop allows you to perform the same set of instructions many times until a certain condition is met.

Let's say that you want to choose a random number between 1 and 10. You have done so six times before already, and the results were 1, 2, 3, 5, 7, and 8. Your program must choose another number, but it must be one of the numbers you did not get before.

So the computer must be set in a loop: choose a number, then compare it to the others; if the number is old, then start again; if the number is new, then finish. So the computer begins:

It chooses 3, checks against the old numbers, finds a match, loops back;
It chooses 8, checks against the old numbers, finds a match, loops back;
It chooses 1, checks against the old numbers, finds a match, loops back;
It chooses 3, checks against the old numbers, finds a match, loops back;
It chooses 9, checks against the old numbers, finds no match, and finishes.

There are different types of loops. Two of the most common are "for" and "while" loops.

A for loop repeats a set of instructions a set number of times:

for( $mynumber = 0; $mynumber < 10; $mynumber++ ) {
instruction #1;
instruction #2;
instruction #3;
....
}

In the above example, in the first line, you can see the "for" loop begun, with three "arguments" set. They are:

  1. $mynumber = 0; sets the variable $mynumber at zero
  2. $mynumber < 10; sets the condition under which the loop is repeated—as long as the variable is less than 10, the loop repeats
  3. $mynumber++ adds 1 to the variable each time the loop is performed

So, with the for loop, you know exactly how many times it will run.

A while loop, in contrast, runs until a certain condition is met; the number of times is not set in the loop, but depends on conditions in the repeated code:

$mynumber = 0;
while( $mynumber < 10 ) {
instruction #1;
instruction #2;
instruction #3;
....
}

In this case, the loop will repeat while the variable is under 10; if the variable gets to 10 or higher, the loop stops. Note that the instructions in the loop must allow for the $number variable to change in a way that the value can be 10 or greater.


Functions

A function is a set of instructions which produce a desired result. Functions often require arguments to work; the arguments, if any, are included in parentheses after the name of the function.

For example, the function to create a random number is:

rand(1,100);

The function as typed above would return a number between 1 and 100. It requires two arguments (also called "parameters"), which are the lowest possible number and the highest possible number.

Some functions do not require arguments, or the arguments are optional and may be left blank, in which case the parentheses () are still used, but nothing is inside. For example, rand() will return a random number between values you did not set.

You can also create your own functions. Just type the word "function" followed by the function name and parentheses, then define the function actions, and that's it. For example:

function printmyname() {
print "My name is Luis.";
}

After defining that function, if you type a line of code using printmyname();, then it will print "My name is Luis." Of course, most functions are of course much more complicated.

Functions, by the way, are commonly used in many programs—Microsoft Excel, for example, uses them.

Dumb Computer


One last note: computers are stupid, at least in the human sense. They seem to be "smart" because they work very fast and use the programmer's intelligence, but in fact, they are simply just complex calculators. They don't actually "think" or "reason"; they just do anything they are told to do. If you tell them to say they are stupid, they will say exactly that.

Because computers are exactly literal, you have to be very careful when you program them. Take the joke below, for example:

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."

Programmers will instantly understand this joke. What happened was that the programmer's wife was not specific enough. She instructed, "Get a loaf of bread; If they have eggs, get a dozen." She did not say to "get a dozen eggs."

A human being would of course understand the statement. After all, it is nonsense to think that you would get 12 loaves of bread, even more strange if it is on the condition that the store also contains eggs. Why would eggs being available cause you to get 12 times the loaves of bread?

However, to a computer, the programmer's actions make sense. After all, it was established at "loaf of bread" was the object of "get"; therefore, the statement "If they have eggs, get a dozen" would mean that if it is true that the store has eggs, then you should get a dozen loaves of bread.

Like the programmer in the joke, computers don't reason or understand anything you do not tell them. There is a famous saying: Computers don't do what you want them to do, they do what you tell them to do.

Thus, when you are programming, you have to think for the computer; you must foresee and prepare for all possible outcomes, and you must make sure that your instructions are ones that make sense to the computer.

Terms to Know

programa list of instructions for a computer to follow.
high-level languagea programming language more similar to human language.
low-level languagea programming language more similar to machine language.
programming languagea language used to create programs.
assembly languagea low-level language more easily used by humans.
machine codeinstructions in the form of numbers which the machine best understands.
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.
procedural programminga type of programming in which instructions are given step by step.
object-oriented programming(OOP); a type of programming in which objects, what they can do, and how they interact are defined and used.
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.
operatora symbol which represents a mathematical function.
inputa command which asks the user to input some data, which is then usually saved as a variable.
outputdata revealed to the user.
printa command telling the computer to print something on the screen; a form of output.
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.
functiona pre-defined series of instructions which are carried out whenever the function is used.

Previous Chapter Chapter Quiz Next Chapter