0% found this document useful (0 votes)
11 views35 pages

Lesson 5 - Variables

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views35 pages

Lesson 5 - Variables

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Welcome!

Variables
INTRO PROG

JOBERT C. GRAPA, BSIT


Instructor
Let’s
pray…
Review…
• C++ Basics
 What is C++?
 C++ Installation
 Your First C++
Program
 C++ Output
Today, we’ll talk
about…
• Variables
 What are variables?
 Data types
 Identifiers
 Definition, Declaration,
and Initialization
Where to
put? It’s always good to practice segregating
wastes properly. Let us dispose of the wastes below
in the appropriate wastebin.
• Used tissues, paper towels, • Candles and wax, cigarette butts and
napkins ashes
• Masks, plastic gloves • Gum packages, blister packs
• Hot drink cups • Light bulbs, dishes, drinking glasses
• Cold drink cups, straws • Metallic gift wrap and bows
• Plastic bubble wrap • Broken mugs, dishes
• Plastic or foil wrappers, • Feminine hygiene products
aluminum foil
• Drink pouches, straws

Biodegrada Non- Hazardous


Variables
Variables
At this point of our academic experience, we should
have heard and encountered the term “variable” –
whether in Algebra, Geometry, and just about any
branch of Mathematics. It is usually the symbol for
representing an unknown value. One notable example is

𝒂𝟐 + 𝒃𝟐 = 𝒄𝟐
the use of variables in the Pythagorean Theorem:
Variables
A variable is a portion of memory that stores
data values that our program can manipulate.
Each variable in C++ has a specific type, which the
size, layout, range of values and the set of operations that
can be applied to the variable.

Note: Our goal is to reserve a storage in our


program where the data will be stored so it can be
used in the program.
Variables
Find the sum of two
numbers:
num1 + num2 =
sum

Variable
s
Variables
Find the sum of two numbers:

2 +
3
=
Variable 5
s
Variables
Display the user’s first and last
name:

First Name:
Jobert Variable
Last Name: s
Grapa
Variables
Here are some examples of C++
variables:

Variables
: a is to 5
equal
b is to 6
equal
result no value ye
has t
Variables
Result:
Variables
Here are some examples of C++
variables:

int age = 18;


char MiddeInitial = ‘J’
string FirstName =
“Ford”;
int result;

Syntax:
Data type identifier =
value;
Data Types
A data type is a classification that specifies which
type of value a variable has – numbers, text, paragraphs,
decimal points, etc. It is very important which data type to
use when creating a variable. Here are some of the most
common C++ data types:

Data Type Keyword


Character char
Integer int
Floating Point/Double Floating float/double
Point
String string
Boolean bool
Data
types(char) – Stores a single character, letter or
Character
number value.
A, a, x, y, 1, &
Integer (int) - Stores whole numbers, without decimals.
1, 5600, 1010110
Floating Point (float) - Stores fractional numbers,
containing one or more decimals. Sufficient for storing 7
decimal digits.
0.1234567, 12.5, 329.9999
Data
types
Double Floating Point (double) - Stores fractional
numbers, containing one or more decimals. Sufficient for
storing 15 decimal digits.
0.123456789012345, 12.5, 329.9999
String (string) – stores more than one symbol or
characters.
This is good for words, paragraphs, etc. To use strings, you
must include an additional header file in the source
code, the <string> library.
#include
Data
types
Boolean
values.
(bool) - Stores true or false

1, 0 (1 = True, 0 =
False)
Variable Syntax:
Data type
identifier;
Identifiers
An identifier is a user-defined word used to represent
the variable. Programmers can either use any words, or
string of alphanumeric characters as identifiers. Identifiers
must always start with an alphabet or an underscore (_). To
use an identifier, it must be written after the datatype:

int a;
float
average;
...where a and average are the identifier for
the data types int and float.
Variables
Here are some examples of C++
variables:

Variables
: a is an intege
r
b is an intege
r
c is an intege
r
Identifiers
While you can choose any words as your
identifiers, there are words whose meaning is already
defined by C++ compilers, thus, you can not use it as
identifiers. These are called keywords.
Keywords
Here are the C++
keywords:
Definition,
Declaration, and
Initialization
Definition, declaration and Initialization are the
three steps in writing a variable.

Definition – is simply defining which data


type to use depending on the value.

int - numbers
cha - single letter, symbols
r - number with decimal
float/ points
double - words, paragraphs, etc.
-
Definition,
Declaration, and
Initialization
Declaration – is writing the variable identifier. Identifiers
must always start with an alphabet or an underscore (_). To
use an identifier, it must be written after the datatype:

Syntax:
datatype identifier;
Exampl int a;
e: float
_average;
Definition,
Declaration, and
Initialization
Note: Identifiers must only include one word. Should you
desire to put multiple words, you can separate them by
adding an underscore, a hyphen or just write them in a
single word.

Example:
string First_Name;
string LastName;
string Middle-
Initial;
Definition,
Declaration, and
Initialization
Initialization – is setting an initial value to the
variable.

Syntax:
datatype identifier = value;
int a = 50;
Example: int b = 40;

float quotient = a / b;
Note: The initialized value must agree with the data type it defines.
Definition,
Declaration, and
Initialization
Format:
Each datatype has it’s way of setting values:
char – the value must be enclosed with a single quote (‘ ’).
Example: char MiddleInitial =
‘R’;

string – the value must be enclosed with a double quote (“


”).
Example: string FullName =
Exampl float average =
“Tony Stark”;
e: 45.7; int result =
Definition,
Declaration, and
Initialization
There are two formats in declaring and initializing variables:

1. All variables can be declared and initialized separately.


Exampl int
e: a; Note: Each
variable must
int always be
b; separated
2. Variables with the same data type, can be
float with a semi-
declared together (must be separated with a comma colon (;).
_aver
“,”) Exampl age = b;
int a,
e: 47.5;
float ave,
result;
Practice
Let’s Activity
practice writing appropriate variables for the
following
program/scenarios:
Program 1:
A program that displays the user’s first
name.

Datatype Identifier
s: s:
string - first FirstNam
name e
Variables:

string FirstName =
“Tony”;
Program 2:
A program that displays the user’s first name, last name,
and
age.

Datatype Identifier
s: s:
string - first name, last FirstName,
name Lastname,
int - age age
Variables:
string FirstName =
“Tony”;
string LastName =
“Stark”;
Program 3:
A program that displays the sum of two
numbers.

Datatypes:

int – first number, second number, and


the sum.

num1, num
Identifiers: int num1, num2,
Variables:
2, sum sum=num1+num2;
O int
r num1;
int
num2;
Assignmen
tWrite the variables for the following programs:
1. A program that displays the quotient of two integers.
2. A program that displays your first name, middle initial, last
name and address.
3. A program that displays the average of three numbers.

Note: You can write the identifier and value on your own.
Deadline:
Wednesday, October 30, 2024 at 6:00 PM
through Google Classroom.
That’s all
for
today!
Thank you.
Keep safe.
God bless.

You might also like