Lesson 5 - Variables
Lesson 5 - Variables
Variables
INTRO PROG
𝒂𝟐 + 𝒃𝟐 = 𝒄𝟐
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.
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:
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:
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.
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’;
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:
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.