NAF Principles of Information Technology

Lesson 13 Introduction to Programming

NAF Principles of Information Technology

Lesson 13

Introduction to Programming

Student Resources

Resource / Description
Student Resource 13.1 / Comparison Matrix: Introduction to Computer Code
Student Resource 13.2 / Reading: Introduction to Computer Code
Student Resource 13.3 / Keyword Notes: How Programming Languages Work
Student Resource 13.4 / Reading: How Programming Languages Work
Student Resource 13.5 / Introduction to Programming: Using Python
Student Resource 13.6 / Reading: The Software Development Process

Student Resource 13.1

Comparison Matrix: Introduction to Computer Code

Student Name:______Date:______

Directions: Using information in Student Resource 13.2, Reading: Introduction to Computer Code, fill out the matrix below by placing an “x” wherever it is applicable to the items in the left-hand column.

/ Machine Language / Source Code / Assembly Code / Compiled Code /
Computers can read it
Programmers can read it
High-level program
Low-level program

Student Resource 13.2

Reading: Introduction to Computer Code

As you know, the number of tasks computers can perform often seems limitless. But computers aren’t built knowing how to perform these tasks. In fact, computer hardware without software telling it how to function isn’t of any use at all (unless what you need is a doorstop). Computers are stupid—they can do only what we tell them to do. To get the computer to do something, you must tell it in a very precise way—with no errors—using a language the computer understands. You can see this firsthand any time you mistype an email address or URL. One typo will keep your mail from being delivered or keep you from finding your favorite website.

The problem, then, is that people need to be able to give computers very detailed sets of instructions, but the languages that people speak (for example, English or Spanish) are very different from the languages that computers understand. A computer’s microprocessor can only understand one language: the computer’s machine language. This language consists of very detailed and simplistic instructions made up of 0s and 1s. Unfortunately for human programmers, writing a program in machine language is very challenging. It’s like having to build a car out of nuts and bolts rather than car parts. Fortunately, programmers have developed programming languages that are easier to use. Programming languages are classified as “high level” or “low level” depending on the amount of abstraction provided to the programmer (i.e., how easy they are for programmers to use). Machine language is the hardest and “lowest-level” language.

Today, programmers use very high-level languages instead. But if a computer can understand only machine language, what is the value of a different language? The advantage of a higher-level language is that it lets the programmer specify code using “tools” like the preassembled components that manufacturers use to build cars. In order for the computer to understand and run a program written in a high-level language, the program must first be converted into the machine language of the computer. This is done by one of three types of programs: assemblers, compilers, or interpreters.

What Is Computer Code?

Computer code is a set of detailed instructions telling a computer’s CPU what to do. This code must be written in the computer’s machine language and free of errors to run correctly (or at all).

When you purchase software programs, they are usually already translated into the computer’s machine language. This means that your computer can execute (or run) these programs, but humans can’t read machine language, so we would not understand the program code.

A programmer writes code using a particular programming language. By “language” we don’t mean a human spoken language such as English. Instead, the term refers to any language that can be used to define a sequence of instructions, or commands, that can be translated for a computer to understand. We call the day-to-day human languages natural languages, while programming languages are artificial languages (these terms are a little misleading in that both types of languages are invented by humans, but programming languages are invented specifically to write computer code). Most programming languages used today look something like English and mathematical notation combined.

The high-level language program written by the programmer is called source code. In other words, source code consists of program instructions in a form that can be read by humans. But computers cannot directly execute source code, so the code must be translated into machine language code, or executable code. As noted above, when you buy computer software, you almost always receive just the machine or executable code. Programs written for an open source platform such as Linux usually provide both the source code and the machine code.

The translation process turns source code into machine code, which consists of a series of 0s and 1s (also called binary code). Programmers use utility programs called assemblers, interpreters, and compilers to translate source code into machine code. The difference between these three types of programs depends on what is being translated.

An assembler converts a program written in assembly code into machine language. Assembly code, like machine language code, is very low-level. In fact, one instruction written in assembly code is converted into a single machine language instruction. The difference between the two languages is that while machine language consists of 0s and 1s, assembly language uses terms that can be read. Mostly, these terms are abbreviated in what is known as mnemonics.

Here’s an example of how assembly language works: An instruction that tells the CPU to branch from one location to another in the program might be written as jge next. (The code jge stands for “jump if greater than or equal to.”) This means that if the most recent comparison found that the first value was greater than or equal to (ge) the second value, then the CPU should “jump,” or execute the instruction at location next. The word next is called a label.

In a machine language program, the jge and the label next would be recorded entirely using 0s and 1s. So even though assembly language might be easier to write than machine language, the instructions are still low level, and it makes programming challenging.

Starting in the late 1950s, high-level languages were being pioneered. Today, nearly everyone who writes code uses a high-level language. There are two general schools of thought on converting high-level language programs into machine language. First, and more common, are those languages that use a compiler. The compiler is a program that converts an entire program into machine language at one time. Once the program has been successfully compiled, the executable version of the program can be sold. A positive aspect of compilation is that it only has to take place once. Once compiled, the executable program runs quickly. Also, compilers can locate a type of error in code known as syntax errors.

The other school of thought for converting high-level code: use an interpreter. The idea behind an interpreter is that you can enter one instruction and have it translated and executed. In this way, the programmer can explore what an instruction does, work through ideas, and test each one out. While this helps a programmer develop the code needed to solve the given problem, it has a drawback. Since each instruction is translated before execution, the program must be translated, one instruction at a time, each time the program runs. This slows down execution. The good news about using an interpreter is that most high-level languages that are interpreted also have a compiler. So you can experiment when writing your code, and then you can compile the code when you’re ready.

Student Resource 13.3

Keyword Notes: How Programming Languages Work

Student Name:______Date:______

Directions: Use the following chart to take notes on Student Resource 13.4, Reading: How Programming Languages Work. There are five sections to the reading. After each section, write down the three words that you think will help you remember the most important information in that section. Then move on to the next section and write three more words about that section. Continue this pattern with each of the remaining sections. Based on your keywords, write a summary in the bottom box that includes the important points you observed for each of the sections. Remember that a summary is not a rewrite of the article. Focus only on the key points you need to remember.

Introduction / Different Programming Languages
Programming Methods / Choosing a Language
The Flow of a Program
Summary

Student Resource 13.4

Reading: How Programming Languages Work

Introduction

High-level programming languages enable people to use familiar words and symbols to tell computers what to do in a way that the computers can understand. Just as human spoken languages have their own grammar and vocabularies, so each programming language has rules for writing commands, and special words and symbols that do different things. If you want to write a program, you choose a programming language (for example, Python or Java) and use its rules and grammar to write instructions, called source code, for the computer. When you are finished, you use another piece of software to turn what you’ve written into something the computer can understand. That software can be either an interpreter or a compiler.

Programming, the process of creating computer code, can be compared to writing a cooking recipe. Programs do for computers what recipes do for cooks: they organize data and give instructions for carrying out tasks. Sometimes, these instructions are simply a sequence of unchanging steps. At other times, more complex methods are needed for doing analysis and arriving at decisions.

A typical program consists of a main module and many submodules, sometimes called functions, methods, procedures, or subroutines. These modules are stored as a collection of files. Large programs can contain thousands of individual files, each with a specific purpose. These programs divide big problems into smaller problems. They operate quickly to solve all the smaller problems until the work adds up to one complete solution.

Different Programming Languages

There are many different kinds of programming languages. The higher-level a language is, the more it resembles a natural language like English, and the less programmers need to directly concern themselves with the internal workings of the computer hardware. By way of comparison, consider a person who wants to construct a computer. Low-level construction would require that the person use individual circuits to build the computer. With high-level construction, the person would use off-the-shelf components like a specific type of processor and memory circuit boards. At the highest level, the person might order a preassembled computer. Similarly, in programming, we see languages that range from low to high level.

Whether high level or low level, each language has its own set of detailed grammar, spacing, and punctuation rules, just like the rules in languages that people use to communicate with each other. These rules are called syntax. If one of these rules is broken in the program code, the result is a syntax error, because the computer cannot understand the instructions that were given.

Here are some different language types explained:

·  The original programming language, machine language, is the language that a computer’s processor understands. Machine language consists of large sets of 0s and 1s. The language is not only challenging because it is difficult to read, but also because the instructions are very low-level. Programming in machine code is not common today because of the use of high-level languages and translation programs like compilers and interpreters. However, the original computer programmers had no choice but to use machine language.

·  Assembly is another low-level language. It uses letters and words instead of 0s and 1s in its code, but an assembly programmer still has to know a lot about how the computer’s hardware works. Different versions of assembly language are specific to different types of computers. Once an assembly program has been written, the computer cannot yet execute it. Instead, the program must be translated into machine language using a program called an assembler.

·  Starting in 1958, high-level languages were being produced. The earliest was called Fortran, a language that made mathematical programming easier than it was with machine or assembly language. Fortran was quickly followed by COBOL, a business processing language. Over the years, high-level languages were improved. A significant language called C was developed in the late 1960s. C offered portability; that is, a program written in C could be compiled not just for the computer it was written for but potentially for any computer. C also introduced more sophisticated programming tools in the form of control statements. Today, we might refer to a language like Fortran, COBOL, or C as a mid-level language. These are high-level languages in that they are far easier to understand than machine or assembly language, but they do not offer as many of the abstraction tools as newer languages do.

·  The newer languages have more easily understood terms that make the programmer’s job easier. In 1988, C was expanded to include a new concept known as objects. This ushered in a shift in programming from procedural programming to object-oriented programming. So, C++ lets programmers do things that are difficult when using an assembly language or C. A C or C++ programmer still needs to be aware of things like how the computer’s memory stores numbers and data. These languages are popular in part because they use a computer’s resources efficiently. They also have the advantage of being able to run on many different kinds of processors.

·  Although we would classify C, C++, Fortran, and COBOL as high-level programming languages, many improved languages have been released in recent years, including Java, C#, and Visual Basic .NET. Added to these are interpreted languages like Perl, Ruby, and Python. These high-level languages are sometimes thought of as higher level than C and C++ because of added features.

Mid- and high-level languages use words and symbols to represent complex concepts simply. This allows a programmer to do more with less effort. Mid- and high-level languages also use familiar math symbols, like the plus and minus signs, to show computer operations. Programs created with high-level languages can direct much more complex tasks than those created with low-level languages. Some high-level languages serve a specific purpose, like creating graphics, while others are very flexible and are considered general purpose. A programmer who needs to write code that will work well with several different operating systems may choose Python or Java. Someone who needs a language that is good at dealing with text may use Perl.