0% found this document useful (0 votes)
107 views10 pages

Basics of Computer Programming Guide

This document provides an introduction to computer programming, explaining key concepts such as programs, programming languages, and the role of a programmer. It outlines the stages of program development, the qualities of a good program, and common programming errors. Additionally, it emphasizes the importance of good coding techniques and the use of algorithms in problem-solving.

Uploaded by

clintonomondi890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views10 pages

Basics of Computer Programming Guide

This document provides an introduction to computer programming, explaining key concepts such as programs, programming languages, and the role of a programmer. It outlines the stages of program development, the qualities of a good program, and common programming errors. Additionally, it emphasizes the importance of good coding techniques and the use of algorithms in problem-solving.

Uploaded by

clintonomondi890
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Lesson 2 - Introduction to Computer Programming

Computer programming is the act of writing computer programs, which


are a sequence of instructions written using a Computer Programming
Language to perform a specified task by the computer.
Computer Programming is fun and easy to learn provided you adopt a
proper approach. This tutorial attempts to cover the basics of computer
programming using a simple and practical approach for the benefit of
novice learners.
Basic Concepts of Computer Programming
 Program
 Programming languages
 Programmer
 Software
 Computer vs Human beings
Definitions:
Program: Is a set of step by step instructions written in a language that the
computer can interpret and understand. They are used to guide a computer
to perform a task (simple or complex).
Programming Problem: - A task that requires a program to be written.
Programming language: - This is a program with a special role i.e. it
enables us to come up with other programs. Has instructions that enable us
to write programs or modify an existing program. C, C++, JAVA, PHP, JSP,
C#, JAVASCRIPT ETC
They have features of language
Grammar : SYNTAX
Vocabulary
Interpretation/ meaning: SEMANTICS
Programming: - This is the art of formulating a set of instructions to enable
the computer to perform certain tasks.
Software: - A program that has been debugged, tested, documented and
ready to be used.
Computer Programming - Overview

1
Introduction to Computer Program
Before getting into computer programming, let us first understand computer
programs and what they do.
A computer program is a sequence of instructions written using a
Computer Programming Language to perform a specified task by
the computer.
The two important terms that we have used in the above definition are −

 Sequence of instructions
 Computer Programming Language

To understand these terms, consider a situation when someone asks you


about how to go to a nearby KFC. What exactly do you do to tell him the
way to go to KFC?
You will use Human Language to tell the way to go to KFC, something as
follows −
First go straight, after half kilometer, take left from the red light and then
drive around one kilometer and you will find KFC at the right.
Here, you have used English Language to give several steps to be taken to
reach KFC. If they are followed in the following sequence, then you will
reach KFC −
1. Go straight
2. Drive half kilometer
3. Take left
4. Drive around one kilometer
5. Search for KFC at your right side
Now, try to map the situation with a computer program. The above
sequence of instructions is actually a Human Program written in English
Language, which instructs on how to reach KFC from a given starting point.
This same sequence could have been given in Spanish, Hindi, Arabic, or any
other human language, provided the person seeking direction knows any of
these languages.
Now, let's go back and try to understand a computer program, which is a
sequence of instructions written in a Computer Language to perform a
specified task by the computer. Following is a simple program written
in Python programming Language
print "Hello, World!"
The above computer program instructs the computer to print "Hello, World!"
on the computer screen.
2
 A computer program is also called a computer software, which can
range from two lines to millions of lines of instructions.
 Computer program instructions are also called program source code
and computer programming is also called program coding.
 A computer without a computer program is just a dump box; it is
programs that make computers active.
As we have developed so many languages to communicate among
ourselves, computer scientists have developed several computer-
programming languages to provide instructions to the computer (i.e., to
write computer programs). We will see several computer programming
languages in the subsequent chapters.

Introduction to Computer Programming


If you understood what a computer program is, then we will say: the act
of writing computer programs is called computer programming.
As we mentioned earlier, there are hundreds of programming languages,
which can be used to write computer programs and following are a few of
them −

 Java
 C

 C++

 Python

 PHP

 Perl
 Ruby

Uses of Computer Programs


Today computer programs are being used in almost every field, household,
agriculture, medical, entertainment, defense, communication, etc. Listed
below are a few applications of computer programs −
 MS Word, MS Excel, Adobe Photoshop, Internet Explorer, Chrome, etc.,
are examples of computer programs.
 Computer programs are being used to develop graphics and special
effects in movie making.
 Computer programs are being used to perform Ultrasounds, X-Rays,
and other medical examinations.
3
 Computer programs are being used in our mobile phones for SMS,
Chat, and voice communication.

Computer Programmer
Someone who can write computer programs or in other words, someone
who can do computer programming is called a Computer Programmer.
Based on computer programming language expertise, we can name a
computer programmers as follows −

 C Programmer
 C++ Programmer

 Java Programmer
 Python Programmer

 PHP Programmer

 Perl Programmer

 Ruby Programmer

Algorithm
From programming point of view, an algorithm is a step-by-step procedure
to resolve any problem. An algorithm is an effective method expressed as a
finite set of well-defined instructions.
Thus, a computer programmer lists down all the steps required to resolve a
problem before writing the actual code. Following is a simple example of an
algorithm to find out the largest number from a given list of numbers −
1. Get a list of numbers L1, L2, L3....LN
2. Assume L1 is the largest, Largest = L1
3. Take next number Li from the list and do the following
4. If Largest is less than Li
5. Largest = Li
6. If Li is last number from the list then
7. Print value stored in Largest and come out
8. Else repeat same process starting from step 3
The above algorithm has been written in a crude way to help beginners
understand the concept. You will come across more standardized ways of
writing computer algorithms as you move on to advanced levels of
computer programming.
Program/software life cycle (stages of program development)

4
The process of developing a program generally involves six stages which are
referred to as the program development life cycle and shown below:
1. Requirements specification: States the purpose/function of the
program. Understand the problem or task: A program to compute the
area of a rectangle. It involves the definition of inputs, transformation,
outputs and treatment of exceptional situations.
2. Program design: This is where the programmer identifies the
processing tasks required and the precise order in which they are to be
carried out, that is the algorithm from which the computer program will
be produced. The design process takes little or no account of the
programming language to be used.
3. Coding the program: Involves converting the design specification (the
algorithm) into actual computer instructions in the computer language to
be used. If sufficient care has been taken at the design stage, coding is
an almost mechanical process for an experienced programmer since
most of the hard work of analyzing the problem and breaking it into a
sequence of small, simple steps has already been done during the design
stage.
4. Testing and Debugging: Debugging is the detection and correction of
errors that may exist in the program. The only way of completely
confident that a program will behave as expected, under all
circumstances that conceivably arise, is to test the program thoroughly
before implementing it. Program testing includes creating test data
designed to produce predictable output.
5. Documentation: Having written and debugged the program. It must be
documented. There are two main types of documentation.

a) User Guide: These are guides that are intended to help the user to
use/operate the program with minimal or no guidance
b) Technical manuals: These are intended for the system
analysts/programmers to enable maintenance and modification of the
program design and code.

6. Implementation/Operation: At this stage, the system designers will


actually install the new system, create data files and train people to use
the new system. A common practice is to run the new system in parallel
with the old system to ensure that if something goes wrong, the business
doesn't come to a grinding halt. After a number of parallel runs, the old
system will be abandoned and the new system will be fully be
operational. At this point the system goes into the final stage off
monitoring and review (or evaluation) where the performance of the
system is evaluated with reference to the initial requirements.

5
7. Maintenance and Evaluation:

Qualities of a good program


1. Accuracy: The program must do what it was supposed to do. The
results/output should be correct as per specifications.
2. Reliability: The program must always do what it was supposed to do
and never crash.
3. Robustness: The program should cope with invalid data without creating
errors or stopping without stopping without indication of the cause.
4. Usability: To make sure that your program is easy to use, make sure
that the flow of your program is logical and provide a proper user
guide/manual.
5. Readability: To make your code more readable, use the following
rules/guidelines

a) Use Meaningful names: When coming up with the names for


variables or constants, use names that hint what the variable or
constant holds e.g. balance, tax, tax_rate, etc. instead of using letters
like a,b,c.
b) Use Comments: Use comments at the module level to explain the
purpose of the module and within the code to explain complicated
algorithms or highlight error prone sections.
c) Use Indentation: Lay your code neatly and make proper use of
indentation to reflect the logic structure of the code.
d) Modularization: Split your program to make it easier to understand.
This is always achieved by including functions in your program.
Modularizing a program, reduces the effort required later to change
the program since changes need only to be made to the module
whose functionality changes or if a new feature is to be added then
the new module can be added. Testing and debugging also becomes
easier with small independent modules.
e) Line Spacing

6. Simplicity: The clarity and accuracy of a program are usually enhanced


by keeping things simple.
7. Efficiency: Efficiency is concerned with execution speed and efficient
memory utilization.
8. Generality: Generality. A program should be as general as possible,
within reasonable limits. We may design a program to read in the values
of certain key parameters rather that placing fixed values into the
program.
6
Good programming practice
1. Apply the computer problem solving steps:

-Identify and study problem


-Formulate and represent a solution to the problem (Algorithm)
-Use any convection to represent the algorithm
-Check that the algorithm works
-Write the code (program)
-Test and document code
-Run program
2. Plan the work
3. Document your work

Functions

Functions are small units of programs and they are used to carry out a
specific task. For example, the above program makes use of two
functions: main() and printf(). Here, the function main() provides the entry
point for the program execution and the other function printf() is being used
to print an information on the computer screen.

You can write your own functions which we will see in a separate chapter,
but C programming itself provides various built-in functions like main(),
printf(), etc., which we can use in our programs based on our requirement.

Some of the programming languages use the word sub-routine instead of


function, but their functionality is more or less the same.

Comments

A C program can have statements enclosed inside /*.....*/. Such statements


are called comments and these comments are used to make the programs
user friendly and easy to understand. The good thing about comments is that
they are completely ignored by compilers and interpreters. So you can use
whatever language you want to write your comments.

Whitespaces

When we write a program using any programming language, we use various


printable characters to prepare programming statements. These printable
7
characters are a, b, c,......z, A, B, C,.....Z, 1, 2, 3,...... 0, !, @, #, $, %,
^, &, *, (, ), -, _, +, =, \, |, {, }, [, ], :, ;, <, >, ?, /, \, ~. `. ", '.

Apart from these characters, there are some characters which we use very
frequently but they are invisible in your program and these characters are
spaces, tabs (\t), new lines(\n). These characters are called whitespaces.

These three important whitespace characters are common in all the programming languages and they
remain invisible in your text document −

Whitespace Explanation Representation

New Line To create a new line \n

Tab To create a tab. \t

Space To create a space. empty space

A line containing only whitespace, possibly with a comment, is known as a


blank line, and a C compiler totally ignores it. Whitespace is the term used in
C to describe blanks, tabs, newline characters, and comments. So you can
write printf("Hello, World!" ); as shown below. Here all the created spaces
around "Hello, World!" are useless and the compiler will ignore them at the
time of compilation.

COMPUTER PROGRAMMING ERRORS


ERROR: Is something that will make the program not to run or give
erroneous result.
TYPES OF ERRORS:
1. SYNTAX ERRORS: Are caused when you violate the rules of a
programming language.

Causes
a) Missing semicolon to terminate a statement
b) Misspelled reserved word
c) A reserved word consisting of a capital letter.
d) { without corresponding } or ( without corresponding )
e) " without corresponding "

8
f) Use of wrong expressions float x; area=l*w; Error: l*w=area;
g) Use of undeclared identifiers
h) Failure to include the appropriate header file.

2. SEMANTIC ERRORS/RUNTIME ERRORS: Caused when the computer is


not able to understand the meaning of a statement. And thus is not able
to do what it is being instructed to do.

Causes
a) Division by zero
b) Computing the square root of a negative number
c) Endless loops

3. LOGICAL ERRORS: The program is giving erroneous result.

Causes
a) Wrong formula
b) Failure to initialize variables
c) Testing wrong conditions in loops

4. LINKING ERROR: The computer is not able to get the intermediate


programs required by the program.

PROGRAMMING TESTING STAGES


There are several stages in testing the program:
1. DESK CHECKING: After writing the program, the programmer goes
through the program on paper to pick up any error that would cause
extra work at a later stage
2. TRANSLATOR SYSTEM CHECKING: After keying in the program, it is
checked using the translator to detect any syntax errors. The
programmer corrects the errors and re-submits the programs until an
error free listing is obtained.
3. PROGRAM RUN WITH TEST DATA: Test data is created designed to
produce predictable output. The program is then run using this data and
its output is compared with the predictable output. Test data should
include all important variations and extremes or including data errors to
ensure that the program doesn't grind to an halt if incorrect data is read
in.
9
4. DIAGNOSTIC PROCEDURES: For complex programs diagnostics
procedures such as trace routines, may be used to find logical errors. A
trace prints result at each processing step to enable errors to be detected
quickly. If a trace routine is not available the programmer inserts
instructions in the program to print results at key points.
5. SYSTEM TEST WITH ACTUAL DATA: As part of the "Acceptance Trials"
new programs are usually run in parallel with the existing system for a
short while so that results can be compared and adjustments made. The
systems run using actual data.

Good Coding Techniques


There are various techniques that are usually recommended to be used by
the programmer when coding
a program to ensure that the program produced are of good quality. They
include the following:
1) Documentation lines: Are lines to explain what the instructions of a
program are doing. They help one to be able to read and understand
the program. They also make the program easy to write and modify
because another programmer can understand the code easier. They
are also known as comment lines.

There are two types:


a) Line comments//bbbbbbbbb vbbb bbbbb
b) Block comments /*ffffffff

ggggggggg */
2) Line spacing: Leaving empty lines between sections of codes so as to
visibly distinguish among the individual sections.
3) Indenting code
4) Use of meaningful identifiers

10

You might also like