Web Design and Programming Projects

Back to resources main


A Brief Introduction to Programming

Paul S. Wang


To bring the non-CS majors up to speed in programming is not an easy task. But this document gives some help to those who are motivated to learn and it can make absorbing the programming material in the Web Design and Programming course much easier.

What is a computer

A general-purpose computer is an electronic device that can carry out, or execute, any sequence of instructions stored in its memory. The stored sequence of instructions is called a program.

So the computer is a machine that will perform any task for which a program can be written.
Modern computers are complicated machines:

  • Central processing unit (CPU) that executes the instructions
  • Random-access memory (RAM) to storing the program under execution
  • Data storage devices (hard disk, floppy disk, Zip disk, ...) where programs and other files can be stored and retrieved instantly
  • Operating system (a program) that help run the computer and provide user access and control
  • Graphical user interface (GUI) (another program) that make it possible to have windows on the screen and mouse/button interactions with the user
  • Networking to connect the computer to other computers or to the Internet

Thus, a computer is actually a combination of hardware and software and can easily be the most sophisticated mechanism the human race has ever built. Thus, we usually refer to a computer as a computer system. The same hardware under different operating system or different GUI can become very different.

Please refer to a book on "Computer Appreciation" for more fascinating details on "the machine that changed the world" (a 5-episode TV series available from PBS).

What is a program

A program is a sequence of instructions to perform a desired task. It is not unlike a recipe for cooking a dish or directions to a party location, only it is very precise and detailed. You write a recipe in English. But you write a program in a language the computer understands. Popular computer languages are C, C++, Java, Perl, Lisp, and so forth. To write programs in a language, you need to know the syntax (grammar) and semantics (meaning) of the programming language.

Win95, MAC OS, and UNIX are all programs written to control and operate computers so they are easy to operate by users. Netscape and Internet Explorer are programs for users to surf the Web. Such programs are known as Web browsers. Paint Shop, Photoshop, and CoralDraw are programs to help you create and manipulate graphics and images. Vi, Emacs, and WordPerfect are text editing programs to help you enter and revise text-based documents.
There are many other software tools, too numerous to mention. A software tool is usually also programmable: it can carry out a sequence of instructions and achieve tasks capable by the tool. Such programs are usually known as "scripts" because they are only understood and carried out by the target tool.

What is programming

Writing a program or script in a computer language is programming. The program consists of a sequence of instructions to be carried out one after another in a strict sequence. The next instruction will be started only after the previous instruction has been completed. Thus execution flows from one instruction to the next and this sequencing is referred to as the "control flow".

Here is a make-believe program for transforming all upper case characters to lower case in a file.

Step 1: Open the input file (infile)

Step 2: Open the output file (outfile)

Step 3: Read in the next character (char) from infile,

Go to Step 6 if no more characters.

Step 4: Write char to outfile if it is not an upper case character,

write the lower case of char if it is upper case.

Step 5: Go back to Step 3

Step 6: Close infile

Step 7: Close outfile

Step 8: Finished

This example shows several things. The infile, outfile, and char are "variables" that can hold different values that are assigned to them as the program executes. Files can be opened for input or output (I/O) and must be closed at the end. Control flow can be diverted to a specific place in the program by a statement like "go to ...".

In an actual program you would have to have specific constructs for

  • decision making --- steps can be taken or not taken depending on testing certain conditions
  • iterations --- repeated execution of a group of steps until certain conditions are met
  • functions --- grouping a number of steps into a larger step and giving it a name making it into a "function" that can be "called" from any step in your program. Functions are sometimes also know as routines or subroutines.

Programming Concepts

A program to a computer is like a recipe to a person. A program contains "statements" just like a recipe contains "sentences". Usually a statement has a special character that terminates the statement, just like a sentence having a period (.) at the end. A program is written in a computer language such as C, C++, Java, or Perl. A recipe is written in a natural language such as English, Chinese, French, or Greek.

In programming, just like in natural languages, syntax (grammar) rules govern what are acceptable statements and semantic (meaning) rules govern what each acceptable statement means.

  • An assignment statement assign a value to a variable:
          x = 3 * y;
          x = x - 1;
  • A conditional statement does one thing or the other depending on a condition:
          if ( i > 0 )  
               j = i;
          else j = -i;
  • An iteration statement continues to execute a group of statement as long as a condition is met:
           i = 10;
           while  ( i > 0 )
           {    print(i);
                i = i -1;
           }
  • A function gives a group of statement a name to be called from any part of your program:
          double average(int i, int j)
          {    i = i + j;
               return ( i/2.0 );
          }
  • Function call:
          int a, b;
          double c;
          a = 10;
          b = 20;
          c = average(a,b);
  • Declarations : variables a and b are "integer type" variables, c is a double type variable. Some languages use more type declarations others use less or none. Declarations make a program run faster and makes programming less error prone. But less declarations simplifies programming.

  • Input/Output: statements to read data from a use or a file or to display data to a user or into a file. A program usually has a "standard input" channel to receive input entered on the keyboard and a "standard output" channel to display data on the terminal screen.

Creating A Program

You usually use a text editor to enter the program into a "source code file". Some computer systems offer "program development environments" for creating programs in different languages. In any case, you first create one or more source code files, then either compile or run it to test it for correctness. If errors are found, you fix the errors and test again. It is important to test your program with different kinds of input data and anticipate problem areas.

Once a program is tested it can be put to use.


Go to Top of Page