Faculty of Computer Science and Information Technology
BIC 10204 Algorithm and Programming
CHAPTER 3
Basic Structure in
Programming
Basic Structure in
Chap 3
Programming
After this lesson, you should be able to understand
or explain about:
Understand and implement the basic
structure of computer programming.
Write a computer program using C
programming language.
Convert algorithm into computer
program.
Basic Structure in
Chap 3
Programming
Program Basic Syntax
Development of Programming
Environment
CHAPTER 3
Data Types
Variable
Declarations
Basic Structure in
Chap 3
Programming
Involves translating high-level language (programming
language such as C,C++, Java, PHP,Visual Basic, C#,etc.)
WHY? Because computers do NOT
understand high level language!
Translated to
00011101 01010101 11011111
Basic Structure in
Chap 3
Programming
Typical program development environment consist of
six phases to be executed.
Create/ Edit
Preprocess
Compile
Link
Load
Execute
Basic Structure in
Chap 3
Programming
Phase 1: Creating a Program
Programmer types or creates program in an editor.
Makes corrections if necessary.
Saves or stores program on disk such as C:\
or A:\ etc.
Editor?
Editor or text editor is a type
of program used for editing
plain text files.
Basic Structure in
Chap 3
Programming
Turbo C
editor
(free)
Basic Structure in
Chap 3
Programming
Vim
editor
(free)
Basic Structure in
Chap 3
Programming
Phase 2: Preprocessing
Programmer gives command to compile the program.
Preprocessor program executes automatically and process
the program code.
The prepocessor obeys commands from preprocessor
directives.
Preprocessing occurs before a program is compiled.
Basic Structure in
Chap 3
Programming
Phase 3: Compiling a Program
When compiled, compiler translates program into machine
language code and creates object code. Programmer click
The object code will be stored in disk. Compile
Dialog box in
Turbo C editor
shows
compiling
process.
Basic Structure in
Chap 3
Programming
Phase 3: Compiling a Program
The object code will be only created if the translation
process into machine code is successful.
Otherwise, if unsuccessful, error messages will be displayed
in the compiling dialogue box.
Programmer must fix all the errors before proceed to the
next phase.
The process of correcting errors is called debugging.
Basic Structure in
Chap 3
Programming
Phase 4: Linking
A linker links the object code with the libraries.
A linker will creates an executable file and stores it on disk
if the program compiles and links correctly.
A linker might name the executable file with .exe file
extension depending on type of programming language
used.
Basic Structure in
Chap 3
Programming
Phase 5: Loading
Before a program can be executed, the program must first
be placed in memory.
Loader takes the stored program from disk and puts in
memory.
Additional components from shared libraries that support
the program are also loaded.
Basic Structure in
Chap 3
Programming
Phase 6: Executing
CPU takes each instructions and executes it.
Results or output will be displayed.
Basic Structure in
Chap 3
Programming
Terms Description
Machine language Binary number codes understood by a specific
CPU.
High-level Machine-independent programming language
language that combines algebraic expressions and
English symbols.
Source file File containing a program written in a high-
level language; the input for compiler
Compiler Software that translates a high-level language
program into machine language.
Linker Software that combines object files and create
an executable machine language program.
Basic Structure in
Chap 3
Programming
Common Programming Errors
Error
(bugs)
Run-time
Errors
Syntax Errors
Logic Errors
Basic Structure in
Chap 3
Programming
Common Programming Errors
1 Syntax Error
Error occurred during compilation
normally due to syntax problem
Misplaced else.
Declaration syntax error
Undefined symbol ‘_main’ in module.
Statement missing in function main()
Basic Structure in
Chap 3
Programming
Common Programming Errors
2 Logic Error
Error occurred due to inappropriate
output.
Programming mistake.
Not detected during compilation.
Basic Structure in
Chap 3
Programming
Common Programming Errors
3 Run-time Error
Error occurred due to wrong user input.
User’s mistake.
System would either display alert message
or hang.
Basic Structure in
Chap 3
Programming
Computer Program
WHAT
HOW
UNDERSTAND THE
SYNTAX AND FORMAT
STRUCTURE OF
PROGRAMMING
Basic Structure in
Chap 3
Programming
C Basic Structure
Program block
C preprocessor directive
components:
1. Preprocessor
main function
directive
{
2. Program body
//Identifiers/Variables
3. Main function
//C statements
4. Identifiers/Variable
}
5. C statements
6. Comment
Basic Structure in
Chap 3
Programming
PICK AND MATCH
COMMENT
PREPROCESSOR
DIRECTIVE
C
STATEMENT PROGRAM
BLOCK MAIN
FUNCTION
IDENTIFIERS/
VARIABLE PROGRAM
BODY
Basic Structure in
Chap 3
Programming
Preprocessor Directive
Utility program which link files from compiler library to the
program code.
Must be included in the first line of a computer program.
Must be started with the symbol #, otherwise syntax errors
will be occurred.
Two types of common preprocessor directive: #include and
#define.
Basic Structure in
Chap 3
Programming
Preprocessor Directive
Format:
#include <header file> or #include “user defined files”
Example
#include <stdio.h>
#include <conio.h>
#include “jam.h”
Basic Structure in
Chap 3
Programming
Preprocessor Directive
Example:
Called from
#include <stdio.h> standard library
A directive to the C preprocessor
Lines beginning with # are processed by the preprocessor
before the program is compiled.
The above code line tells the preprocessor to include the
contents of stdio.h ( standard input/output header)
Basic Structure in
Chap 3
Programming
Consists of built-in functions
Standard
Library Functions contains standard instructions
Function will be called and linked to program
via header file
Header file List of functions
List of header printf(),
stdio.h file and its functiondll
scanf(),fflush(),
conio.h clrscr(),putch().getc().dll
math.h sqrt(),pow(), log(),dll
Basic Structure in
Chap 3
Programming
Contain functions defined by programmer.
User-defined
Library Developed by expert programmers.
Header file List of user-defined
functions
utama.h cetak(),baca(),papar(),dll
kira.h plus(),minus(), divide(),dll
Basic Structure in
Chap 3
Programming
Preprocessor Directive
Format:
#define “file name” or #define constant_name constant_value
Example
#define MAX 100
#define “jam.h”
Basic Structure in
Chap 3
Programming
Program Body
The part in which the program code will be started to
execute.
Consists of main function, C statements and identifiers.
Use { to start the program code and } to end the
program code.
Format:
main function
{ //identifiers
//C statements }
Basic Structure in
Chap 3
Programming
Main function
void main( )
{ …………..}
int main( ) main( )
{ {
return 0; return 0;
} Main function }
Basic Structure in
Chap 3
Programming
Write the most basic structures of
C programming.
#include <stdio.h>
void main()
{
}
Basic Structure in
Chap 3
Programming
C Statement
Instructions to be executed by computers
Every statements must be ended with semicolon
Function Declaration
statement statement
Control Types
Input/Output
statement statement
Compound
statement
Basic Structure in
Chap 3
Programming
Comment
Statement in program code that will be ignored by compiler
Differs in terms of colour : grey
To increase
program readability To document
a program
Function
As a future
To provide references
additional
information
Basic Structure in
Chap 3
Programming
Reserved Word
Standard/special word in standard library
Contain special meaning understood by compiler
Rules
Case –sensitive
Cannot be used as identifier
Must be written in
or variables
small case
Basic Structure in
Chap 3
Programming
Reserved Word
Example:
void int
Refer to the function that will not
The acronym for integer
return any value
case default switch break
for continue float double
return while if do int
Basic Structure in
Chap 3
Programming
Identifier
Representing particular name in programming
Store values to be used in programming
Refers to the storage in computer
Standard User-defined
identifier Type identifier
Basic Structure in
Chap 3
Programming
Identifier
Standard Special built-in words
identifier Referred as function name
which will called from C library
printf() scanf()
puts() gets()
Basic Structure in
Chap 3
Programming
Identifier
Name given to the declaration of
User-defined data to be used in program
Refer to the storage name
identifier Store data values/result/output
Constant Type Variable
Basic Structure in
Chap 3
Programming
Identifier
User-defined
identifier
RULES
Identifiers name can only consists of name, number and
underscore
Identifiers name cannot be started with numbers
Symbol cannot be used in identifier name
Cannot contains spaces between two identifiers name
Identifiers name should be unique
Identifiers is not case sensitive
Basic Structure in
Chap 3
Programming
Identifier
Valid identifiers
UThM DIT1064 Seven_eleven integer
WHY?
WHY?
Invalid identifiers
8Century BIT 1033 Two*four
‘Sixsense’ void
WHY?
WHY? WHY?
Basic Structure in
Chap 3
Programming
Identifier
Constant Name which used to store data value
Refer to name of one cell in computer
storage
Constants value is fixed
How to give name
to a constant Follow identifiers rules
value?
Basic Structure in
Chap 3
Programming
Declaration format: 1
const data_type const_name = const_value;
Constant
Reserved word const float pi = 3.142; Value
Basic Structure in
Chap 3
Programming
Declaration format: 2
#define const_name const_value;
#define pi 3.142;
Reserved word
Basic Structure in
Chap 3
Programming
Example of constant:
#define minimum 0;
#define MAX 100;
const int counter = 100;
const char alphabet = ‘J’;
const float value = 4.5;
Basic Structure in
Chap 3
Programming
Identifier
Name which used to store data/input
value
Variable
Refer to the name of one cell in computer
storage
Variable’s value can be modified/changed
during execution
Declaration Format:
data_type variable_name;
Basic Structure in
Chap 3
Programming
Declaration Example
Declaration of a variable number of
integer data type.
int number;
Declaration of a variable weight of
float weight; floating point data type.
char alphabet; Declaration of a variable alphabet
of character data type.
Basic Structure in
Chap 3
Programming
Variable/constant declaration example
//Variable and constant declration
#include <stdio.h>
int number; Variable declaration
float weight;
void main()
{
const float pi =3.142;
Constant declaration
int bilangan;
float berat;
char abjad;
Variable declaration
}
Basic Structure in
Chap 3
Programming
Variable and constant declaration example:
//Variable and constant declaration
#include <stdio.h>
const float pi=3.142;
void main()
{
int bilangan, bil, bilang;
float berat, kg;
char A;
}
Basic Structure in
Chap 3
Programming
Method to give/assign value to variable
Interactive Initialization
Input/enter data through Assign value to variable during
input devices declaration.
Use input statement (scanf/gets) Assign value to variable.
Use assign symbol, “=“
Basic Structure in
Chap 3
Programming
Assigning value to variables
#include <stdio.h>
void main()
{
Initialize a variable
int number = 10;
float weight;
weight = 60.00; Interactive
printf(“Enter the value of number :”);
scanf(“%d”,&number);
number = 50.00; Initialize a variable
}
Basic Structure in
Chap 3
Programming
Data Types
Represents types of data can be stored in computer.
Types of data to be stored and used in programming should be
informed to the compiler/system
Types
Integer Character
Floating
point
Basic Structure in
Chap 3
Programming
Data Types
Represents any round number with +/-
values.
Integer Divided into short and long integer.
Reserved word for integer – int
Valid until 5 places of integer number.
Example:
age is used to represent the age of students between
18 and 25 years old. The declaration for the
variable is as follow:
int age;
Basic Structure in
Chap 3
Programming
Data Types
Floating Represents any floating point numbers +/-
Reserved word– double /float
number
Example:
height is used to represent the student’s height
between 150 cm and 180 cm. The declaration for the
variable is as follow:
float height;
Basic Structure in
Chap 3
Programming
Data Types
Character Represents character data.
Reserved word – char
Example:
gender is used to represent the gender of a student.
The declaration for the variable is as follow:
char gender;
Basic Structure in
Chap 3
Programming
Determine whether the following identifiers is valid or
invalid. Give reason for invalid cases.
1) Parit Raja
2) 20thCentury
3) int
4) INTEGER
5) _BMW2003
6) Reservedword
7) BIT1033
8) markah_pelajar
9) jam*kredit
10) printf
Basic Structure in
Chap 3
Programming
Write a suitable variable declaration for each of the following
statement:
i. Salary of an employee
ii. Student’s mark for programming subject
iii. ATM pin number
iv. Phone number
v. Price of one item
vi. Bus seat number
vii. Student name
Basic Structure in
Chap 3
Programming
Based on the following problem, determine the appropriate
variables can be declared:
Given the value of x is 10 and a is 12,
find the result of the following equation:
y = 2x + a - 6
Basic Structure in
Chap 3
Programming
Based on the following problem, determine the appropriate
variables can be declared:
Mrs Sue needs to determine her students grade for
programming subject based on the mark scored during
final examination. The ‘A’ grade will be given if the mark
scored is between 85 to 100. If a student has scored 90
marks, what is the grade should Mrs Sue gives to the
student?
Basic Structure in
Chap 3
Programming
Based on the following problem, determine the appropriate
variables can be declared:
A box has height, width and length.
Calculate the volume of a box.
Basic Structure in
Chap 3
Programming
Based on the following problem, determine the appropriate
variables can be declared:
Uncle Degawan wants to buy 5 tins of paint from
Cinda’s shop. The price of each tin of the paint is
RM 15.60. Calculate the price which Uncle Degawan have
to pay for all the tin of paints he bought.