Chapter 3-Basic Elements of Computer Program
Chapter 3-Basic Elements of Computer Program
Prepared for:
CSC 402 – Programming 1
INTRODUCTION
What is a Computer
Program?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
COMPUTER PROGRAM
Put water
INSTRUCTION SET
Add a lemon
Add sugar
Taste it
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
A C++ PROGRAM
Syntax Rules:
Are rules that determines whether the statements (instructions)
within the program are valid or not
If valid, this means that the statements are accepted by the
programming language
Semantic Rules:
Are rules that determine the meaning of the instructions
COMMENTS
Comments:
Are meant only for the readers, not for the compiler
The compiler will ignore comments placed inside the program
SPECIAL SYMBOLS
Token:
Is the smallest individual unit of a program written in any language
C++ tokens include:
Special symbols
Word Symbols
Identifiers
WORD SYMBOLS
IDENTIFIERS
Identifiers:
Are names of things that appear in a program
This includes:
Variables
Constants
Functions
Can either be:
Predefined identifiers:
• cout
• cin
Identifiers defined by users
Consists of letters, digits and underscore character ( _ )
Must only begin with letter or underscore ( _ )
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
IDENTIFIERS
VARIABLE
Variable:
Is an identifier that refers to memory location, which can hold values
However, it can only store ONE value at a time
Variable Declaration:
Each variable must be declared before they can be used in the
program
dataType variableName;
DATA TYPES
Data Types:
Are used on identifiers that store data
It specify the type of data the identifiers can hold
This include:
Integer
Floating-point
Boolean
Single character
String of characters
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
DATA TYPES
Integers:
Are whole numbers that can either be negative, positive or zero
Cannot use comma ( , ) within an integer
For example:
int
DATA TYPES
Floating-points:
Are decimal numbers
For example:
DATA TYPES
bool
DATA TYPES
Single character:
Represents only one character, which include:
Letters
Digits
Special symbols
Each character is enclosed using single quotation marks ‘ ’
For example:
char
Eg: char grade = ‘A’;
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
DATA TYPES
String of characters:
Represents a sequence of characters
Each string is enclosed in a double quotation marks “ ”
Contains a null terminator (\0), therefore the size of string must be
declared one character longer than it actually is
For example:
“Hello” “hard work is the key to success!!”
char [ ]
QUICK EXERCISE
a) myFirstProgram
b) MIX-UP
c) C++Program2
d) quiz7
e) _part_2
f) full name
g) total_fees
h) 3rdNumber
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
QUICK EXERCISE
Give the most appropriate data type for the following values:
a) 23
b) ‘C’
c) 18.52
d) true
e) 2.452679753
f) “Michael Jackson”
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
WHITESPACES
Whitespaces:
Are used to separate special symbols, reserved symbols and
identifiers
It makes the program readable
This include:
Blanks
Tabs
Newline characters
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
a) age
b) grade
c) average
d) sum
e) address
f) totalCost
© Najwa Abd Ghafar – UiTM Johor
VARIABLE
©Rose Hafsah Ab Rauf – UiTM Shah Alam
Variable Initialization:
Variables can be initialized after it was declared
This is done by giving the variable a value, either by:
1. Assignment statement
variableName = expression; int age = 10;
This value placed inside must match the data type of variable
This value will represent the first value that is placed inside the
variable
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
CONSTANTS
Constants:
Are values that are fixed
The content inside it will not change throughout the program execution
ASSIGNMENT STATEMENT
©Rose Hafsah Ab Rauf – UiTM Shah Alam
Assignment statement:
Is used to place value into a variable
This value is assigned directly within the program by the programmer and
does not come from the value keyed in by the user
variableName = expression;
Suppose that num1, num2 and num3 are int variables and
the following statements are executed in sequence:
1. num1 = 18
2. num1 = num1 + 27
3. num2 = num1
4. num3 = num2 / 5
5. num3 = num3 / 3
PROMPT LINES
Prompt Lines:
Will inform the user what to do when interacting with the program
Always include prompt lines when getting input from user
#include <cstring>
String input:
Is a sequence of zero or more character and is enclosed in a double
quotation marks “ ”
cin.getline(variableName, size);
• cin function:
char name[30]; Will only input one word
For example: cin >> name; OR • cin.getline function:
cin.getline(name, 30); Will input more than one word
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
strcpy(x, y);
OUTPUT STATEMENT
Output statement:
Is used to display messages or output data
endl Manipulator
endl Manipulator:
Causes the insertion point to move to the beginning of the next line
ESCAPE SEQUENCES
Escape Sequences:
Allows you to control how your output looks
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
WHAT SHOULD BE
INSIDE A C++ PROGRAM?
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
PREPROCESSOR DIRECTIVES
Preprocessor Directives:
Are commands given to the preprocessor to allow us to use predefined
functions contained inside a header file
#include <headerFileName>
<iostream>:
Allow us to use input and output functions
cin
cout
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
namespace
To use cin and cout in a program, use the following two statements:
#include <iostream>
using namespace std;
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
Semicolons:
Every statement inside a C++ program will end with a semicolon ;
Commas:
Are used to separate items in a list float average, area, sum;
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
DEBUGGING
Debugging:
Is the process of finding and fixing errors in a computer program
The compiler will identify the syntax error
Therefore, syntax errors are found during compilation
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
QUICK EXERCISE
Hello World!!
I am excited to learn C++ programming.
QUICK EXERCISE
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
Write a program that prompts the user for their first name
and age. The output should be displayed as below:
Welcome Michael.
This year, you are 20 years old.
This is so exciting!!
Note: For this program, you are not allowed to use the endl manipulator.
QUICK EXERCISE
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
Write a program that asks the user's full name, student ID,
gender (male/female) and the amount of fees paid for one
semester in UiTM. Then, display a welcoming message to the
user and display the details that were entered by the user.
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
ARITHMETIC OPERATORS
Arithmetic Operators:
Are used to manipulate integral and floating-data types
This include:
+ addition
- subtraction Can be used with:
• Integral data type
* multiplication
• Floating-point data type
/ division
(gives quotient in ordinary division)
% modulus operator Can be used with:
(gives remainder in ordinary division) • Integral data type
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
ARITHMETIC EXPRESSION
Arithmetic Expression:
Is constructed by using:
Arithmetic operators
Operands (are the numbers)
Unary Operator:
Operator that has only one operand
This include: +, -
Binary Operator:
Operator that has two operands
This include: +, -, *, /, %
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
ORDER OF PRECEDENCE
Order of Precedence:
Needs to be followed when there are more than one arithmetic operators
are used in an expression
Operator Operation
( ) Parenthesis Highest level of precedence
* , / , % Multiplication
Division
Modulus
Higher level is evaluated first
+ , - Addition
Subtraction Lowest level of precedence
EXAMPLE
1. X=8/2*3+4*5%3
2. X = 8 / 2 * (3 + 4) * 5 % 3
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
EXAMPLE
1. X=8/2*3+4*5%3
2. X = 8 / 2 * (3 + 4) * 5 % 3
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
EXPRESSIONS
Integral Expression:
Is an expression when all of the operands (numbers) involved are integers
For example:
2+3*5
Will give out an integral result
Floating-point Expression:
Is an expression when all of the operands (numbers) involved are floating-
point
For example:
12.8 * 17.5 – 34.50
Will give out a floating-point result
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
EXPRESSIONS
Mixed Expression:
Is an expression when all of the operands (numbers) involved are both
integers and floating-point
For example:
3 / 2 + 5.5
Rules of Evaluation:
If the operator have the same type of operands, it will be evaluated
according to the type of operand
If the operator has both types of operands, then the integer will be
changed to a floating point.
Therefore, it will yield a floating-point result
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
QUICK EXERCISE
QUICK TIP!!
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK QUESTION
<cmath>:
Allow us to use mathematical functions inside a program
pow (x, y)
x to the power of y
sqrt (x)
square root of x
log (x)
natural log of x
sin (x)
sine of x
cos (x)
cosine of x
tan (x)
tangent of x
abs (x)
absolute value of x
Must be in a floating-point expression
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK TIP!!
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
PREPROCESSOR DIRECTIVES
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
<cstring>:
Allow us to use predefined functions on cstring
strcpy (x, y)
Will copy a string, y into a variable of type string, x
Output:
strlen (x)
Will return the length of the string in x (excluding the null terminator)
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
PREPROCESSOR DIRECTIVES
<iomanip>:
Allows you to manipulate how your output is displayed
EXAMPLE
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK TIP!!
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
CONTROL STRUCTURES
QUICK EXERCISE
QUICK TIP!!!
© Najwa Abd Ghafar – UiTM Johor
©Rose Hafsah Ab Rauf – UiTM Shah Alam
QUICK EXERCISE
Calculate the total wages a student earns working a part time job, given their
hourly rate and hours worked in a month.
QUICK EXERCISE