1
Algoritma dan Pemrograman
Chapter 2
Simple Variables and Data Types
04/05/24
Chapter Overview
2
1. Variables, constants and assignment statements
2. Data types
3. Arithmetic operators
4. Input statement
5. Operator precedence
04/05/24
Variables
3
A storage location in computer’s memory
Has a name and a type of data it can hold
Variables are like small erasable whiteboards
We can write a number on them
We can change the number
We can erase the number
C++ variables are names for memory locations
We can write a value in them
We can change the value stored there
Must be defined before it can be used
We cannot erase the memory location
1. Variables, Constants, and Assignment Statements 04/05/24
Identifiers
4
Variables names are called identifiers
Choosing variable names
Use meaningful names that represent data to
be stored. For example, “studentGrade”, “studentMarks”, etc.
Identifier Rules:
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/different.
Special characters are not allowed to be used.
Keywords cannot be used as an identifier.
1. Variables, Constants, and Assignment Statements 04/05/24
Literals
5
A literal is a value that is written into a program’s
code.
"hello, there" (string literal)
12 (integer literal)
Variable Definition
o int number;
Variable Initialization
o number = 5;
You can also write the above two statements as:
o int number = 5; //variable declaration & initialization
1. Variables, Constants, and Assignment Statements 04/05/24
Valid and Invalid Identifiers
6
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 $
1. Variables, Constants, and Assignment Statements 04/05/24
C++ Key Words
7
You cannot use any of the C++ key words as an
identifier. These words have reserved meaning.
1. Variables, Constants, and Assignment Statements 04/05/24
Assignment Statement
8
The expression on the right-hand side of the equal
sign is evaluated first, and the result is then assigned
to the variable on the left-hand side.
Syntax:
Variable = Expression;
Examples
distance = rate * time;
count = count + 2;
1. Variables, Constants, and Assignment Statements 04/05/24
C++ Program with a variable
9
5 is an integer literal
This is a string literal
1. Variables, Constants, and Assignment Statements 04/05/24
Constants
10
Unlike a variable, a constant is a data item whose
value cannot change during the program’s execution
Following program contains integer constants
1. Variables, Constants, and Assignment Statements 04/05/24
C++ Data Types
11
Data is information that can be stored and used by a
computer program.
There are many different types of data.
Variables are classified according to their data type,
which determines the kind of information that may
be stored in them.
Integer variables can only hold whole numbers.
04/05/24
Things to consider while using numeric data
12
Your primary considerations for selecting the best
data type for a numeric variable are the following:
Whether the variable needs to hold integers or floating-
point values, the largest and smallest numbers that the
variable needs to be able to store.
Whether the variable needs to hold signed (both positive
and negative) or only unsigned (just zero and positive)
numbers.
The number of decimal places of precision needed for
values stored in the variable.
2. Data Types 04/05/24
Integer Data Types Sizes & Ranges
13
Some integer
variable definitions:
Depending on your operating system, the sizes and ranges
may be different. E.g. int 2 bytes for 16-bit systems (MS DOS)
2. Data Types 04/05/24
Defining Variables
14
Variables of the same type can be defined
- On separate lines:
int length;
int width;
unsigned int area;
- On the same line:
int length, width;
unsigned int area;
Variables of different types must be in different definitions
2. Data Types 04/05/24
Program to demonstrate “INTEGER VARIABLES”
15
2. Data Types 04/05/24
The char Data Type
16
A variable of the char data type holds only a single
character.
2. Data Types 04/05/24
Relationship between characters & Integers
17
2. Data Types 04/05/24
Float Data Type
18
Floating-point data types are used to define variables
that can hold real (decimal point) numbers.
*Some compilers use more than 8 bytes for long doubles.
These allow greater ranges.
2. Data Types 04/05/24
Signed Data Types
19
Note: The values of any of these may be different on your system.
2. Data Types 04/05/24
Unsigned Data Types
20
2. Data Types 04/05/24
Determining the Size of a Data Type
21
The sizeof operator may be used to determine the
size of a data type on any system.
2. Data Types 04/05/24
Arithmetic Operators
22
There are many operators for manipulating numeric
values and performing arithmetic operations.
Three types of operators:
Unary operators only require a single operand. For
example, consider the following expression: −5
Binary operators work with two operands. E.g. 4+2
Ternary operators, as you may have guessed, require
three operands. C++ only has one ternary operator which
will be discussed later.
04/05/24
Fundamental Arithmetic Operators
23
Write a program to display the sum, difference,
product, division and modulus of two numbers.
3. Arithmetic Operators 04/05/24
The Input Statement Using cin Object
24
cin >> variable ;
cin takes the value from the user at
run time and stores it in the variable
04/05/24
Input Example using cin
#include<iostream> 25 OUTPUT
void main( ) {
Enter value for i = 7 8 6
clrscr(); value of i = 65000
int i; float j; char w; Enter value for j = 5.8 9
value of j = 100000
cout<<“Enter value for i = ”; Enter value for w = c
cin>>i;
value of k = 543.77
cout<<endl<<“Enter value for j = ”; The value
valueyou
of lentered
= for i = 786
50000.897
cin>>j;
cout<<endl<<“Enter value for w = ”;
value
The value you m =90000.8997
of entered for j = 5.89
cin>>w; value
The value of entered
you c =w for w = c
cout<<endl<<“The value you entered \
for i = “<<i<<endl<<“the value you \
entered for j = “<<j<<endl<<“the value\ you entered for w = j i
“<<w;
}
4. Input Statement 04/05/24
Program to take input from user
26
Stream extraction operator
4. Input Statement 04/05/24
Entering Multiple Values
27
You can use cin to input multiple values at once.
4. Input Statement 04/05/24
cin to read multiple values of different data types
28
4. Input Statement 04/05/24
Operator Precedence
29
Operator precedence describes the order in which
C/C++ evaluates the expression?
It is possible to build mathematical expressions with
several operators. The following statement assigns
the sum of 17, x, 21, and y to the variable answer.
answer = 17 + x + 21 + y;
Some expressions are not that straightforward, e.g.
outcome = 12 + 6 / 3;
12 + 6 / 3
12 + 2
14
04/05/24
Precedence
30
5. Operator Precedence 04/05/24
Arithmetic & Algebraic Expressions
31
5. Operator Precedence 04/05/24
References
32
1. Problem Solving with C++ (10th Edition), Walter Savitch, Addison Wesley,
2018
2. CS201 Introduction to Programming, Virtual University of Pakistan
3. Paul J. Dietel, C++ How to Program (10th Edition), Pearson, 2017.
4. Turbo C programming for the PC, Robert Lafore
04/05/24