Professional Documents
Culture Documents
Variables, Literals, and Constants: Quinto, J. Sanchez, T
Variables, Literals, and Constants: Quinto, J. Sanchez, T
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;
“Variable Declaration”
Variable Definitions
Examples,
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 number = 5 * 3 + 2;
int num = 5;
cout << num; value of num is 5
num = 15;
cout << num; value of num is now 15
Sample Program
#include <iostream>
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
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,
Examples,
Examples,
Examples,
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