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

Lesson 6 - Working with variables

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lesson 6 - Working with variables

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Welcome!

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:

int age = 18;


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

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

Displays the value of the


variables
User Input
To make a more useful program with the use of C++
variables, we will learn how to apply the standard input stream
on variables. This means, we will allow the users to set values
for the variables while the program is running. For C++, the
stream object defined to access the standard input is cin.
cin is a predefined variable that reads data from the
keyboard and stores the data to the declared variable. To do
so, type cin, followed by the extraction operator (>>) then the
variable name.
Example:
int x;
cin>>x;
User Input
For example:

Variable x doesn’t have a


value yet. This will allow the
user to input the value for
variable x.
User Input
This input syntax applies to all of the data types.
However, for string data type, cin considers a space
(white space, tabs, etc.) as a terminating character which
means that it can only input a single word.
It only
displays the
first word
User Input
To input multiple words in string variable, we will use
another predefined variable for input called getline.
getline allows multiple words as string value. To do
so, enter getline(cin, variable_name);.
It
displays all
of the word
More on Output
You can display values of multiple variables. To do
so, simply add << and the second identifier after the first
identifier.

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:

string - first name

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 - first name, last name


int - age
Variables:

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:

1. A program that displays

Deadline:
Monday, November 4, 2024 at 6:00
PM
That’s all
for
today!
Thank you.
Keep safe.
God bless.

You might also like