0% found this document useful (0 votes)
15 views

CP2 Module 5 - Variables and Data Types

1) The document discusses variables and data types in C++ programming. It defines variables as containers that store and retrieve data in memory, and notes they have different data types like int, float, char, and string to represent stored values. 2) The main variable data types are described, along with their size and examples. Int stores whole numbers, float and double store fractional numbers with different levels of precision, char stores single characters, and string stores sequences of characters. 3) Rules for naming variables in C++ require them to start with a letter, allow numbers but not as the first character, and prohibit spaces or special characters. Keywords cannot be used as names.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

CP2 Module 5 - Variables and Data Types

1) The document discusses variables and data types in C++ programming. It defines variables as containers that store and retrieve data in memory, and notes they have different data types like int, float, char, and string to represent stored values. 2) The main variable data types are described, along with their size and examples. Int stores whole numbers, float and double store fractional numbers with different levels of precision, char stores single characters, and string stores sequences of characters. 3) Rules for naming variables in C++ require them to start with a letter, allow numbers but not as the first character, and prohibit spaces or special characters. Keywords cannot be used as names.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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