Code deciphering

The power of digital computing is that it can not only be used, but also instructed or 'programmed'. Modern computers are programmed through their Central Processing Unit (CPU). Although most CPUs can only perform a small number of mathematically orientated operations, they can do so with a high level of reliability, tremendous speed and in infinitely large combinations. This set of operations is called the 'instruction set'. Computer programs are sequences of such instructions and data - usually called machine code - and is extremely complex.

A programming language, on the other hand, consists of a group of keywords closely resembling English, a familiar mathematically-orientated syntax, algebraic-like variables and various simple mathematical operators, such as plus or minus. This makes programming a CPU a much simpler task.

In order for a programming language to be used, however, 'compilers' and 'interpreters' must translate a program's source code into machine code.

A compiler assesses the best way in which a program's source code can be translated into machine code and then generates that machine code. Since the resulting program is stored on a disk, it can be executed time and time again, if required. Compiled languages include C and Fortran.

In contrast, an interpreter is an instructable program, which means it can translate the instructions for its language into machine code and execute it immediately. It does not output an executable program. Examples of interpreted programming languages are PHP and Perl.

MODERN PROGRAMMING LANGUAGES

At the most basic of levels, there are six important features that define modern programming languages - control structures, data structures, functions, syntax, complexity and general design.

Control structures

The control structures of a language allow the programmer to determine the 'path of execution' of a program. That is, which code gets executed in which order. For example, say you need to design a program that tells its user if it is before, at or after midday. Each of the three forms of output can be written in the code of a program and output only if a condition is true: namely, that the time of day correlates to the particular output. In this case, control is placed with the code that meets the condition. Such structures are usually called if/else comparisons or case switching.

Or, you may want to design a program to add together the numbers 1 through 1000. You could do this by typing 1000 lines of arithmetic (n = 1; n = n + 2; n = n + 3, and so on) or by performing such a calculation in a loop which lasts 1000 iterations. Such loops are usually called for or while loops. These control structures also have associated commands that can be used either to jump to the beginning of the next iteration of the loop or to break out of the loop altogether.

Some languages have more complicated control structures, such as goto statements and exception handling. Such control structures tend to be unpopular as they complicate languages and make them hard to read.

Data structures

Popularly called variables, data structures allow programmers to interact with data. Generally speaking, the most basic data structures allow the storage of numbers or characters. Some languages allow the programmer to design their own data structures. This means that programmers can group related data - making the source code easier to write and often increasing the performance of the program.

Some languages take this concept to the limit, designing much of the language around it. They are often termed 'object-orientated', since they treat all variables as objects and have a framework that complements this.

Functions

Functions allow programmers to modularise their code. If source code becomes complex or if programmers are using the same code many times throughout a program, it is often convenient to reduce this to a single line: the function.

Modern languages also support functions that can take 'arguments'. Arguments are simply variables passed from a section of code to a function. Since they can be manipulated inside the function in the same way as they can be in the higher-level section of code, functions are extremely useful.

On top of this, modern programming languages often come with a standard library of functions to perform basic operations. This greatly simplifies development of programs.

Syntax

Syntax is the grammar of a language. It defines how variables are declared and used, how functions are called and the results stored, how mathematical operations can be performed and how lines of code get executed.

Complexity

Languages with a large number of features can be highly taxing on a programmer. There are so many ways for any given problem to be solved, and many of these ways are so complicated, that bugs and performance problems are easily introduced.

By the same token, languages which oversimplify challenging programmatic problems can limit the programmer by lacking functionality.

General design

The general design of a language relates to the structure of a written program and its underlying focus. Some languages are designed to appeal to everyday software development, others focus on graphical user interfacing or on attempting to implement programmatic theory, such as object orientation, within the structure of the language.

PROGRAMMING IN C

Most modern languages stem from concepts implemented in C and its immediate predecessors. To this end, we will look at C in depth and see how its ideas and flaws are tackled in other languages.

A basic C program

Probably the most elementary of C programs is Hello World (hello.c). This program (below) prints Hello World to the screen.

#include

int main(void)

{

puts("Hello World");

return(0);

}

As simple as it is, hello.c shows a lot about the structure of C. It begins with an 'include' control statement that allows C programmers to use the C standard library function printf(), defined in stdio.h. After this section is written 'int main(void)', which is the declaration of the main function. To all intents and purposes, when the hello program is run, it is the commands inside the curly braces of main() that determine what the program does.

In hello.c, there are only two lines of code in main(). The first calls the puts() function to output the text "Hello World" to the screen. The second line in main() is a return statement. In hello.c, zero is returned. This is the 'exit status' of the program. Conventionally, an exit status of zero means that the program was executed successfully and there were no errors.

Beyond Hello World

Of course, there's much more to C than hello.c. Let's look at C in terms of the criteria set out above to see what it comprises.

Control structures

The hello.c program introduces the include and return control structures. Other control structures in C include for and while loops, if/else comparisons, and the switch statement, among others.

In C, a for loop iterates as long as a certain condition is true. When an expression is evaluated, an incorrect result returns zero (false) while a correct result returns anything but zero, usually one (true).

C also implements the if/else control structure. Consider the program discussed earlier that told the user if the hour was before, at or after midday. Given a variable h, which is the current hour on a 24 hour clock, C offers the following possibility. if(h < 12) { puts("It is before midday"); } else if(h == 12) { printf("It is midday"); } else { printf("It is after midday"); } This section of code illustrates how if/else comparison works. The first line tests if the hour is before 12 o'clock. If it isn't, control is switched to the next conditional, h == 12. This is true if h is equal to 12. If this is false, control is given to the final block of code at else, which encompasses all other possibilities not covered above.

Data structures

C has several built-in data structures. These are int (for whole numbers), double and float (for numbers requiring decimal accuracy, such as 3.141), char (for characters), void (a special data structure) and some variations.

These keywords are used to define variables. For example: char c; char s[256]; char hw[] = {"Hello World"}; The first line defines a variable c which can store a single character, such as a or b. The second line defines a variable s which can hold 256 characters (an array of characters). The third line is a short cut in C that allows programmers to define a variable hw which contains Hello World and is the length of that string. On the other hand: int i; int n[12]; float f; double d; The first line defines an integer i which can store any single integer between -32768 and 32767. The second line defines n, which can store 12 such integers. The third and fourth lines define precision floating point variables f and d. In C, the programmer can define any number of variables with any alphanumeric name, provided that the same variable isn't defined twice in the same function.

C also allows programmers to define their own data structures.

There is one other data structure C supports: the pointer. Pointers 'point' to the location in memory where the data for a given variable is stored.

Since the number of bytes is an integer, C programmers can apply general arithmetic to pointers in order to have them point to different locations in memory. Such operations closely replicate machine code. This helps C stand out in terms of performance.

Syntax

The syntax of C reads very much like algebra: evaluate from left to right and store the results in the variable on the left-hand side.

The other important point is that the code in control structures is written inside curly braces. If these were omitted, the code would not work correctly.

All other commands to be executed are followed by a semi-colon (;). This makes code easier to understand - both for the programmer and the compiler.

The final point in terms of C syntax is commenting. You can store information, such as the name of the program or why you chose to do something a particular way, in a structure that the compiler ignores.

Summary

C is a very versatile language. It has a large range of features, a simple design and is widely used in the software development industry. It is, however, a little daunting for beginners. Concepts such as pointers require a good deal of experience in order to be used properly.

Language: C

Complexity: Medium

Recommended prior knowledge: "Some experience in programming "and mathematics Further reading: "Dan Gookin, C For Dummies "($49.95; ISBN: 156884915X); "Paul Davies, The Indispensable guide to C ($85; ISBN: 0201624389)PROGRAMMING IN C++ C++ is an extension of C, which means that everything we've already covered on C holds true. The way in which C++ really differs is at the level of data structures.

Data structures in C++

C allows the programmer to define data structures using the typedef struct statement. C++ takes this to extremes with classes and objects. A class is akin to a C language data structure. An object, on the other hand, is an 'instance' or variable of that structure.

However, C++ structures are much more complicated than those in C. class Person { public: void putperson(); char name[256]; float height; float weight; int age; private: static const char intro[] = "Details of a person"; }; This structure differs from the C structure in two important ways. The first is that there are two sections to the class: public and private (there can be others). Those listed under public or private are called public or private 'members' respectively. Whether a member is public or private dictates who can access it. Public members can be manipulated anywhere an object of the class occurs in the code; private members can only be used internally by the object.

The other major difference is the line 'void putperson(char *name)', which is the definition of a 'member function'. Member functions are able to access private members.

Advanced features

The object-orientated nature of C++ offers the programmer much more object-related functionality. Extra features include inheritance, which allows different classes to use the same member functions; templates, to make classes even more generic by allowing the programmer to pass the data types of variables to a class when creating a new object; and function overloading.

Summary

While C++ extends C, experienced programmers tend to argue over just how useful that extension is. C++ is often only suited to applications dealing with large amounts of related information.

To this end, C++ is rightfully the cornerstone of graphical interface development and game design, which require such features.

Language: C++

Complexity: Medium to complicated

Recommended prior knowledge: C and some experience with basic object orientation Further reading: Bjorne Stroupstrup, The C++ Programming Language ($128; ISBN: 0201700735)JAVA Java is an almost 'text book' implementation of object orientation. Whereas C++ classes are used to extend and simplify aspects of C, in Java the class is the building block of the language. tData structures in Java In general, the action in Java takes place inside classes. Consider Hello World in Java: class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); } } Notice that the main() function (called a 'method' in Java) is defined inside a class, and that its code block also appears there, unlike C++. If you wanted to define other methods inside the HelloWorld class, they would follow underneath main().

The concept of classes is also applied to the standard library of functions. The println() method is a member of the out object (which is itself a member of the System class). This illustrates how well structured Java is.

The design of Java

In one way, Java has been designed the way C++ should have been - from the ground up, rather than as an extension. This means that it implements all the best object-orientation concepts from C++ and builds the language on top of them.

Java also addresses a few other key problems in the software development industry, including portability, security and memory management. Portability, in particular, is a major problem for software developers. A program compiled for Windows, for example, will not run natively on Linux. Java attempts to solve this by compiling source into a generic 'byte-code' which is translated to machine code on individual machines at run time. In theory, Java programs can be run on any computer architecture.

One of the main problems is that this fix comes at a performance price, as anyone who has used Web-based Java applications will know.

Unlike C and C++, Java does not give the programmer such close access to the computer. The code is executed on a 'virtual machine' whose internals are mapped to the actual computer hardware. This means that, unless the Java runtime environments have security problems (and so far they have), if a program written in Java crashes, it will not cause a security problem.

Finally, Java takes care of memory management for the programmer with a 'garbage collection system'. This system detects when data in variables is no longer required by the program and frees up the memory they have used.

Summary

In theory, Java solves many of the problems of C and C++. However, it is still the new kid on the block and has experienced its fair share of problems, particularly relating to performance.

Language: Java

Complexity: Medium

Recommended prior knowledge: Some experience with object orientation concepts Further reading: http://java.sun.com/docs/books/tutorial/ VISUAL BASIC Visual Basic is a beginner's 'visually orientated' programming language. Visual Basic (VB) has grown from the long history of BASIC languages, two of which Microsoft used to ship with DOS (GW-BASIC and Q-BASIC). These BASIC languages were aimed toward beginners looking to learn more about programming (BASIC stands for Beginner's All-purpose Symbolic Instruction Code).

Visual Basic continues this focus by simplifying the designing and building of graphical componentry.

General design of Visual Basic

The design of VB is remarkably different to the other languages covered in this feature. There are two different programmatic levels to the language: the first is writing code, and the second is designing graphical interfaces to correlate to sections of code, in much the same way as one might design something in a graphics development application.

As Java was built on the concept of classes, VB is built on the concept of the form. A form is like a canvas. The programmer can build buttons (known as widgets), text boxes, check boxes - indeed, any visual component that Windows users are accustomed to seeing on their screens.

Each visual component is then associated with code. This can determine what happens when the program is executed or when a button is clicked, and so on. In the case of the Hello World example, the programmer would have designed a form with a text box called tb1: tb1.Text = "Hello World"; This code would result in the text box containing the words Hello World. What if you wanted a button to close the application? You would design a button and call it 'Quit', and associate the following code with it:

Private Sub cmdQuit_Click()

End

End Sub

The first line defines a sub routine, or method, of the Quit widget - an event. Events are sensitive to the way in which the user works with the interface. That is, Visual Basic runs this code when the button is clicked. The word Click at the end of the sub routine name is not actually a part of the name. Instead, it specifies what the code is used for. End, as its name suggests, ends the program.

Data structures in VB

Visual Basic supports both basic data types, like integer and character variables, and user-defined data types, called modules. It also provides more intricate data structures designed to hook into databases, printers and the file system.

As in Java, these structures are designed so that just about all operations that need to be made on the data structure can be done by calling routines from it.

Summary

Visual Basic is a very simple language. It allows beginners to produce simple applications quickly and effectively without having to dabble in the intricacies of software development.

This is usually its main problem, however. By simplifying complicated concepts, VB programmers often struggle when they need more control over their program or have to design larger projects.

The other consideration with Visual Basic is that, in order to code and compile applications in VB, programmers need to purchase software from Microsoft. More information can be found at www.microsoft.com.

Language: Visual Basic

Complexity: Beginner

Recommended prior knowledge: Experience in using Windows Further reading: Richard Mansfield, Visual Basic 6 Weekend Crash Course ($49.95; ISBN: 0764546791)FortranAt its release, Fortran was primarily aimed at the scientific community. It gave the programmer not only the ability to produce programs that could perform a wide range of mathematical operations but also to build logic into the flow of programs - making it very popular.

Fortran is still in use today, being well suited to solving complex mathematics problems in high-performance computing. All other uses for the language have largely been superseded by C.

Perl

Perl has been in development since 1987. It is one of the first languages to be designed by developers connected by the Internet. It is developed in an open source environment, and has an extremely large number of features. This makes it the language of choice for UNIX system administrators looking to automate administrative tasks.

See more at www.perl.org.

THE FUTURE OF PROGRAMMING

Much work is being put into a large number of all-purpose programming languages. Over the last few years, companies such as Microsoft, and developers in the open source community, have been looking at ways to reduce the amount of work involved in maintaining languages by designing a shared component system.

Microsoft has incorporated this under the .NET banner, which will see languages such as Microsoft Visual C++ and Visual Basic share components of the new (Microsoft-created) C# language. Microsoft is relying on similar technology to Sun's Java language to make this happen.

Members of the Perl, Python and PHP core development team, meanwhile, are taking a different approach. They are attempting to design componentry that can be translated and incorporated into the design of the language with little to no performance cost.

Both approaches aim to reduce development time for language designers and users alike. Only time will tell if they will be successful in achieving this goal.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World

Comments

Comments are now closed.

Latest News Articles

Most Popular Articles

Follow Us

GGG Evaluation Team

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell

LIFEBOOK UH574

The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi

STYLISTIC Q702

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott

STYLISTIC Q702

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Resources

Best Deals on GoodGearGuide

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?