Lesson 6 - Working with variables
Lesson 6 - Working with variables
Working with
Variables
INTROPROG
Computer Programming
11
JOBERT C. GRAPA,
BSIT
Instructor
Let’s
pray…
Review…
• Variables
What are variables?
Data types
Identifiers
Definition, Declaration, and
Initialization
Today, we’ll talk
about…
• Working with
Variables
Variable Output
User Input
Variables
A variable is a portion of
memory that stores data values that our program
can manipulate.
Variables
Here are some examples of C++
variables:
Variables
: a is equal to 5
b is equal to 6
resul i equals to th
t s e
sum 5 and 6
of
Variables
Result:
Variables
Here are some examples of C++
variables:
Syntax:
Data type identifier =
value; Or
Data type identifier;
Variable Output
In our past discussions, we learned about the
standard output stream, the cout, and how we use it to
display simple texts on the screen. However, the cout can
also be used to display the value of any variables. To do
so, type cout, followed by the << operator, and then the
variable name.
Example:
string firstname = “Bruce
Banner”;
cout<<firstname;
Output:
Bruce Banner
Variable Output
For example: Output:
Variable firstname
Displays the value of the
variable firstname
Variable Output
For example: Output:
Variabl
es
Example:
string firstname =
“Jobert”; string lastname
= “Grapa”;
cout<<firstname
<<lastname;
Output:
JobertGrapa
Outpu
tdisplayLastly, you can combine texts and variables to
a more detailed output. To do so, simply add
double quotation where you will write the text, then use <<
to separate the text and the variables.
Example:
stringfirstname = “Jobert”
;
stringage = 25;
cout<<“Hi, my name is ”<<firstname<<“. I am
”<<age<<“years old”;
Output:
Hi, my name is Juleus. I am 25 years old.
Practice
Let’s Activity
practice writing appropriate variables for the
following
program/scenarios:
Program 1:
Write a program that display the user’s first
name.
Datatypes:
Variables:
string FirstName =
“Tony”;
Program 1:
Code: Output:
Program 2:
Write a program that display the user’s first name, last
name,
and age
Datatypes:
string FirstName =
“Tony”; string LastName
Program 2:
Code: Output:
Write the a program that displays the first
name, middle initial, last name, address and
age by the user input.
Assignmen
tWrite the variables for the following
programs:
Deadline:
Monday, November 4, 2024 at 6:00
PM
That’s all
for
today!
Thank you.
Keep safe.
God bless.