Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Mahfudzah Othman
UiTM Perlis
CHAPTER 2
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 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 \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.”;
}
BASIC ELEMENTS
Identifier
2
CSC128
Mahfudzah Othman
UiTM Perlis
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;
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
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
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
#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
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