0% found this document useful (0 votes)
26 views17 pages

CP2 Module 2 - Variables and Data Types in C++

This document provides an overview of variables and data types in C++ programming. It defines what a variable is, explains different data types like integers, floats, doubles, characters, strings and Booleans. It discusses how to declare and initialize variables, the rules for naming variables, and the scopes of local and global variables. Examples are given to demonstrate declaring and initializing variables of different data types.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
26 views17 pages

CP2 Module 2 - Variables and Data Types in C++

This document provides an overview of variables and data types in C++ programming. It defines what a variable is, explains different data types like integers, floats, doubles, characters, strings and Booleans. It discusses how to declare and initialize variables, the rules for naming variables, and the scopes of local and global variables. Examples are given to demonstrate declaring and initializing variables of different data types.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 17

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 Create Variables in C++?

DECLARATION INITIALIZATION
Provide the name and Provide the value of
data type of the variable the variable

COMPUTER PROGRAMMING 2 6
How do we create 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 (identifiers).
type of variable
end of statement
int score;

name of variable

COMPUTER PROGRAMMING 2 7
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 8
How do we Initialize 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 9
Declare and Initialize Variables:

COMPUTER PROGRAMMING 2 10
Variable Scopes:

COMPUTER PROGRAMMING 2 11
Creating Constants in C++
• When you do not want others (or yourself) to override existing
variable values
• use the const keyword (this will declare the variable as
"constant", which means unchangeable and read-only):

COMPUTER PROGRAMMING 2 12
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 13
Demonstration Sample Output:

COMPUTER PROGRAMMING 2 14
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 15
COMPUTER PROGRAMMING 2 16
COMPUTER PRORAMMING 2

You might also like