Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 28

C++

PROGRAMMING
 #include <iostream.h>
▪ cout object
▪ cin object
 Identifier
 Define variable
 Data type
 Integer
 Floating point
 Char
 Boolean
 Literal
 Variable Definition
 Variable Assignment
 Variable Initialization
 Arithmetic operation
 Comment
 Mathematical Expressions  Order of Operations
 Algebraic Expressions
 Constants
Program output
Character Name Meaning
{ } Open/close brace Encloses a group of statements

" " Open/close Encloses string of characters


quotation marks
; Semicolon End of a statement
 Displays output on the computer screen
 You use the stream insertion operator << to send output
to cout:

cout << "Programming is fun!";

Programming is fun!
 This produces one line of output:

cout << "Programming is ";


cout << "fun!";

Programming is fun!
 You can use the endl manipulator to start a new line of
output. This will produce two lines of output:

cout << "Programming is" << endl;


cout << "fun!";

Programming is
fun!
 You can also use the \n escape sequence to start a new
line of output. This will produce two lines of output:
TIP: Notice that the \n is
INSIDE the string.

cout << "Programming is\n";


cout << "fun!";

Programming is
fun!
cout<<"Saya belajar di UPSI"<<endl<<"Saya suka UPSI"<<endl;
cout<<"Saya akan belajar rajin-rajin";

Saya belajar di UPSI


Saya suka UPSI
Saya akan belajar rajin-rajin
cout<<"Saya belajar di UPSI\nSaya suka UPSI\nSaya akan belajar rajin-rajin Saya pelajar multimedia";

Saya belajar di UPSI


Saya suka UPSI
Saya akan belajar rajin-rajin Saya pelajar multimedia
cout<<"Saya sayang ibubapa saya."<<endl;
cout<<"Mereka mempunyai harapan tinggi terhadap saya."<<endl;
cout<<"Saya tidak akan mengecewakan mereka.";

Saya sayang ibubapa saya.


Mereka mempunyai harapan tinggi terhadap saya.
Saya tidak akan mengecewakan mereka.
Write a program to display output as follows :

My name is Shakirah.
I’m studying at UPSI.
I’m doing degree in Software Engineering.

cout<<“My name is Shakirah.”<<endl;


cout<<“I’m studying at UPSI.”<<endl;
cout<<“I’m doing degree in Software Engineering.”;
cout<<“******\n******\n********\n”<<endl<<endl<<“##########”;

******
******
********

##########
An identifier is a programmer-
defined name for some part of a
program: variables, functions, etc.
 A variable name should represent the
purpose of the variable. For example:

itemsOrdered

The purpose of this variable is to hold the


number of items ordered.
 The first character of an identifier must be
an alphabetic character or an underscore
 After the first character you may use
alphabetic characters, numbers, or
underscore characters.
 Upper and lowercase characters are distinct
(case sensitive)
Anita (5 character/5 aksara)

Jamilah (7 character/7 aksara)


( first)(second and onwards)
_ _
a..z a..z
A…Z A..Z
0..9
Ahmad
_999
_9_aminah
__amy
4sum
_#jumlah
Dividen
Product_
Product_001
 Case sensitive

sum

Sum

sum and Sum are two different variables


You cannot use any of the C++ key words as an
identifier. These words have reserved meaning.
IDENTIFIER VALID? REASON IF INVALID
totalSales Yes
total_Sales Yes
total.Sales No Cannot contain .
4thQtrSales No Cannot begin with digit
totalSale$ No Cannot contain $
_totalSales Yes
_44sales Yes
total sales No Contain space
while No Key word
While Yes
 Has a name and a type of data it can hold
 Must be defined before it can be used:

Syntax to define variable


data_type variable_name;
DataType Length Range
(in bits)
int 16 -32768 to 32767
long 32 -2147483648 to 2147483647

unsigned int 16 0 to 65535


unsigned long 32 0 to 4294967296
Define variable
data_type variable_name;

Example
int number;
EXAMPLE
// This program has a variable
#include<iostream.h>

void main () define variable


{
int number;

number = 50;
cout<<"The value in number is ";
The value in number is 50
cout<<number;

}
EXAMPLE
// This program has a variable
#include<iostream.h>

void main () define variable


{
int number;

number = 50;
cout<<"The value in number is "; The value in number is number
cout<<"number";

}
Notes:

Make sure you understand slide 25 and 26


clearly.
- print string ( print sentence/word)
- print variable
int ringgit;
ringgit = 100;
cout<<"I have RM"<<ringgit<<"."<<endl;

Output

I have RM100.

You might also like