You are on page 1of 7

CSC128

Mahfudzah Othman
UiTM Perlis

CHAPTER 2

BASIC ELEMENTS OF A COMPUTER PROGRAM

Here are some examples of simple C++ program:

Eg 1:

#include<iostream.h>//header file
//This program prints “Assalamualaikum w.b.t.”

void main()
{
cout<< “Assalamualaikum w.b.t\n”;
}

 The #include directive on the first line is necessary for the program to
have output. It refers to an external file named iostream.h where
information about cout object is provided. The angle bracket < and > are
not part of the file name. They are used to indicate that this is a Standard
C++ Library file.

 The second line is comment, identified by the double slashes //.


Comments are included in programs to provide explanations for human
readers. They are ignored by the compiler.

 The third line contains the function header void main(). This is required
for every C++ program. It tells the compiler where the program begins.
The parentheses () following main are also required.

 The fourth and seven line contain only braces { and }. These enclose the
body of the main() function and are also required for every C++ program.

 The fifth line contains the statement


cout<<”Assalamualaikum w.b.t\n”;

This tells the system to send the message “Assalamualaikum w.b.t\n” to


the cout (see-out) object. The output should look like this:
Assalamualaikum w.b.t

 The \n symbol is the newline symbol. Note that this single symbol is
formed from the two characters ‘\’ and ‘n’. Putting this symbol at the end
of the quoted string tells the system to begin a new line after printing the
preceding characters, thus ending the current line.

1
CSC128
Mahfudzah Othman
UiTM Perlis

 The symbol << is called the output operator. It inserts the message into
the output stream.

Eg 2:

#include <iostream.h>
const double CITY_TAX_RATE = 0.0175;

void main()
{
double gross_income, city_tax;
cout<<”Enter the gross income :”;
cin>>gross_income;
city_tax = CITY_TAX_RATE * gross_income;
cout<<City tax is :”<<city_tax<<”dollars.”;
}

We will use these programs for explanation below.

BASIC ELEMENTS

Identifier

Is used to represent name and reference certain program entities.


Eg: gross_income, city_tax.

Rules: i) Can consist of the capital letters A to Z, the lowercase letters a to z,


the digits 0 to 9 and underscore.
ii) The first character must be a letter or an underscore
iii) No length limitation. In many implementations, compilers recognize
only the first 32 character.
iv) No embedded blanks
v) Reserved words cannot be used
vi) Are very case-sensitive. Therefore, TAX and tax are different.

Eg: For illegal identifiers

Student age because embedded blank


Continue because continue is a reserved word
17throw because first char is a digit
Principle+interest because special char +

2
CSC128
Mahfudzah Othman
UiTM Perlis

We should conform to some style guidelines:


i) Avoid excessively short and cryptic name. Eg: x or wt. Use descriptive
name. Eg: student_major, down_payment.
ii) Use underscore or capital letter to separate word. Eg: student_major
or studentMajor are much easier to read than studentmajor.

Variable

Variables are identifiers that are used to keep a value from certain type. It holds
value that can be changed during the execution of the program.
Eg: double gross_income, city_tax;
int bil_book;
float a,b,c.;

Constant

Are entities that appear in the program code as fixed value (cannot be changed).
Eg: const double CITY_TAX_RATE = 0.0175;

There are several types of constant:


i) Integer constant:
 Numbers with no fractional part/no decimal places.
 Can contain digital values 0 through 9.
 Commas are not allowed in integer constants.
Eg: 1,500,000 is illegal. It must be written as 1500000
Eg: -15,0,+250 and 7550.
Eg: 0179 is illegal because its first digit is zero.

ii) Floating point constant:


 Are positive or negative decimal numbers with an integer part.
Eg: 20.35, 20, 20.0, -2, and 0.2
Eg: 1,500.57 are illegal. Must be written as 1500.57.

iii) Character constant:


 Is a character enclosed in single quotation marks.
Eg: ‘I’, ‘n’, ‘A’.
Eg: cout<<’T’; will print the letter T on the screen.
cout<<’n’; will print the letter n on the screen.
Whereas cout<<’\n’; will bring the cursor to the beginning of
a newline.

iv) Statement/String:
 Is a sequence of any number of characters surrounded by double
quotation marks.
Eg: cout<<”This is a string constant”’; will print This is a string
constant

3
CSC128
Mahfudzah Othman
UiTM Perlis

cout<<”125”; will print 125


cout<<”Is this John’s car?”; will print Is this John’s car

Named constant
 Const double CITY_TAX_RATE = 0.0175;
 Is an identifier whose value is fixed and does not change during the
execution of a program.
 Instead of statement: city_tax=0.0175 * gross_income; we can write
equivalently city_tax= CITY_TAX_RATE * gross_income;

DATA TYPE

 Is a set of data values and a set of operations that can be applied on


these values.
 Eg: double gross_income, city_tax;
 Types of gross_income and city_tax is double

There are five data types:


i) int
ii) double
iii) float
iv) char
v) void

Data type int


 Is used to declare numeric program variables of integer type.
 Eg: int counter;
 Can be positive or negative
 Range –32,768 through 32,767

Data type double/float


 To declare floating point variable
 Eg: double gross_income;
 Eg: float gross_income;

Data type char


 Is any value that can be represented in the computer alphabets.
 Eg: char more;
 Eg: char more=’y’;

Data type void


 Has no values/operations (empty).

4
CSC128
Mahfudzah Othman
UiTM Perlis

Example:

In a program In a memory
char Status M
int quantity 20
double pie_radian 3.1415926
float cgpa 3.66

ARITHMETIC OPERATORS

An operator is a symbol that “operates” on one or more expressions, producing a


value that can be assigned to a variable.

Operators are: +, -, *, /, and %

Table 1.1 Integer Arithmetic Operators

Operator Description Example


+ Add M+N
- Subtract M–N
- Negate -N
* Multiply M*N
/ Divide M/N
% Remainder M%N

Eg 1: This program illustrates the use of the six arithmetic operators:

#include<iostream.h>

void main()
{
int m=38, n=5;
cout<<m<<” + “ <<n <<” = “<<(m+n)<<endl;
cout<<m<<” - “ <<n <<” = “<<(m-n)<<endl;
cout<<” - “ <<n <<” = “<<(-n)<<endl;
cout<<m<<” * “ <<n <<” = “<<(m*n)<<endl;
cout<<m<<” / “ <<n <<” = “<<(m/n)<<endl;
cout<<m<<” % “ <<n <<” = “<<(m%n)<<endl;
}

5
CSC128
Mahfudzah Othman
UiTM Perlis

Run:

38 + 5 = 43
38 - 5 = 33
- 5 = -5
38 * 5 = 190
38 / 5 = 7
38 % 5 = 3

Eg 2: This program is used to determine how the computer handles the division
of negative integers:

#include<iostream.h>

void main()
{
int m=-14, n=5, q=m/n, r=m%n;
cout<<”m =”<<m<<endl;
cout<<”n =”<<n<<endl;
cout<<”q =”<<q<<endl;
cout<<”r =”<<r<<endl;
cout<<”q*n+r=”<<”(“<<q<<”)*(“<<n<<”)+”<<r<<”=”<<q*n
+r<<”=”<<m
<<endl;
}

Run:

m=-14
n= 5
q = -2
r = -4
q*n+r=(-2)*(5)+-4=-14=-14

6
CSC128
Mahfudzah Othman
UiTM Perlis

Operator Precedence and Associativity

Table 1.2 Some C++ Operators

Operator Description Precedence Associativity Example


- Negate 15 Right -n
* Multiply 13 Left m*n
/ Divide 13 Left m/n
% remainder 13 Left m%n
+ Add 12 Left m+n
- Subtract 12 Left m-n
<< Bit shift left, output 11 Left Cout<<n
= Simple assignment 2 Right m=n

The operator – has precedence level 15, and the operator * has precedence level
13, so negative is evaluated before multiply.

The column Associativity tells what happened when several different operators
with the same precedence level appear in the same expression.
Eg: + and – both have precedence level 12 and are left associative, so the
operators are evaluated from left to right.

Eg: 8 – 5 + 4

First 5 is subtracted from 8, and then 4 is added to that sum. The answer is 7

Do: Given a=10, b=20, c=15, d=8, e=40


a*b/(-15*31%13)*d =
a* (b*b) – (c*b) + d =