Introduction to Computer Programing for Beginners

After reading this chapter, you will be able to:

  • Understand the various programming languages in use
  • Draw a Flowchart
  • Write a simple program using FORTRAN

A Computer Program is a set of instructions that directs a computer to perform some processing function or combination of functions. For the instructions to be carried out, a computer must execute a program, that is, the computer reads the program, and then follows the steps contained in the program in a precise order until completion. The process of developing computer programs is referred to as computer programming.
Computer programs are written using programming languages where Programming Languages are special languages used to write computer programs.

A computer program is actually just a series of instructions telling the computer what to do. The programmer writes instructions using the programming language, and the language tells the computer what to do. You can become a computer programmer!

Examples of Programming Languages
BASIC
FORTRAN
COBOL
JAVA
ADA
C
ALGOL
C++
QBASIC
VISUAL BASIC
PASCAL
LOGO

Flowcharts
A Flowchart is a diagram use to show the stepwise procedures used in performing a task, as in manufacturing, or solving a problem, as in an algorithm. Flowcharts are commonly used in the designing of computer programs.

 

Flowchart Symbols
Terminators (also called start and stop): This is the symbol used to begin and end the flowchart. Terminators are represented with the oval symbol.

Terminator in Flowchart

Terminator in Flowchart

Input/Output: The Input/Output symbol is used to signify entry of data or output of data from the system. The Input/Output are represented with the parallelogram
For example, a program to find the average of three numbers x, y, z. the input is x, y, and z and the output is average.

Input/Output in Flowchart

Input/Output in Flowchart

Process: this a symbol used to indicate a process being carried out. For example, still given the three number x, y and x to find the average, the process for finding the average is
(x + y + z)/3

Decision in Flowchart

Decision in Flowchart

Processes are represented with the rectangle.
Decision: this is a symbol showing that the program makes a choice between a number of options. Decisions are represented with the diamond symbol.

Decision in Flowchart

Decision in Flowchart

Connector: A connector is used to link the various steps in the program. They are represented with the arrow.

 

ALGORITHMS
Algorithm is a step by step procedure to solve a particular problem. A basic example is the process of long division in arithmetic. The term algorithm is now applied to many kinds of problem solving that employ a mechanical sequence of steps, as in setting up a computer program. The sequence may be displayed in the form of a flowchart in order to make it easier to follow.
To better understand the meaning of algorithms, lets develop an algorithm to replace a faulty electric light bulb.
Step 1: Go to an electrical shop
Step 2: Buy a good electric bulb
Step 3: Come back home
Step 4: Put off the switch controlling the light bulb to be replaced
Step 5: Unscrew the faulty light bulb
Step 6: Insert the new one you just bought
Step 7: Put on the switch you previously put off
Step 8: Did the light come on
Step 9: If yes then stop
Step 10: Else, Goto Step 1

 

Example:
Develop a flowchart to calculate the product of three numbers: a, b and c
Write the algorithm
Solution
(a) FLOWCHART

 

(b) ALGORITHM
Start
Enter A, B, C
D = A + B + C
Print D
Stop

 

(c) BASIC PROGRAM

REM PROGRAM TO FIND SUM OF THREE NUMBERS
READ A,B,C
D = A+B+C
PRINT D
END

WRITING BASIC PROGRAMS
QBasic is a very simple language to pick up, and yet it can accomplish a great deal. Let’s consider a simple program and then explain the various parts of the program

Type the following (including the quotation marks) in the QBasic interpreter:

Program 14.1

REM Hello World program
CLS
PRINT "Hello, world!"
END

.

How to Run BASIC Program

  • Click on QBASIC
  • Type your program
  • Now press F5 to run the program. You should now see a black screen, with Hello World at the top, and Press any key to continue at the bottom.
  • Press a key on the keyboard to return to the main screen

Let’s understand this simple program by looking at each individual line to see the meaning of all the words

REM Hello World program
This line is a comment. All comments in Basic begin with the keyword
REM followed by a space. So, all this line does is tell us what the program is all about eg the name of the program.

CLS – Clear Screen
This line is a command. If you know a little DOS, you probably already know this. The CLS command clears the screen. I used this to get rid of everything before we wrote the text.

PRINT “Hello, world!”
This is another command. As you can probably guess, PRINT displays text on the screen at the current cursor position. Following the PRINT keyword is a literal constant, the text to display.
You can PRINT just about anything.

END
This line, easily enough, ends the program.

VARIABLES
A variable, simply defined, is a name which can contain a value. Programming involves giving values to these names and presenting them in some form to the user. A variable has a type which is defined by the kind of value it holds. If the variable holds a number, it may be of integer, floating decimal, long integer, or imaginary. If the variable holds symbols or text, it may be a character variable or a string variable. These are terms you will become accustomed to as you continue programming.

Types of Variables
STRING “hello, this is a string”

INTEGER 5

LONG 92883

SINGLE 39.2932

DOUBLE 983288.18
The first is a string. Strings contain text.

The last four are number types. But the computer does not know what kind of value you are trying to give a variable unless you tell it! There are two methods of telling the computer what kind of variable you are using:

1. Explicitly declare the variable AS a type. This is done by using the DIM statement. Say you wanted to make a variable called number which would contain an integer (whole number, no digits after the decimal point). You would do it like this:

DIM number AS INTEGER

Then you would use that variable as an integer.

2. Put a symbol after the variable name which is defined as representing that type. QBasic has a set of symbols which represent each variable type:

$ String
% Integer
& Long
! Single
# Double

Appending one of these symbols to the name of a variable when you use it in the program tells the computer that you are using it as that type.

EXERCISES

  • What do you understand by the word computer program?
  • Differentiate between Computer Program and Computer Programming.
  • Mention 5 programming languages you know.
  • Draw a flowchart to calculate and print the area of a triangle given the base B and the height H.
  • Write the algorithm of question 4 above
  • Write a BASIC program to calculate the area of a triangle
  • Process in flowchart is represented with a rectangle while input/output is represented with a _____________________
  • Explain the word “Variable” in your own word

 

Multiple Choice Questions
1. One of these is not a programming language
(A) ADA
(B) COBOL
(C) RECOL
(D) ALGOL
2. Which symbol in flowchart is used to represent input/output
(A) diamond
(B) oval
(C) rectangle
(D) parallelogram
3. The diamond symbol as shown below is used to represent what?
(A) decision
(B) process
(C) input/output
(D) start and stop
4. What is the purpose of the command CLS in BASIC programming language.
(A) changes the color of the screen
(B) changes the color of a line
(C) clears the errors in the program
(D) clears the screen
5. Which of these is not a type of variable in BASIC?
(A) string
(B) long
(C) float
(D) double

START

D=A+B+C

PRINT D

STOP

ENTER, A, B, C