CHAPTER 5 PROGRAMMING 1.0 BASIC PROGRAMMING CONCEPTS 1.1 Definition of program and programming language A computer program is a series of step by step instructions telling the computer what to do. A programming language is a set of words (reserved words such as Dim, If, Then, For, Next) symbols (=,*,&) and codes (set of rules) that enables humans to communicate with computers. Programming languages are used to write computer program or develop software.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
CHAPTER 5PROGRAMMING
1.0 BASIC PROGRAMMING CONCEPTS
1.1 Definition of program and programming language A computer program is a series of step by step instructions telling the computer what to do. A programming language is a set of words (reserved words such as Dim, If, Then, For, Next) symbols (=,*,&) and codes (set of rules) that enables humans to communicate with computers. Programming languages are used to write computer program or develop software.Examples of programming languages are COBOL, Java, Javascript, HTML, Visual Basic 6.0, Ada, C, C++, C#, Pascal, Ruby, Perl, BASIC,Prolog and Smalltalk.
1.2 Levels and generation of programming laguages
LOW LEVEL PROGRAMMING LANGUAGES
Low-level programming languages designed for a particular complete and reflecting its internal machine code; low-level languages are therefore often described as machine-oriented languages that closer to the hardware.
Assembly language is the human-readable notation for the machine language used to control specific computer operations.
An assembly language programmer writes instructions using symbolic instruction codes that are meaningful abbreviations or mnemonics.
With this language, a programmer write abbreviations such as ADD for addition, CMP for compare, MUL for multiply.
An assembler is a program that translates assembly language into machine language.
Assembly Language
1.2 Levels and generation of programming laguages
HIGH LEVEL PROGRAMMING LANGUAGES
High-level programming languages allow the specification of a problem solution in terms closer to those used by human beings. These languages were designed to make programming far easier, less error-prone and to remove the programmer from having to know the details of the internal structure of a particular computer. These high-level languages were much closer to human language.
The third generation of programming language 3GL or procedural language uses a series of English-like words that are closer to human language to write instructions.
High-level programming languages make complex programming simpler and easier to read, write and maintain.
Program written in a high level programming language must be translated into machine language by a compiler or interpreter.
BASICLOGO
SmallTalk
CC++
PascalJava
High-level programming language
Generation Explanation ExamplesFourth Generation
The fourth generation programming language or non-procedural language, often abbreviated as 4GL, enables users to access data in a database. SQL can be used to query data.
A very high-level programming language is usually limited to a very specific application that might use syntax which is never used in other programming languages.
SQLNOMADFOCUS
Intellect
High-level programming language
Generation Explanation ExamplesFifthGeneration
The fifth generation programming language or visual programming language is also known as natural language.
Provides a visual or graphical interface, called a visual programming environment, for creating source codes.
Fifth generation programming language allows people to interact with without needing any specialize knowledge.
PrologMercury
PROGRAMMING LANGUAGE APPROACHES
Object-Oriented Approach Structured Approach
(a) Object-Oriented approach is a computer programming techniques based on the concept of an “object” that combine both data and the function into a single unit. Programming approaches may includes features such as encapsulation, polymorphism and inheritance.Examples of OOP : C++, Java, Visual Basic 2005, C#
(b) Structured approach is computer programming technique in which the program is divided into modules like function, subroutine and procedure.Examples: Pascal, C
1.3 PROGRAMMING LANGUAGE APPROACHES
DIFFERENCES BETWEEN OOP AND STRUCTURED APPROACHSTRUCTURED APPROACH
Differences OBJECT-ORIENTED APPROACH
Structured programming approached uses a top down design model
Uses Object-Oriented approach uses object
The programmer divides programming problem into module like function
Emphasize The programmer packages the data and the function into a single unit, an object
Medium programming language
Suitable for Large programming language
1.4 TRANSLATION METHOD OF PROGRAMMING USINGASSEMBLER, INTERPRETER & COMPILER
TRANSLATOR
Assembler Interpreter Compiler
(a) An Assembler is a computer program that translates assembly language into machine language.
(b) An Interpreter is a computer program that translates a high-level language into machine language. Interpreter translates programming statement of a program into machine language; no object code is saved and then executes it immediately, line by line. (Interpreted code generally run more slowly)
(c) A Compiler is a computer program that translates a high-level language into machine language. Compiler translates entire programming statement of a program into machine language, save it in an object code file and then executes it later.
DIFFERENCES BETWEEN INTERPRETER AND COMPILERINTERPRETER Differences COMPILER
Translate programming statement line by line and execute it immediately
Method Translate entire programming statement and execute it later.
No object code is saved Object code Store machine language as object code on the disk
Interpreted code run slower
Running time Compiled code run faster
(a) An Assembler is a computer program that translates assembly language into machine language.
(b) An Interpreter is a computer program that translates a high-level language into machine language. Interpreter translates programming statement of a program into machine language; no object code is saved and then executes it immediately, line by line. (Interpreted code generally run more slowly)
(c) A Compiler is a computer program that translates a high-level language into machine language. Compiler translates entire programming statement of a program into machine language, save it in an object code file and then executes it later.
Study Case: Microsoft Visual Basic
Visual Basic programming language uses both aninterpreter and a compiler. The programmer can use the interpreter during program development. When the program is complete and error free, the programmer can compile the program so it runs faster when it is placed into production for users to execute.
1.5 BASIC ELEMENTS IN PROGRAMMING
CONSTANT differences VARIABLESConstant retain their value during program execution
Characteristics Variables can change their value during program execution
const PI = 3.142Const GRAVITY = 9.8Const WAGE = 5.5
examples Dim Name as StringDim Score as IntegerDim Mark as Integer
(A) CONSTANT(B) VARIABLE
1.6 DATA TYPES(a) Boolean (b) Integer (c) Double
(d) String (e) DateData type determines the type of data a variable can store,
for example a number or a character.DATA TYPES EXPLANATION
Integer (Number)
Integer data type contains any whole number value that does not have any frictional partExamples: Age, Mark and Temperature.17, 85 and -5
Double (Number)
Double data type contains any decimal number value that has a fractional part.Examples: Wages, Fees and WeightRM 3500.45, RM 33.00, 33.3 kg
DATA TYPES EXPLANATIONString (Text)
String data type contains a sequence of character.Examples: Name, Address and IC Number.Ahmad, Jalan 2 Taman Permata, 850306-02-5264
Boolean(Logical Value)
Boolean data type contains either a true or false value.Examples: Paid (school fee), Result, Passed and Married (Marital Status)If Not Paid then msgbox(“Please pay immediately)If Passed then msgbox(“Good”)
Date Date data type contains date and time value.Examples: DOB, Date of payment, Time7/8/91, 2-Sept-2007, 12:24 AM
1.7 MATHEMATICAL AND LOGICAL OPERATORS
Operator is a symbol or notation that tells a computer to perform certain actions or operation.
MATHEMATICAL OPERATOR
MEANING
+ Plus- Minus* Multiply/ Divide
LOGICAL OPERATOR
MEANING
And And OperatorOr Or Operator
Not Not Operator
COMPARISON OPERATOR
MEANING
> Greater Than< Less Than= Equal
<> Not<= Less Than or Equal
Comparison Operator or Relational Operator
Mathematical Differences LogicalMathematical operators are notations that tell the computer to perform mathematical operations such as addition, subtraction, multiplication and division
Function
Logical operators are notations that tell the computer to perform logical operations such check the status of two Boolean values
+, -, *, / Symbol And, Or, Not
DIFFERENCES BETWEEN MATHEMATICAL AND LOGICAL OPERATOR
1.8 SEQUENCE CONTROL STRUCTURE AND SELECTION CONTROL STRUCTURE
SEQUENCE CONTROL STRUCTURE
SELECTION CONTROL STRUCTURE
A sequence control structure executes statement one by one in linear order
A selection control structure will execute statements based on certain conditions
Programmer uses sequence control structure when he/she want to execute code line by line
Programmer uses selection control structure when he/she want to implement decision making process in the program
The program does not use the decision symbol
The program has to use the decision symbol
SEQUENCE CONTROL STRUCTURE
SELECTION CONTROL STRUCTURE
Sequence Control Structure Selection Control Structure
Action 1
Action 2
Action 3
Condition
Action 2 Action 1
False
True
SEQUENCE CONTROL STRUCTURE
SELECTION CONTROL STRUCTURE
Example 1: Dim marks as integer Input “Please enter marks”; markExample 2: Dim no1 as integer Dim no2 as integer no = 4 no = 6 Print “Addition” Print “Number 1”; no1 Print “Number 2”; no2 Input “Your answer”; answer
Example 1: If marks > 50 then Print “Passed” else Print “Failed” End IfExample 2: If answer = no1 + no2 Then Print “Excellent” Else Print “Please, Try Again” End if
1.9 FLOW CHART
SYMBOLS NAMES EXPLANATIONTerminal
Begin or EndUse to show the beginning or end of a program
Flowline Use to connect symbols and indicate the sequences of operation
Input or output(Print/Display)
Use to show either an input operation(e.g an input from the user) or an output operation (e.g print or display messages)
1.9 FLOW CHART
SYMBOLS NAMES EXPLANATIONProcess
(Formula)Use to show a process to be carried out (e.g. calculation)
Decision(If-Then-Else)
Use to shows a decision (or choice) to be made. The program should continue along one of two routes (e.g. if…then…else)
1.9 FLOW CHARTDraw a flow chart to show how a program calculates the area of a circle. Begin
Get length, base, width
Volume of a cuboid = length*base*width
Print volume of a cuboid
End
Draw a flow chart to make a decision. If student has more than 50 marks then he will pass. Begin
Print “Failed”
Mark > 50
Get mark
Print “Pass”
End
False
True
1.10 PSEUDO CODES•Pseudo codes is text only sentences that describe the logic and program flow of a computer program.•Pseudo code assembles plain English.•It usually does not have any specific programming language syntax and grammar.•Pseudo code is directly linked to the computer codes because each pseudo code statement can often be converted into the programming language virtually line by line.•There are no set of rules for writing pseudo code.•A programmer can have his or her personalized pseudo code.•He or she must use consistent language and syntax in the pseudo code, so that he or she can understand it at a later stage.
Write a pseudo code to show how a program calculates the area of a circle.
Pseudo Code Begin
Get radiusCalculate area of a circle = ½ * PI * radius 2
Print area of circle End
Draw a flow chart to make a decision. If student has more than 50 marks then he will pass.
Pseudo Code Begin
Get marksIf mark > 50 Then Print “Passed”Else Print “Failed” End If
End
2.0 PROGRAMME DEVELOPMENT PHASES
5 MAIN PHASES IN PROGRAM DEVELOPMENT
• Problem Analysis
• Program Design
• Coding
• Testing & Debugging
• Documentation
PROBLEM ANALYSIS
1. Identify input, processing, output and data component
2. Refer to book and website3. Ask expert4. Meet with system analyst and users
PROGRAM DESIGN
1. Divide all program activities into program modules
2. Create solution algorithm for each program modules (logic structure)
3. Describe the flow or steps of a program with English statement. (pseudo code)
4. Create a pictorial diagram of the logical sequence of a program (flowchart)
5. Design/Draw user interface
CODING
1. Coding a program involves translating the solution algorithm into programming language usually on paper.
2. Typing the code into the computer using programming language such as Pascal, C++ and Visual Basic.
TESTING AND DEBUGGING
Several methods have been devised for finding and removing logic error and syntax error.1. Manually testing with sample data2. The program is run through a computer,
using a translator program. Before the program will run, it must be free of syntax errors. Such errors will be identified by the translator.
3. Testing by a select group of potential users. Potential users try out the program and provide feedback.
DOCUMENTATION
Documentation is important for people who may involve with the program in the future.
Programmers: As time passes, even the creator of the original program may not remember much about it. Other programmers wishing to update and modify it (program maintenance)
DOCUMENTATION
Activity:1. Produce a description of the program, layouts of the
input and output records and a program listing.2. Produce a problem definition, flow chart and pseudo
code.3. Write comments with the program code(internal
documentation). These comments explain the program’s purpose of the code statements within the program.
4. Write a document for a novice users that explains how to use or operate the program (write a user guide).