You are on page 1of 23

Variables,

Literals, and
Constants
Quinto, J.
Sanchez, T.

Slides by GCuyasen
Variable
• Memory location whose value
can change during runtime
• Variables are like storage
areas for holding values or
data where information can be
maintained and referenced.
Identifiers
• Programmer-define names that represent some
element of a program.
• Represents memory location (RAM)
❑Building Room Number
❑Locker Number
Example
int playerScore = 90;

int – data type


playerScore – identifier
90 – value
Variable
Definitions
Define one or more variables and
indicate the type of data they will hold.

Causes variables to be created in


memory.

“Variable Declaration”
Variable Definitions
Examples,

int playerScore; whole number values

words, sentence,
string playername; group of characters
Assigning Value
= (equal sign)
▪ assignment operator
▪ copies the value on it’s right into the variable to it’s left

int playerScore = 90;

assign 2 as the value of number


Assigning Value cont…
Store the result of expression on the right to the variable on the left.

int number = 5 * 3 + 2;

assign 17 as the value of number


Re-assignment

Variables can be assigned new values.


Succeeding statements that use the variable
will get the new value and the current and
preceding statements will not be affected.

Old value is overwritten.


Printing Variables

int num = 5;
cout << num; value of num is 5

num = 15;
cout << num; value of num is now 15
Sample Program
#include <iostream>

using namespace std;

int main () {
int number = 5;
cout << "The value in number is " << number << endl ;
return 0 ;
}
Literals
Literal
• Piece of data that is written directly into a program’s code
• One of the most common uses of literals is to assign a value to a variable

int number = 100;

number variable
100 integer literal
Literal cont
int score = 97;
cout << "Your score is " << score;

97 integer literal
"Your score is " string literal
C++ Literals

▪ Integer literals
▪ Floating-point literals
▪ Characters
▪ Escape Sequences
▪ String
Integer
Numeric literal without fractional or exponential part.

Examples,

0 -9 22 143 108
-143 1 214 127 107
Floating-point literal
Numeric literal that has either a fractional form or an exponent form

Examples,

-2.0 9.99 0.22


-3.0 2.75 1.50
Characters
Created by enclosing a single character inside single quotation marks.

Examples,

'j' 'c' 'Q' '2' '1' '3' '+' '?'


Escape Sequences
Characters that cannot be typed or have special marks

Examples,

\b Backspace \f Form feed \0 Null character


\n Newline \r Return
\t Horizontal tab \v Vertical tab
\\ Backslash \' Single quotation mark
\" Double quotation mark \? Question mark
Constants
Variables with fixed values.

Examples,

const float PI = 3.14;

Assigning different value to variable PI is not allowed.


String Literals
Sequence of characters enclosed in double-quote marks

Example

"good" ""
" " "x"
"Earth is round\n"
• Albarico, J.M. (2013). THINK Framework. Based on
Ramos, E.G. and N. Apolinario. (n.d.) Science LINKS.
Quezon City: Rex Bookstore Inc
• Gaddis, T., & Manna, M. M. (2015). Chapter 2 Introduction
to C++. In Starting out with C++: From control structures
References through objects . Harlow, Essex: Pearson Education
Limited.
• C Variables, Constants and Literals. (n.d.). Retrieved July
14, 2020, from https://www.programiz.com/c-
programming/c-variables-constants
• https://ph-live-05.slatic.net/shop/
b5336e5c1f037a39cc9f354e9d10b1ac.jpeg_2200x2200q80
.jpg_.webp
Image Link • https://i.pinimg.com/originals/c0/cb/57/c0cb57cb95dd9f3
991cf6564674eb206.jpg

You might also like