INTRODUCTION TO
COMPUTER PROGRAMMING
UNIT I
COMSCI 1101 - COMPUTER PROGRAMMING I
Learning Outcomes
Define what a program is;
Identify the main computer components responsible
for program execution;
Enumerate the basic processes of program execution;
Explain the necessity to translate a program written
in a higher-level programming language into machine
language;
Discuss the history of the C programming language;
and
Enumerate the basic steps of problem solving.
COMSCI 1101 - COMPUTER PROGRAMMING I
What is a Programming?
Programming
It is the process of writing instructions for
computer
It become a huge catalyst of great technological
developments in the world.
Useful and necessary technologies such as
software, computers, web applications, smart
phone application, robotics, digital devices etc.
are results of programming.
COMSCI 1101 - COMPUTER PROGRAMMING I
What is a Program?
Program
It is a sequence of instructions, written to
perform a specified task through a computer.
A computer needs a program in order to function.
A computer accepts data using an input device
and its processor process it according to a
program.
COMSCI 1101 - COMPUTER PROGRAMMING I
Basic Components of a Computer
Two Major Components
Hardware are the tangible part of a computer
which is composed of electronic and mechanical
parts.
Software are the intangible part of a computer
that is consisting of computer programs and
related data.
COMSCI 1101 - COMPUTER PROGRAMMING I
Basic Components of a Computer
Hardware
Components of the Hardware
Central Processing Unit (CPU)
Memory (storage)
Input devices
Output devices
COMSCI 1101 - COMPUTER PROGRAMMING I
Basic Components of a Computer
Central Processing Unit (CPU)
It is the brain of the computer
It does the fundamental computing/data processing within
the system
Composed of two major parts: Control Unit (CU) and
Arithmetic and Logic Unit (ALU)
Computing performance of the CPU mainly depends on the
design of it.
COMSCI 1101 - COMPUTER PROGRAMMING I
Basic Components of a Computer
Memory
It is where data and instructions needed by the CPU to do its
required tasks can be found.
It is divided into several storage locations which have
corresponding addresses.
Main memory is used to hold programs and data, which the
processor is actively working with. It is also called Random
Access Memory (RAM). It is considered as volatile storage
since it stores data in the form of electrical pulses.
Secondary Memory is used to hold programs and data for
long term use.
COMSCI 1101 - COMPUTER PROGRAMMING I
Basic Components of a Computer
Input Devices and Output Devices
These devices allows the computer system to interact with
the outside world by moving data into and out of the
system.
Examples of input devices are keyboard, mouse and
microphone.
Examples of output devices are monitor, printer and speaker.
COMSCI 1101 - COMPUTER PROGRAMMING I
Basic Components of a Computer
Software
Types of Computer Software
System Software
Application Software
Compilers
COMSCI 1101 - COMPUTER PROGRAMMING I
Basic Components of a Computer
System Software
Programs that are needed to keep all the hardware and
software running smoothly.
It controls the operations of the computer hardware.
Computer hardware needs the system software to be able to
function properly and manage its resources.
COMSCI 1101 - COMPUTER PROGRAMMING I
Basic Components of a Computer
Application Software
These are programs that people use to get their work done.
COMSCI 1101 - COMPUTER PROGRAMMING I
Basic Components of a Computer
Compilers
The computer only understand machine language. Machine
language is in the form of 1’s and 0’s.
It translate a language that can be understand by the
computer.
COMSCI 1101 - COMPUTER PROGRAMMING I
What is a Programming Language?
Programming Language
It is a standardized communications technique for
expressing instructions to a computer.
It enables programmer to precisely specify what
data a computer will act upon, how these data
will be stored/transmitted and what actions to
take under various circumstances.
There are different programming languages that
can be used to create programs.
COMSCI 1101 - COMPUTER PROGRAMMING I
What is a Programming Language?
Categories of Programming Language
Low-Level Assemby Language
High-Level Programming Languages
COMSCI 1101 - COMPUTER PROGRAMMING I
Syntax, Sematics and Pragmatics
Syntax
It is the form by which a code is written
Semantic
It is the meaning given to the various program
construct.
Pragmatic
It refers to the history and some implementation
methodology specific to the language.
COMSCI 1101 - COMPUTER PROGRAMMING I
C-Based Language
C++ includes all the features of C, but adds
classes and other features to support object-
oriented programming.
Java is based on C++ and therefore inherits many
C features.
C# is a more recent language derived from C++
and Java.
Perl has adopted many of the features of C.
COMSCI 1101 - COMPUTER PROGRAMMING I
The Programming Process
Basic steps in solving problem.
1. Problem Definition
“ Create a program that will determine the number of
times a name occurs in a list”
2. Problem Analysis
Determining the input and output.
Example problem:
Determine the sum of two integer.
Input to the program:
Two numbers, num1 and num2
Output of the program:
The sum of two numbers, sum
COMSCI 1101 - COMPUTER PROGRAMMING I
The Programming Process
3. Algorithm Design and Representation
Algorithm – a clear and unambiguous specification of the
steps needed to solve a problem.
a. Expressing the solution through pseudocode.
b. Expressing the solution through flowchart.
COMSCI 1101 - COMPUTER PROGRAMMING I
The Programming Process
Coding and Debugging
Debugging – adding some fixes to the program in case of
errors/bugs.
Two types of errors:
Compile-Time error – occur if there is a syntax error in the
code.
Run-Time error – appears after the program runs.
Fatal – terminates program execution immediately.
Non-Fatal – allows program to run but produces incorrect
result.
COMSCI 1101 - COMPUTER PROGRAMMING I
Programming in C
C Program is composed of one or more source file. A source file is a file
containing the source code of a program. Each source file contains one or
more functions and possibly references to one or more header file.
Main function
One and only one in entire program.
Start function in a program.
Header file
Provided by the system or created by the programmer.
ASCII file that contains functions, variable declarations, definitions
COMSCI 1101 - COMPUTER PROGRAMMING I
FUNCTIONS, STATEMENTS, and EXPRESSIONS
Function is an independent section of program code that performs a
certain task and has been assigned a name.
The program can send information, called arguments.
There are two types of functions in C:
library functions which are part of the C compiler package
user-defined functions which the programmer creates.
Function composed of statements.
A statement is any valid expression (usually an assignment or a function call)
that is immediately followed by a semicolon.
COMSCI 1101 - COMPUTER PROGRAMMING I
FUNCTIONS, STATEMENTS, and EXPRESSIONS
General composition of functions, statements, and expressions.
COMSCI 1101 - COMPUTER PROGRAMMING I
COMPILE, LINK, and EXECUTE
The basic stages of a C program: source code, object code, executable code.
COMSCI 1101 - COMPUTER PROGRAMMING I
THE PREPROCESSOR
Identified by the # sign
first non-blank character on the line
# - recognizes the statement as a directive - locates the system header
file - copies it into the source file automatically.
#include - request inclusion of header file
#define - assigning symbolic names to program constants
COMSCI 1101 - COMPUTER PROGRAMMING I
COMMENTS
notes written to a code for documentation purposes
not part of the program
do not affect the flow of the program
Types of Comments:
/* */ - C-style comments for multiline comments
/* this is an example of a
C style or multiline comments */
// - C++ style comments for single line comments
// This is a C++ style or single line comments
COMSCI 1101 - COMPUTER PROGRAMMING I
PROGRAM STATEMENTS AND BLOCKS
STATEMENT is one or more lines of code terminated by a semicolon.
printf(“Hello world!”);
sum = num1+num2;
BLOCK is one or more statements bounded by an opening and closing
curly braces that groups the statements as one unit.
int main( ) {
printf ("Hello");
printf ("world!");
}
return 0;
COMSCI 1101 - COMPUTER PROGRAMMING I
CODING GUIDELINES
1. In creating blocks, you can place the opening curly brace in line with the
statement,like for example,
int main( ){
or you can place the curly brace on the next line, like,
int main( )
{
2. You should indent the next statements after the start of a block, for
example,
int main( ){
printf ("Hello");
printf ("world!");
}
COMSCI 1101 - COMPUTER PROGRAMMING I
IDENTIFIERS
represent names of variables, functions, arrays, structures, etc.
e.g. addValues, firstName, x, arr, number_1
case-sensitive: x is not the same as X
alphanumeric characters ( a ~ z , A~Z , 0~9 ) and underscore ( _ )
Should not start with a number
Should not use the C keywords like void, printf, goto, while, etc.
no rule for the length of an identifier.
COMSCI 1101 - COMPUTER PROGRAMMING I
CODING GUIDELINES
1. For names of functions and variables, the first letter of the word should
start with a small letter. For example:
firstNumber, thisIsAnExampleOfFunctionName
2. In case of multi-word identifiers, use capital letters to indicate the start
of the word except the first word. For example:
charArray, fileNumber, lastName
3. Avoid using underscores at the start of the identifier such as:
_read or _write.
COMSCI 1101 - COMPUTER PROGRAMMING I
KEYWORDS
Also called as “reserved words”, which means that it has a special
meaning in C language.
If one of these words must be used as identifiers, the case of one or more
letters must be changed in order to make it distinct from the reserved
word.
auto double int struct
break else long switch
case enum register typedef
const extern return union
char float short unsigned
continue for signed volatile
default goto sizeof void
do if static while
COMSCI 1101 - COMPUTER PROGRAMMING I
LITERALS
INTEGER LITERALS
Integer literals come in different formats: decimal (base 10),
hexadecimal (base 16), and octal (base 8).
also known as “whole numbers”
FLOATING POINT LITERALS
decimals with fractional parts
CHARACTER LITERALS
an alphabet, a single digit or a single special symbol enclosed within
single quote delimiters.
STRING LITERALS
multiple characters and are enclosed by double quotes
COMSCI 1101 - COMPUTER PROGRAMMING I
Thank YOU for listening…
Presented by: Gemlyn S. Inocencio
COMSCI 1101 - COMPUTER PROGRAMMING I