Chapter 1 Intro HF

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

CHAPTER 1

Introduction to Computer
Programs
Objectives
To introduce the elements of a computer
program
To explain the importance of programming
To describe good programming styles
To explain the relationships between
compilers, interpreters and programs
Contents

Basic Concept
Program Development Process
Program Control Structure

September 1, 2019 CSC425 : INTRODUCTION TO COMPUTER 3


PROGRAMMING
Basic Concept
 Computer Programming
 Program
 Programmer
 Programming Language
 Compiler
 Interpreter
 Interactive Programming Environment
 Algorithm

September 1, 2019 CSC425 : INTRODUCTION TO COMPUTER 4


PROGRAMMING
Basic Concept

Reasons for studying Concepts of Programming Languages :

1. 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.

2. Increased ability to automate process


 Programs are built mainly so that simple, or even complicated
processes to be executed automatically.

September 1, 2019 CSC425 : INTRODUCTION TO COMPUTER 5


PROGRAMMING
Elements of a computer program

Introduction to computer

 Computer is an electronic device that


accept input, processes data, store
data and generate the output
Elements of a computer program (Cont…)

Basic components of a computer systems

o Hardware
o Device that processes data to create information (ex:
input, output, storage and processing devices)
o Software
o Step-by-step instructions that tell the computer how to do
its work.
o It is also called a program.
o It’s purpose to convert data to useful information
Elements of a computer program (Cont…)

Basic components of a computer systems (Cont…)

SOFTWARE

SYSTEM SOFTWARE
• provides an environment for user
to execute the application software. APPLICATION SOFTWARE
•Enables the application software
to interact with computer • end user software.
hardware. •A program that performs a
•Eg. Operating System common task to the user.
•Eg. Word, Excel.
Elements of a computer program (Cont…)
Computer Program

o Program is a set of instructions that the computer needs to


follow to process the data into information.

o Programming Language contains set of instructions, data


and rules that are used to construct a program. Ex: C++, C,
VB

o Programming is the act of designing/writing and


implementing computer programs using the programming
languages.

o Programmer is a person who writes a computer program.


Basic Concept

Computer program
Hanly [2001], “List of instructions that direct the
computer to transform information from one
form to another.”
Information refers to the contains of specific
memory location.
It is written using programming language.

September 1, 2019 CSC425 : INTRODUCTION TO COMPUTER 10


PROGRAMMING
Basic Concept

Computer program
Turn data (input) into useful information
(output).
Program uses variables to store data

Input Program Output

Storage

September 1, 2019 CSC425 : INTRODUCTION TO COMPUTER 11


PROGRAMMING
Elements of a computer program (Cont…)

STORAGE
•Store the data or information for
future use (permanently or
INPUT temporarily)
•E.g. Hard disk, memory
•Whatever data that is PROCESSING
inserted into a computer
using input devices •Convert input data into
•E.g. Mouse, keyboard information.
•E.g. CPU OUTPUT
•Generate the useful
information using output
devices
•E.g. Monitor, printer
The Importance of Programming

o Easy to express tasks from a particular


problem domain.
o Allow user to instruct computer to do a
specific task in a certain instances.
o A skill required for a computer scientist
and software engineering.
o A skill required to create a successful
computer program.
The Importance of Good Programs

 Reliability of Output
Good program should produce correct output
During testing phase different set of input data is used to
ensure the reliability of output
 Program’s Efficiency
Good program should be reliable and efficient in the
sense that it is produces no errors during execution
process
Program must achieve its purpose so that the final result
can be trusted
Use of pseudocode or flowchart to outline the program
The Importance of Good Programs
(Cont…)
 Interactivity
Interaction between user and the program is well
defined
Interactivity is important so that the user knows the
processing status
User-friendly programs allow user to respond to the
instructions correctly and allow them key in valid input

 Program readability
Readability is concerned with how other person views
one’s program
Use of indention and comment increase the level of
readability
The Importance of Good Programs
(Cont…)
o Spacing
 Each instruction begins with new line
o Indent
Each instruction has indention depends on its
structure
Makes the structure of the program clearer and
easy to read
A statement within a statement should be
indented to show the user which statement is
subordinate of the other
The Importance of Good Programs
(Cont…)
 Comments
Help a human reader to understand the
program
Can be placed anywhere within a program
Will not be executed by compiler
C++ supports two types of comments
Line Comment
Block Comment
The Importance of Good Programs
(Cont…)
 Comments (Cont…)
Line comment
Begin with two slashes (//)
Ex: //This is Line comment
Block Comment
Begin with the symbol /* and end with the symbol */
Use for statements that span across two or more
lines
Ex: /* This program calculates the salary of
employees */
Program Errors

Also known as bugs


Can be divided into three
Compile-Time and Run-Time errors
Syntax errors
Logic errors
Program Errors (Cont…)

Compile-Time and Run-Time errors


Compile-Time error detect by compiler
Run-Time error is detected when the program is
being run
Program Errors (Cont…)
 Syntax errors
 Syntax error is an error in the structure or spelling of a
statement
 Can be detected by compiler

 Example
1 cout << “/n There are five syntax errors”
2 cot >> “Correct Them;

Invalid use of backslash (/) in Line 1


Missing semicolon (;) in Line 1
Keyword cout is misspelled in Line 2
Invalid use of symbol (>>) in Line 2
Missing a closing quote( ” ) in Line 2
Program Errors (Cont…)
Logic errors
Refer to unexpected or unintentional errors
resulted from some flaw in the program’s logic
Compiler cannot detect logic error
Some indication of logic errors
No output
Unappealing or misaligned output
Incorrect numerical results
Premature program termination
Program Errors (Cont…)
Logic errors (Cont…)
Example

1 int radius = 4.562, num = -4, count = 0;


2 float total = 10.0, average;
3 average = total/count;
4 cout << “Average is “;
5 cout << “\n the square root of ” << num << “ is ” << sgrt(num);

Assigning invalid values to variable of type integer in Line 1


Attempting to divide by zero in Line 3
Missing numerical output in Line 4
Taking the square root of a negative number in Line 5
Program Tracing

Writing down each variable as it is


encountered in the program and listing the
value that should be stored in the variable
Generation of Programming
Languages
o 1st generation (low-level language)
o Machine Language is the basic language of the computer,
representing data as 1’s and 0’s.
o 2nd generation
o Assembly language (low-level language) that allows a
computer user to write a program using simple word
instead of numbers.
o 3rd generation
o High-level language resembles some human language
(English) – eg. COBOL, FORTRAN, BASIC.
o Procedural languages.
o 4th generation
o Very high-level language, much more user-oriented and
allow users to develop programs with fewer commands
compared with procedural languages – eg. SQL, NOMAD
o 5th generation
o Natural Language uses human language to give people a
more natural connection with computers.
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 STA A

September 1, 2019 CSC425 : INTRODUCTION TO COMPUTER 26


PROGRAMMING
Basic Concept

 Type of High-level languages


Language Application Area Origin Name
FORTRAN Scientific Programming Formula Translation

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 objects and object Incremental modification of C (++ is


oriented programming the C incremental operator)

Java Supports Web Programming Originally name “Oak”

September 1, 2019 CSC425 : INTRODUCTION TO COMPUTER 27


PROGRAMMING
Language Translator

o a program that accepts a human-readable


source statements and produces machine-
readable instructions. There are
Assembler, Compiler, Interpreter.
Program Translator (Cont…)

SOURCE CODE • a program is a plain text written


using any text editor

INTERPRETER
ASSEMBLER COMPILER
• a program that • a program that translates • a program that translates
translates the assembly high-level language into source code of high-level
language program into machine language. language into machine
machine language. •Translate the source code language.
once and saves to be reused. •Each instruction is
•Requires less memory and interpreted into machine
runs faster. language.
•Eg. PASCAL, C++, COBOL •Program is easier to develop.
•Eg. BASIC.
Basic Concept

Compilation Process

Source File Compiler Object File

(Input) (Output)

September 1, 2019 CSC425 : INTRODUCTION TO COMPUTER 30


PROGRAMMING
Compiling a C++ Program

Using C++ Editor to produce the Source code


source code

Using C++ Compiler to compile compiler


the source code

The compiled process produces Object code


object code

Link the object code with linker


additional codes

Produce result Executable


code
Structured Programming VS Object
Oriented Programming
 Structured Programming
 Is a disciplined approach to writing programs that are clearer
than unstructured programs, easier to test, debug and modify.
 Top-down approach to writing programs that breaks programs
in modular forms and users standard logic tolls called control
structures (Sequential, Selection, Case, Iteration).
 Object Oriented Programming
 Is a disciplined approach to programming that results in
program that are easier to read, understand and less likely to
contain errors.
 Data and instructions for processing the data are combined
into self-sufficient “object” that can be used in other program.
Principles of Structured Programming
 Use Standard logic tools (Control structures)
:-
Sequential = one program statement follows another in
sequence order.
Selection = represents a choice, offers two paths to
follow when decisions must be made by a program.
Case = variation on selection. More than a single yes-
or-no decision.
Iteration = process may be repeated as long as a
certain condition remains true.
End of Chapter 1

You might also like