Introduction To Computer
Programming (CSC425)
Lecturer : Assoc Prof Ts Dr Nur Atiqah Sia Abdullah
Introduction
Topic 1
Contents
• Basic Concept
• Program Development Process
• Program Control Structure
Basic Concept
• Computer Programming
• Program
• Programmer
• Programming Language
• Compiler
• Interpreter
• Interactive Development Environment
• Algorithm
Basic Concept
Reasons for studying Concepts of Programming
Languages :
Increased capacity to express ideas and solve problems
Studying programming languages may increase the capacity and ability of
students to express their ideas in a formal, computational form.
Increased ability to automate process
Programs are built mainly so that simple, or even complicated processes to
be executed automatically.
Basic Concept
Computer programming
Craft of developing a computer program.
Require knowledge, skill and creativity
Both skill and knowledge in problem solving and
programming language.
•
Basic Concept
Computer program
From Hanly [2001], it is “list of instructions that
direct the computer to transform information from
one form to another.”
Another name for software
Is a set of detailed, step-by-step instructions
that directs the computer to do what you want
it to do.
Programs are written in a programming
language.
Basic Concept
Computer program
Turn data (input) into useful information
(output).
Program uses variables to store data
Input Program Output
Storage
Basic Concept
Programming Language
A set of rules, words and symbols are used
to write a computer program.
Provides a way of telling the computer what
operations to perform.
Programmer is a person who writes programs
by using programming language.
Basic Concept
Lower or higher depending on how close
they are to the language that the computer
uses.
Lower (0s and 1s)
Higher (more English-like)
Generation of Programming language
Machine language
Assembly language
High Level language (Pascal, C, C++, Java etc)
Basic Concept
Machine Language
Binary number codes understood by a specific CPU.
Computer understands only binary numbers, 0s and
1s.
These represent the on and off electrical states of
the computer
Programs that are written in 0s and 1s is actually
machine language
Basic Concept
Assembly Language
Very low-level
Use mnemonic codes, abbreviations to replace 0s and 1s
More easier to understand
A for add, C for compare, MUL for multiply, STO for
storing information in memory
Assembler is a translation program that converts the
assembly language program into machine language
Basic Concept
• A Machine-language Program Fragment and
Its Assembly-Language Equivalent
Memory Address Machine-Language Assembly-Language
Instructions Instructions
00000000 00000000 CLA
00000001 00010101 ADD A
00000010 00010110 ADD B
00000011 00110101 STO A
Basic Concept
High-level languages is designed for specific types of
problems and used syntax that was familiar to people
Language Application Area Origin Name
FORTRAN Scientific Programming Formula Translation
Represents complex mathematical
formulas
COBOL Business Data Processing Common Business-
Oriented Language
Lisp Artificial Intelligent List processing
C System Programming Predecessor Language was named B
Prolog Artificial Intelligent Logic Programming
C++ Support OOP Incremental modification of C
Java Supports Web Programming Originally name “Oak”
Basic Concept
Compiler is a program that translates a
high-level language program (symbolic
statements) into machine language as a
complete unit.
Examples of compiler: C, C++, Pascal,
FORTRAN, Java, Ada and COBOL.
Basic Concept
Interpreter is a set of program that
executes the instructions as they were
translated.
Interpreter executes a program from its high-
level form.
Interpreter converts instructions into
machine language one at a time, in succession.
Examples of interpreter: BASIC and LISP
Basic Concept
Interactive Development Environment
(IDE) is a program that provides user with an
environment for editing, debugging and
compiling the source code the program
online.
Basic Concept
Source file
A file containing a program written in a high-level language; the
input for the compiler
Syntax
Grammar rules of programming language
Object file
File of machine-language instructions that is the output of a compiler
Algorithm
A precise step-by-step action to perform overall task of the program
Basic Concept
Compilation Process
Source File Compiler Object file
Input Output
Program Development Process
Computer programming is a process to
develop a computer program
Step 1. Problem analysis
Step 2. Program design
Step 3. Coding
Step 4. Testing
Step 5. Documentation
Program Development Process
Problem Analysis
A process of identifying the output, processes and input of a
program.
How to identify input?
Nouns and adjectives Keywords – accept, enter, receive, read
How to identify output?
Nouns and adjectives Keywords – print, display, produce
Define the storage (variable) and data type to hold data
Outline the process (Arithmetic or logic operation)
Input-Process-Output (IPO) chart as tool
Program Development Process
Problem Analysis
Evaluate the following problem statement and identify
the input and output.
Nation’s Air force has asked you to write a program to
label supersonic aircraft as military or civilian.Your
program is to be given the plane’s observed speed in
km/h and its estimated length in meters. For planes
traveling in excess of 1100km/h, you will label those
longer than 52 meters “civilian” and shorter aircraft as
“military”. For planes traveling at slower speeds, you will
issue an “aircraft type unknown” message.
Program Development Process
Problem Analysis
Evaluate the following problem statement and identify
the input and output.
Nation’s Air force has asked you to write a program to
label supersonic aircraft as military or civilian.Your
program is to be given the plane’s observed speed in
km/h and its estimated length in meters. For planes
traveling in excess of 1100km/h, you will label those
longer than 52 meters “civilian” and shorter aircraft as
“military”. For planes traveling at slower speeds, you will
issue an “aircraft type unknown” message.
Program Development Process
Input-Process-Output (IPO) chart
Example:
Input : speed, length
Process : validate the speed and length
Output : classification – civilian, military,
unknown type
Program Development Process
Input-Process-Output (IPO) chart
Program Development Process
Program Design
Developing an algorithm
Algorithm
is a precise step-by-step action to
perform overall task of the program
Tools used for Program Design are:
Flow Chart
Pseudo Code
Program Development Process
• Flow Chart is a pictorial representation of the
solution algorithm. Symbols are used to represent
actions and arrows indicate the sequence of
actions.
Process Decision Connector Start/Stop Input/Output Direction of flow
Program Development Process
Program Development Process
• Pseudocode is a non-standard English-like
language that lets you specify your algorithm.
• It allows you to focus on the program logic
without worrying about the detailed rules of
programming language.
Program Development Process
Desk-Check the algorithm
Speed Length Classification
1170 35 ?
1180 56 ?
900 66 ?
800 34 ?
30
Program Development Process
Coding
Implement the flowchart or pseudo code into specific
programming language rules (syntax)
Translate the algorithm into a formal programming
language.
Syntax or grammatical rules are important in coding
Identify the storage requirement
Use text editor to write your program
Compilation
Program Development Process
Error correction
Syntax Error occurs when a character or string incorrectly
placed in a command or instruction that causes a failure in
execution. It is an error in a program due to a code that does not
conform to order expected by the programming language.
Logic Error is a bug in a program that causes it to operate
incorrectly, but not to terminate abnormally (or crash). A logic
error produces unintended or undesired output or other
behavior, although it may not immediately be recognized as such.
Runtime Error is a program error that occurs while the
program is running. It might cause the memory leak (infinity loop)
or program crash (quits while running).
Program Development Process
Testing
Program must be freed from syntax error
Use a set of test data to validate the output.
Program must produce receive valid input and produce
correct output.
Program must handle invalid input efficiently.
Does the program accept out of range value?
Program Development Process
Testing and Debugging
System
Library
Source Compiler Object Linkage Load
Module Module Editor Module
Diagnostic
Message
Source
Program
Listing
Program Development Process
Documentation
User manual
Program description
Capability, limitation, user guide
Program Control Structure
There are SIX basic operation:
1. Accept Input through keyboard or files
2. Produce Output and displayed it on screen or file
3. Assign value to a storage
4. Perform arithmetic and logic
5. Make decision using selection statement
6. Repeat the same action
Program Control Structure
THREE types of control structure:
Sequential Structure
Selection Structure
Iteration Structure (Repetition or loop)
Program Control Structure
Sequential Structure
ALL statement(s) will be executed from top to bottom.
Statement can be an input/output statement or a processing statement.
Begin Begin
Read Number1,
Statement 1 Number2
Compute Total
Statement 2
Compute Average
Statement n
Print Total
and Average
End
End
Program Control Structure
Selection Structure
Provides alternative actions
Only one action will be executed
Three types:
One-way Selection
A set of statements will be executed if and only the condition is TRUE.
Two-way Selection
Either one of two set of statements will be executed if the condition is TRUE.
Multi-way Selection
Has more than one conditions and alternative set statements.
Program Control Structure
One-way Selection (Flow Chart)
Begin
Begin
Read Score
FALSE FALSE
Condition Score >= 50
TRUE
TRUE
Statement (s)
Status is “Passed”
End
End
One-way Selection (Example)
Program Control Structure
Two-way Selection (Flow Chart)
Begin Begin
Read Score
FALSE
Condition
FALSE
TRUE Score >= 50
Statement (s) Statement (s) TRUE
Status is “Passed” Status is “Failed”
End
End
Two-way Selection (Example)
Program Control Structure
Multi-way Selection (Flow Chart)
Begin
FALSE
Condition 1
TRUE FALSE
Condition 2
Statement (s)
TRUE FALSE
Condition n
Statement (s)
TRUE
Statement (s) Statement (s)
End
Program Control Structure
Multi-way Selection (Example)
Begin
FALSE
code == ‘M’
TRUE FALSE
code == ‘F’
gender is “Male”
TRUE
gender is “Female” Status is “Unknown”
End
Program Control Structure
Iteration or loops
Perform the same operation more than once
A set of statements will be executed more than once
Can be controlled either by counter or sentinel value.
Initialize counter
False
Test
True Exit loop
Statement(s)
update counter
Program Control Structure
The following flowchart illustrates the process of reading five (5) numbers
and calculate the total.
count = 0
False
count < 5
True
exit loop
Read a
number
Add number to total
Count + 1
Program Control Structure
Sentinel Controlled loop flowchart:
False
Test
Sentinel value
True
Statement(s)
Exit loop
Program Control Structure
The following flowchart illustrates the process of reading a series of
students’ scores and compute the total score. The process will stop
if the value of score read is equal to 999.
Read score
False
Score != 999
True Exit loop
Add score to total
Read Score
Self Exercise
1. Write a flow chart to calculate and display an average of three numbers
using sequential design
2. Rewrite the above design, determine whether the average is higher than
50 or not. Display the appropriate message for each case.
3. Rewrite problem 1 using iteration any iteration approach.
4. Write a flow chart to calculate an average score of 20 students.
5. Write a flow chart to calculate an average of a series positive numbers.
The process will stop if number entered has a negative value.
6. The cost to send a telegram to UK is RM15.50 for the first 15 letters
and RM0.50 for the subsequent letters. Draw a flowchart to calculate the
cost of sending the telegram.
References
Jeri R. hanly, Essential C++ for Engineers and Scientists, 2nd Edition,
Addison Wesley
J. Glenn Brookshear, Computer Science An Overview, 8th Edition, Pearson
Addison Wesley