Lab 1.1 The Nature of a Computer Program and Programming Languages

Team-Fly    

Oracle® PL/SQL® Interactive Workbook, Second Edition
By Benjamin Rosenzweig, Elena Silvestrova
Table of Contents
Chapter 1.  Programming Concepts


Lab Objectives

After this Lab, you will be able to:

  • Understand the Nature of Computer Programs and Programming Languages

  • Understand the Differences between Interpreted and Compiled Languages

A computer needs instructions to follow because it cannot think on its own. For instance, when playing a game of solitaire you must choose which card to move. Each time a card is moved, a set of instructions has been executed to carry out the move. These instructions compose only a small part of the solitaire program. This program comprises many more instructions that allow a user to perform actions, such as beginning or ending a game, selecting a card's color, and so forth. Therefore, a computer program comprises instructions that direct the actions of the computer. In essence, a program plays the role of guide for a computer. It tells the computer what steps in what order should be taken to complete a certain task successfully.

Computer programs are created with the help of programming languages. A programming language is a set of instructions consisting of rules, syntax, numerical and logical operators, and utility functions. Programmers can use programming languages to create a computer program. There are many different programming languages available today. However, all programming languages can be divided into three major groups: machine languages, assembly languages, and high-level languages.

graphics/intfig07.gif

Words such as statement or command are often used when talking about instructions issued by a program to a computer. These terms are interchangeable.


Machine Languages

Machine language is the native language of a particular computer because it is defined by the hardware of the computer. Each instruction or command is a collection of zeros and ones. As a result, machine language is the hardest language for a person to understand, but it is the only language understood by the computer. All other programming languages must be translated into machine language. Consider the following example of the commands issued in the machine language.

graphics/intfig03.gif FOR EXAMPLE

Consider the mathematical notation X = X + 1. In programming, this notation reads the value of the variable is incremented by one. In the following example, you are incrementing the value of the variable by 1 using machine language specific to an Intel processor.

 1010 0001 1110 0110 0000 0001  0000 0011 0000 0110 0000 0001 0000 0000  1010 0011 1110 0110 0000 0001 

Assembly Languages

Assembly language uses English-like abbreviations to represent operations performed on the data. A computer cannot understand assembly language directly. A program written in assembly language must be translated into machine language with the help of the special program called an assembler. Consider the following example of the commands issued in assembly language.

graphics/intfig03.gif FOR EXAMPLE

In this example, you are increasing the value of the variable by 1 as well. This example is also specific to an Intel processor.

 MOV AX, [01E6]  ADD AX, 0001  MOV [01E6], AX 

High-Level Languages

A high-level language uses English-like instructions and common mathematical notations. High-level languages allow programmers to perform complicated calculations with a single instruction. However, it is easier to read and understand than machine and assembly languages, and it is not as time-consuming to create a program in high-level language as it is in machine or assembly language.

graphics/intfig03.gif FOR EXAMPLE

 variable := variable + 1; 

This example shows the simple mathematical operation of addition. This instruction can be easily understood by anyone without programming experience and with basic mathematical knowledge.

Differences Between Interpreted and Compiled Languages

High-level languages can be divided into two groups: interpreted and compiled. Interpreted languages are translated into machine language with the help of another program called an interpreter. The interpreter translates each statement in the program into machine language and executes it immediately before the next statement is examined.

A compiled language is translated into machine language with the help of the program called a compiler. Compilers translate English-like statements into machine language. However, all of the statements must be translated before a program can be executed. The compiled version of the program is sometimes referred to as an executable.

An interpreted program must be translated into machine language every time it is run. A compiled program is translated into machine language only once when it is compiled. The compiled version of the program can then be executed as many times as needed.

Lab 1.1 Exercises

1.1.1 Understand the Nature of Computer Programs and Programming Languages

a)

What is a program?

For the next two questions, consider this scenario: You have been hired to work for the ABC Company. One of your responsibilities is to produce a daily report that contains complicated calculations.

b)

Without using a computer program to fulfill this responsibility, what potential problems do you foresee in generating this report every day?

c)

Based on your observations in question b, how do you think a computer program would make that task easier?

d)

What is a programming language?

For the next question, consider the following code:

 0010 0000 1110 0110 0000 0001  0000 0011 0000 0110 1000 0000  1010 0001 1111 0110 0000 0001

e)

What type of programming language is this code written in?

For the next question, consider the following code:

 MOV AX, [01E9]  ADD AX, 0010  MOV [01E6], AX 

f)

What type of programming language is this code written in?

For the next question, consider the following code:

 variable := 2 * variable - 10; 

g)

What type of programming language is this code written in?

1.1.2 Understand the Differences Between Interpreted and Compiled Languages

a)

What is an interpreted language?

b)

What is a compiled language?

c)

Which do you think will run quicker, an interpreted or a compiled program?

Lab 1.1 Exercise Answers

This section gives you some suggested answers to the questions in Lab 1.1, with discussion related to how those answers resulted. The most important thing to realize is whether your answer works. You should figure out the implications of the answers here and what the effects are from any different answers you may come up with.

1.1.1 Answers

a)

What is a program?

A1:

Answer: A computer program comprises instructions that direct the actions of the computer.

b)

Without using a computer program to fulfill this responsibility, what potential problems do you foresee in generating this report every day?

A1:

Answer: Programs help us with repetitive, time-consuming, and error-prone tasks. If you do not have a program that helps you create this report, it might take you a whole day to collect the needed information for the report and perform the needed calculations. As a result, you will not be able to concentrate on your other responsibilities. In addition, sooner or later you will probably make mistakes while creating the report.

c)

Based on your observations in question b, how do you think a computer program would make that task easier?

A2:

Answer: Using a program guarantees fast retrieval of needed information and accurate results, assuming that the program does not contain any errors. Furthermore, once a program is created, the same set of steps is repeated on a daily basis. Consequently, a well-written program is not susceptible to human frailties such as typographical errors or the accidental exclusion of a formula.

d)

What is a programming language?

A3:

Answer: A programming language is a set of instructions consisting of rules, syntax, numerical and logical operators, and utility functions.

e)

What type of programming language is this code written in?

A1:

Answer: This is an example of a machine language.

Machine language is understood directly by the computer. Each statement in machine language is represented by a string of zeros and ones.

This example illustrates the nonintuitive nature of machine language. However, a computer can read these instructions directly and execute them instantly. You can see that creating a program in a machine language can be a slow and tedious process. To facilitate program creation, programmers use higher-level languages that are closer to human language.

f)

What type of programming language is this code written in?

A1:

Answer: This is an example of an assembly language.

Assembly language uses mnemonic symbols to represent the binary code of machine language. Each assembly instruction is directly translated into a machine language instruction. You may notice that assembly language is slightly easier to understand than machine language.

g)

What type of programming language is this code written in?

A1:

Answer: This is an example of a high-level language.

Programs created in high-level languages are portable. They can be moved from one computer to another because a high-level programming language is not machine-specific. High-level languages must be translated into machine language with the help of an interpreter or a compiler.

1.1.2 Answers

a)

What is an interpreted language?

A1:

Answer: An interpreted language is translated into machine language with the help of another program called an interpreter. The interpreter translates statement in the program into machine language and executes it immediately before the next statement is examined.

b)

What is a compiled language?

A2:

Answer: A compiled language is translated into machine language with the help of the program called a compiler. Compilers translate English-like statements into machine language.

c)

Which do you think will run quicker, an interpreted or a compiled program?

A3:

Answer: Generally, interpreted programs run slower than compiled programs.

As you observed earlier, an interpreted program must be translated into machine language every time it is run. A compiled program is translated into machine language only once when it is compiled, and then it can be executed as many times as needed. As a result, an interpreted program runs slower than a compiled program.

Lab 1.1 Self-Review Questions

In order to test your progress, you should be able to answer the following questions.

Answers appear in Appendix A, Section 1.1.

1)

What group of programming languages is easiest for the computer to understand?

  1. _____ The machine languages

  2. _____ The high-level languages

  3. _____ The assembly languages

2)

Programs created in the machine languages are which of the following?

  1. _____ Portable

  2. _____ Machine-specific

3)

Which of the following is true of interpreted programs?

  1. _____ All statements are translated and only then executed.

  2. _____ Each statement is translated and executed before the next statement.

4)

Before a program written in a high-level language can be executed, which of the following must take place?

  1. _____ A program must be interpreted.

  2. _____ A program must be compiled.

  3. _____ A program can be executed immediately.

5)

Which of the following is true of the interpreter?

  1. _____ It translates instructions written in assembly language into machine language.

  2. _____ It translates machine language into a high-level language.

  3. _____ It translates a high-level language into machine language.


    Team-Fly    
    Top
     



    Oracle PL. SQL Interactive Workbook
    Oracle PL/SQL Interactive Workbook (2nd Edition)
    ISBN: 0130473200
    EAN: 2147483647
    Year: 2002
    Pages: 146

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net