COMP 103 Introduction to
Programming
DCSIT,CU
INTRODUCTION
INTRODUCTION
• A Program is a set of sequenced instructions
that may be executed by a computer. Also
called codes.
• The set of instructions directs the computer
system to carry out required actions to
achieve desired results.
• It is a set of instructions that tell the
computer/hardware what to do.
• These instructions are made of statements
used in a programming language, such as
BASIC, FORTRAN, COBOL, C++, JAVA, etc.
• Programming is the art of developing a
solution to a problem.
TYPES OF PROGRAMMING LANGUAGES
• Programming language is a set of grammar ( lang.
rules), characters, symbols and words – the vocabulary
in which instruction/code is written
• There are 5 levels or generations of programming
languages.
• The five generations of programming languages:
lowest level – highest level
– Machine language - (lowest, data rep. as 0s and 1s -bits)
– Assembly language - (mnemonic codes), uses assembler
– High-level languages - (procedural languages), uses
compiler.
– Very-high-level languages - (problem-oriented languages –
4GL);
– Natural languages (human language-Eng, Ga, Twi )
NATURAL LANGUAGES & AI
• Natural languages are designed to give people (users) a more
human (“natural”) connection with computers.
• These languages are human languages: English, French,
German, Japanese, or whatever.
• Natural languages translate the human voice into code that
the computer understands. The computer analyses the
human language for its syntax and it meaning.
• Natural languages are part of the field of study known as
artificial intelligence (AI).
• AI is a group of related technologies that attempt to develop
machines to emulate human-like qualities, such as learning,
reasoning, communicating, seeing, and hearing.
SOME MAJOR LANGUAGES
• Major languages used today are
– ALGOL, FORTRAN, BASIC, COBOL, Pascal
– VISUAL BASIC, VISUAL C++, C, C++, Java
– Scripting languages
– Python and many more
• All these languages must be converted into
machine language before the Computer can
use it.
• Why?
• Conversion is done by a language translators
WHAT ARE TRANSALTORS
• A translator is a program that converts a
program written in one language (assembly or
high-level) to another language (machine).
Language translators are of three types:
– Assemblers
– Compilers
– Interpreters
Assembler, Compiler & Interpreter
• ASSEMBLER or ASSMBLER PROGRAM
– is a program that translates the assembly-language
program into machine language.
• COMPILER
– is a language translator that converts the entire program
of a high-level language into machine language before the
computer executes the program.
– The object code can be saved and can be executed later
without compiling
• AN INTERPRETER
– is a language translator that converts each high-level
language statement into machine language and executes it
immediately, statement by statement.
– No object code is saved.
Compiler vs Interpreter
• A compiler translates the code into an
intermediary form. This step is called
compiling, and produces an object file. The
compiler then invokes a linker, which turns the
object file into an executable program
• An interpreter translates a program as it reads
it, turning the program instructions, or code,
directly into actions.
Compiler vs Interpreter
• Because interpreters read the code as it is
written and execute the code on the spot,
interpreters are easy
• for the programmer to work with. Compilers,
however, introduce the extra steps of
compiling and linking the code, which is
inconvenient.
Compiler vs Interpreter
• Compilers produce a program that is very fast
each time it is run. However, the time-consuming
task of translating the source code into machine
language has already been accomplished
• Another advantage of many compiled languages
like C++ is that you can distribute the executable
program to people who don't have the compiler.
With an interpretive language, you must have the
language to run the program.
Compiler vs Interpreter
• For many years, the principle goal of computer
programmers was to write short pieces of
code that would execute quickly.
• The program needed to be small, because
memory was expensive, and it needed to be
fast, because processing power was also
expensive.
Compiler vs Interpreter
• As computers have become smaller, cheaper, and
faster, and as the cost of memory has fallen, these
priorities have changed.
• Today the cost of a programmer's time far
outweighs the cost of most of the computers in
use by businesses.
• Well-written, easy-to-maintain code is at a
premium. Easy- to-maintain means that as
business requirements change, the program can be
extended and enhanced without great expense.
Problem solving on computer
• To solve problems on the computer you need
to follow some steps
• Analyse the problem
• Follow the programming process
• Follow the Process necessary for the particular
language to execute
Solving Problems – Main steps
1. Problem definition
2. Algorithm design / Algorithm specification
3. Algorithm analysis
4. Implementation
5. Testing
6. [Maintenance]
Problem Definition
• What is the task to be accomplished?
– Calculate the average of the grades for a given
student
– Understand the talks given out by politicians and
translate them in Chinese
• What are the time / space / speed /
performance requirements ?
Algorithm Design & Specification
• Algorithm: Finite set of instructions that, if followed,
accomplishes a particular task.
• Describe: in natural language / pseudo-code / diagrams / etc.
• Criteria to follow:
– Input: Zero or more quantities (externally produced)
– Output: One or more quantities
– Definiteness: Clarity, precision of each instruction
– Finiteness: The algorithm has to stop after a finite (may be very large)
number of steps
– Effectiveness: Each instruction has to be basic enough and feasible
Implementation, testing & maintenance
• Implementation
– Decide on the programming language to use
• C, C++, Lisp, Java, Perl, Prolog, assembly, etc. , etc.
– Write clean, well documented code
• Test, test, test
• Integrate feedback from users, fix bugs, ensure compatibility
across different versions Maintenance
PROGRAMMING PROCESS
• Developing high quality programs involves more
than simply coding the solution.
• A 5 step process has been identified to aid in the
development of high quality programs
• This is known as the program development cycle.
These are:
– Problem definition/Program Specification
– Program Design/Solution/Algorithm Development
– Program Coding
– Program Testing
– Program Documentation and Maintenance
PROGRAMMING PROCESS
• Developing high quality programs involves more
than simply coding the solution.
• A 5 step process has been identified to aid in the
development of high quality programs
• This is known as the program development cycle.
These are:
– Problem definition/Program Specification
– Program Design/Solution/Algorithm Development
– Program Coding
– Program Testing
– Program Documentation and Maintenance
STEP ONE – PROBLEM DEFINITION
• The objectives, outputs, inputs, and processing
requirements are determined.
– That is determining exactly what it is that you want
the computer program to do.
• The programmer must:
– Understand the problem you are trying to solve by
identifying:
• The Required Output from the program
• The input required for the program
• The processing steps required to turn the input into
output
STEP TWO – PROGRAM DESIGN
• Begin the algorithm development or program design.
• Plan solution using structured programming techniques
(pseudocode and flowcharts).
• An algorithm is a prescribed set of well-defined
instructions for solving a problem in a finite number of
steps. (more later)
• Pseudocode and flowcharts are types of algorithm.
– Pseudocode (English-like statements to describe logic of the
program)
– Flowchart are graphic representation of the detailed
sequence of steps
STEP THREE - CODING
• Coding the program is actually writing the
program,
– translating the logic of the design into a
programming language.
• This is the “program code” that instructs the
computer what to do.
• It consists of choosing the appropriate
programming language and following its rules,
or syntax, exactly.
• More on Syntax soon
STEP FOUR – TEST & DEBUG
• Testing the program consists of desking-checking,
debugging the program of errors, and running real-
world data to make sure the program works.
• Desk-checking (proofreading) is simply reading
through, or checking the program to make sure that it
is free of errors and that the logic works.
• Debugging or to bug, means to detect, locate and
remove all errors in a computer program
• Programming errors are of two types: Syntax errors
(incorrect use of lang.) and Logic errors (incorrect
sequence or control).
STEP FIVE – DOCUMENTATION & MAINTAINANCE
• Documentation
– Consists of written description of what a program is and
how to use it.
– It is an important component of the programming process.
– The problem description, diagrams of the logic, pseudo
code and comments in the program are also most
important.
– It is prepared for users, computer operators and
programmers. Can be internal or external.
• Maintenance
– is any activity designed to keep programs in working
condition error-free and up to date.
SYNTAX
• Syntax are the rules governing the structure of
a programming language and its expressions.
• All programming languages possess a formal
syntax..
• If the syntax is wrong errors are generated and
the program will not work.
• If the syntax is ok, your program may suffer
from other types of errors – logic error,
semantic error, runtime error
Syntax errors
• Syntax errors are caused by typographical errors
and incorrect use of the programming language.
• In other words, it is the breaking of a rule
governing the structure of the programming
language used.
• For example typing IMPUT K instead of INPUT K
results in the computer failing to understand
what is meant.
Logic errors
• Logic errors are caused by incorrect use of
control structures. In other words, it occurs
when the programmer uses an incorrect
calculation or leaves out a procedure.
• Programs called diagnostic exist to check
program syntax (rules) and display syntax-
error message.
• Diagnostic Programs thus help identify and
solve problems.
Semantic Error
• A programming error that occur as a result of
some misunderstanding or effect of some
construct in programming
Algorithm Description
• How to describe algorithms independent of a programming
language
• Pseudo-Code = a description of an algorithm that is
– more structured than usual prose but
– less formal than a programming language
• (Or diagrams)
• Example: find the maximum element of an array.
Algorithm arrayMax(A, n):
Input: An array A storing n integers.
Output: The maximum element in A.
currentMax A[0]
for i 1 to n -1 do
if currentMax < A[i] then currentMax A[i]
return currentMax
Pseudo Code
• Expressions: use standard mathematical symbols
– use for assignment ( ? in C/C++)
– use = for the equality relationship (? in C/C++)
• Method Declarations: -Algorithm name(param1, param2)
• Programming Constructs:
– decision structures:if ... then ... [else ..]
– while-loops while ... do
– repeat-loops: repeat ... until ...
– for-loop: for ... do
– array indexing: A[i]
• Methods
– calls: object method(args)
– returns: return value
• Use comments
• Instructions have to be basic enough and feasible!
Notes on algorithm
• Write an algorithm (pseudocode) and draw a
flowchart that will output the largest number
in a list of numbers.
• An algorithm (pseudocode) for the above
example will read as follows:
– Read the first number and denote it by L;
– Read the second number and denote it by X;
– If X is larger than L, assign X to L;
– If all numbers have not been read, go to step (2);
– Write the value of L and stop.
THE STRUCTURE THEOREM
• Programming consists of any combination of
one or more of 3 basic control structures:
– Sequence
– Selection (including the CASE statement)
– Iteration ( Repetition))
SEQUENCE
• FLOWCHART
PSUEDO CODE
– DO THIS
DO THIS
– THEN THIS ONE
– THEN THAT ONE
DO THIS ONE
DO THAT ONE
SELECTION
• PSUEDO CODE • FLOWCHART
If condition is true
Then DO this Condition
Else
Do that
DO THIS DO THAT
ITERATION - REPETITION
• PSUEDOCODE • Flowchart
DO WHILE
Do while condition True Do this line
Then do this line Then do this
last line FALSE
TRUE
ALGEBRAIC & BASIC EXPRESSION
• A Computer system can handle arithmetic expressions
involving the five arithmetic operators + (addition), -
(subtraction), *(multiplication), /(division) and ^
(exponentiation). The hierarchy of operations is as follows:
(i) Exponentiation
(ii) Multiplication and division
(iii) Addition and subtraction
• Algebraic Expression Equivalent for the Computer
– 2A+B 2*A+B
– A(B+C) A*(B+C)
– B2-4AC B^2-4*A*C
COMPUTER-BASED EXPRESSIONS
• Assign the value N to Nstar
• Assign the string Str1 to the variable Tag
• Increase the value assigned to the variable
counter by 0.01
• Assigned the value of the expression I+J to
variable I
• Double the value assigned to the variable
Price
ASSIGNMENT STATEMENTS
• Write assignment statement for the algebraic
expression
• x2+y2
• (u+v)k-1
Exercise on conditional statements
• Write an appropriate if then statement or if then else
block for the following situation
• (a)Test the value of the variable sum. If sum exceeds
100, then adjust its value to 100
• (b) Test the value of the variable sum. If sum exceeds
100, then display its value, adjust its value to 100 and
assign the string “Maximum Exceeded” to the string
variable Flag.
• (c) Test the value of the variable hours. If hours is less
or equal to 40, assign 6.00 to pay and assign
“Regular” to string variable Status. If pay exceeds 40,
assign 9.50 to pay and assign “Overtime” to Status.
FLOW CONTROLS
FOR STATEMENT - SYNTAX
(1) FOR variable=initial value TO final value STEP increment
Statement …
NEXT variable
e.g. FOR K=1 TO 10 STEP 4
K =K*2
NEXT K
(2) FOR variable=final value TO initial value STEP decrement
Statement …
NEXT variable
e.g. FOR K=10 TO 2 STEP -4
K =K*2
NEXT K
IF THEN .. STATEMENT
• IF…THEN…. Flavour - SYNTAX
• IF condition is true THEN action
– IF Age = 40 THEN salary = 2* Age
• IF condition is true THEN action1, action2
– IF Age = 40 THEN salary = 2*Age; Print salary
• IF condition is true THEN action1 ELSE action2
– IF Age > 40 THEN PRINT “above” ELSE PRINT “below”
• IF … THEN …. ELSE ……ENDIF
Selection control structure
• Simple Selection - IF-Then-EndIF
• Double Selection - If-Then-Else-EndIf
• Multiple Selection - Nested If’s or Case
statement.
• Can use Logical operators AND, OR, XOR and
NOT
Repetition Control Structure
• Looping (repetition) is used to execute a group
of instructions in a program repeatedly. Loops
can be executed:
• while a condition is true,
• until a condition is met, or
• a predetermined number of times.
• An endless loop is s loop that executes
continuously until a break is forced by the user.
Do while loop
• Do While Loop
• Pretest loop - a condition is tested before the
loop is entered.
• The controlling condition must be set to false
somewhere in the body of the loop to avoid
an endless loop.
• Written as:Do While Condition
Statements (indented)
Loop
FOR ….NEXT
• For Next Loop
• Pretest loop, also known as count-controlled
loop
• A counter variable is used to control the
number of times the loop is executed.
• Written as:FOR counter = a To z [Step y]
Statements (indented)
NEXT counter
CHECKLIST
• HOW MANY ALGORITHM HAVE YOU WRITTEN
TO DATE?
• HOW MANY ALGEBRAIC EXPRESSION HAVE
YOU CONVERTED TO DATE?
• HOW MANY FLOWCHART HAVE YOU DRAWN
TO DATE?
• HOW MANY EXPRESSION HAVE YOU
EVALUATED CORRECTLY TO DATE?
• GET HELP BUT DO IT YOURSELF MORE