7.7 Computer Programming

Computer programs solve problems, and therefore need a clear idea of what the problem is. There are two steps:

1. Defining the problem.
2. Writing step-by-step instructions to solve the problem.

From Machine Code to High Level Languages

Machine Code

At its most basic level, in machine code language, the central processing unit simply consists of millions of tiny switches turned on (1) or off (0). This sequence of ones and zeros forms a binary arithmetic, and a lot of ones and zeros are needed to make what humans recognize as a normal number. The binary number 1011 0000 01 10 0001 represents the hexadecimal number B061, for example. Programming at this level, the native language of the CPU, means simply changing the pattern of binary numbers, turning some 0s on and turning some 1s off. It's a perfectly feasible task, but rarely done because so slow and error-prone.

Assembly Languages

Assembly languages are, basically, a shortcut, allowing one command to stand for a dozen commands in machine code. It's the CPU (central processing unit) that does the work, usually retrieving data from the computer's memory (RAM or hard disk) and temporarily storing it in a special storage area called the registry. The CPU then manipulates or edits the data in some way, and sends it back for storage in the computer's RAM or hard disk.

A typical assembly language command might be:

mov a1, 061h

which tells the CPU to move the hexadecimal number 061h into the specific registry named a1.

Again, it's tedious to work at this level, and the registry architecture differs with each CPU, moreover.

Higher Level Languages

Higher-level languages like Basic, Cobol and Fortran aggregate assembly language commands in more intuitive commands (Total = A + B, for example) and are much quicker write and correct (debug). Unfortunately, they run more slowly on the computer. Indeed, the higher the level, the easier the programs are to write and debug, but the more slowly they run on the computer.

One language that enjoys reasonable writing and running speeds is C (in its several versions). It's reasonably intuitive, and runs at acceptable speeds. The code below:

#include int <studio>
main(void)
{
printf'' Hello World! \n'' ) ;
exit( 0) ;
}

prints 'Hello world!'

Because CPUs do not understand high-level languages, however, programs written in these languages have to be 'compiled': i.e. they are run through a software program called a compiler that translates the high-level language into equivalent commands in machine language. Compilers are readily available, and many are free. The compiled program is called an executable program, shown by the suffix .exe.

P-Code Languages

Another approach is to write in a pseudo code or p-code language, which resembles an intuitive, higher level language but is not compiled but run through software called an interpreter or virtual machine. Java is of this nature and will run on any computer that has a copy of the Java virtual machine (VM) installed in its Windows, Mac OS X, or Linux operating system. Interpreted languages run comparatively slowly, however, and, by calling on the interpreter, can breach a computer's security.

Programming

Code can be written with a simple text editor like the Windows 'Notepad', but professions prefer editors that allow built-in commands to be selected rather than laboriously keyed in letter by letter. Also important in editors is the 'debugger', an error-checker that identifies faulty terminology or where the code goes off the rails. Debugging can take longer than writing the actual code, for all that programmers generally prefer to modify a preexisting, working program than start from scratch.

Code needs to be:

1. Written efficiently, using a minimum of commands so that computer resources are not wasted.
2. Laid out neatly in blocks or modules, where each module has a specific job, and can be used repeatedly to do that job.
3. Presented in a way intelligible to other programmers, with notes throughout to explain what is being done, where and why.

Modular programs commonly consist of:

1. Sequences (a list of commands, each following the previous).
2. Branches (two or more sequences the program may follow, depending on some condition. E.g. if password is correct, then do . . )
3. Loops (commands that are continuously repeated until some condition is satisfied: repeat until counter=100, i.e. do one hundred times)

Many editors now make use of a graphical user interface, which provides pull-down menus, windows, buttons, and check boxes. The programmer simply clicks on the graphic, makes a few choices, and the code is automatically delivered. Also useful is the 'event-handler'. If the program user makes a choice between three options, the event-handler prompts for code to handle each possibility.

Client-Server Programming

Webpages are built in simple scripts: HTML and/or CSS. Dynamic webpages often use Javascript. Online forms call on small programs written in Perl and stored on the server. Webpages accessing databases are often written in PHP. All languages are far too detailed and technical to be summarized here, but online tutorials are listed below in the Sources section.

Javascript

Javascript is a scripting language, and is either embedded in the HTML of the webpage itself, or exists as a separate Javascript program called by the webpage:

<html>
<script language=javascript scrc="programname.js"></script>
<body>
</body>
</html>

Javascript has its own syntax, which is fairly intuitive. An if-else branch statement:

if (expression = value1) {
Command;
}
else if (expression = value2) {
Command;
} else {
Command;
}

Java Server Pages (JSP)

A coding standard that allows JSP scripts and Java to run small programs on HTML pages.

Perl

Perl stands for Practical Extraction and Reporting Language. Perl is a general-purpose, high level, interpreted and dynamic computer programming language invented in 1987 but vastly developed to become one of the most popular languages for web applications. Perl is best for short programs that can be entered and run on a single command line. Perl runs well on both UNIX and Windows systems.

The 'Hello World!' program is written as:

#!/usr/bin/perl
print "Hello World!\n";

where usr/bin/perl indicates that Perl is stored in the folder /usr/bin/.

PHP

The PHP or Hypertext Processor is a powerful HTML-embedded scripting language with much of its syntax is borrowed from C, Java and Perl. PHP pages are given the suffix .php (e.g. index.php) and have two important features:

1. Whole web pages can be created in php.
2. Onscreen data can be readily extracted from and inserted into server databases.

Active Server Pages (ASP)

Microsoft's answer to PHP. Will only run on web pages served by Microsoft's IIS webserver.

ActiveX and VBScript

Microsoft's equivalent to Java and Javascript respectively. Well developed languages, but not much used as they display only with the Internet Explorer browser.

ColdFusion

ColdFusion is an integrated server-side platform for writing interactive pages. The software (now marketed by Adobe) comes with design, programming and debugging tools, but the pages need a ColdFusion server.

Ruby

Ruby is an interpreted, object-oriented language used for creating interactive web pages. Although Ruby is similar to Perl and Python, Ruby abandons the C syntax of Perl and more closely resembles the syntax of programming languages like Smalltalk or Ada. A programming framework, dubbed Ruby on Rails, makes it easy to manipulate databases through web pages, and has helped to make the program popular.

Again the syntax is reasonably intuitive to programmers:

# This is a comment
print( ' What is your name? ')
myname = gets() # This is also a comment
puts( ''Welcome to Ruby, #{ myname} '' )

Questions

1. What are computer programs, and how are they written?
2. Explain the differences between machine code, assembly language, p-code and higher-level languages. What are the pros and cons of each?
3. Why is client-server programming necessary, and what languages are commonly employed?
4. Compare and contrast the use of PHP and Javascript in web pages.
5. Why is Ruby often employed to write content management systems, distance learning languages and the like? What other languages are also employed for these tasks?
6. Write the code to display an empty HTML page in PHP, Cold Fusion and Active Server Pages.

Sources and Further Reading

1. Beginning Programming: All-in-One Desk Reference for Dummies by Wallace Wang. For Dummies. June 2008.
2. HTML tutorial. W3Schools. Simple tutorials broken into handy chunks.
3. Learn All About CSS. CSSTutorial. Basics, tutorials and a CSS forum.
4. Javascript Tutorial. Quackit. Written at the beginner's level, but very detailed.
5. Javascript built-in functions. Tutorials Point. An impressive list for a simple language.
6. PHP Tutorial. Tizag. Simple but covers most needs.
7. PHP tutorial for beginners. PHPTutorials. Includes free scripts and advanced treatments.
8. Perl Tutorial. Perl Tutorial. A very full site, with background, tutorials and examples.
9. Processing forms. CGI101. Anatomy of a form-processing program written in Perl.
10. ColdFusion tutorials. EasyCFM. Includes extensive code.
11. Ruby on Rails for Beginners. Web Monkey. An introduction to rails and an example application to manage bookmarks.
12. Ruby on Rails Tutorials. Ruby Pond. A guide to Ruby tutorials and resources.
13. Best practices for programming in C. IBM. Principles apply to all programming.