You are on page 1of 13

COMPUTER PROGRAMMING 2

Variables and Data Types


PRESENTED BY:
Jhaun Paul G. Enriquez
SHS ICT Faculty
1
LEARNING OUTCOMES:

1. Explain how variables are used in C++


2. Differentiate local and global variables in C++
3. Describe the different variable data types used in C++
4. Create a C++ program using different variable data types.

COMPUTER PRORAMMING 2 2
What is a Variable?
• the name representing a piece of your computer’s memory
• container used to store, retrieve, and use data
• has different data types to represent the stored information
and how much memory it needs and how it will be used
In Algebra: In Programming:

x2 - 3xy + y2 int x = 0
a + 2b = 4 char b = 0

COMPUTER PROGRAMMING 2 3
Variable Data Types:
Data Type Size Description Example
int 4 bytes Stores whole numbers, without decimals 0
240
float 4 bytes Stores fractional numbers, containing one or more decimals. 2.51
Sufficient for storing 7 decimal digits 34.26
double 8 bytes Stores fractional numbers, containing one or more decimals. 3.14
Sufficient for storing 15 decimal digits 75.38
char 1 byte Stores a single character/letter/number, or ASCII values a
@
string store a sequence of characters (text) Hello
Hello World
bool 1 byte Stores true or false values true
false

COMPUTER PROGRAMMING 2 4
Let’s check our understanding:
A char
✓ whole numbers
B int ✓ 364.25
C bool
✓ "Juan Dela Cruz“
✓ value true or false
D double ✓ a letter or special character
E string

COMPUTER PROGRAMMING 2 5
How do we use variables?
• Before we can use a variable, we must declare or create it.
• To declare a variable, we need to provide two things:
• A type for the variable.
• A name for the variable.
type of variable
end of statement
int score;

name of variable

COMPUTER PROGRAMMING 2 6
Rules in Naming Variables in C++:
1. Variable names in C++ can range from 1 to 255
characters.
✓ num1
2. All variable names must begin with a letter of ✓ _salary
the alphabet or an underscore (_). ✓ employeeStatus
3. Variable names can also contain numbers but ✓ student_id
not as the first character of the identifier.
4. Variable names are case-sensitive. 1234
5. No spaces or special characters are allowed. final grade
return
6. Keywords in C++ (reserved words) cannot be
2num
used as a variable name.
COMPUTER PROGRAMMING 2 7
How do we use variables?
• Next, we must initialize or assign a value to the variable.
• To initialize a variable, we use the assignment operator (=):

assigned value
declared variable

score = 380;
end of statement
assignment operator

METHOD 2: int score = 380;

COMPUTER PROGRAMMING 2 8
Declare and Initialize Variables:

COMPUTER PROGRAMMING 2 9
Let’s check our understanding:
• Create a variable named myNum and assign the value 50 to it.
____ _______ = ____
1 2 3

• Create a variable with the name message with the text Keep safe!.
string _______ = __Keep safe!__;
4 5 5

COMPUTER PROGRAMMING 2 10
References:
• Pomperada, Jake. 2019. Beginner’s Guide to C++ Programming.
Manila: Mindshapers Co., Inc.
• Pepito, Copernicus. 2009. Introduction to C++ 2008 programming.
Manila: National Bookstore.
• https://www.codecademy.com/courses/learn-c-plus-plus
• https://www.w3schools.com/cpp/default.asp
• https://www.tutorialspoint.com/cplusplus/index.htm

COMPUTER PROGRAMMING 2 11
COMPUTER PROGRAMMING 2 12
COMPUTER PRORAMMING 2

You might also like