Professional Documents
Culture Documents
Data Variable
Data Variable
Data Variable
A variable gives us names that we can use to store values that our
programs can manipulate. In C++, each variable has a type that
determines the size and layout of the variable's memory, the range of
values that can be stored within that memory, and the set of operations
that can be applied to the variable.
Data
Data Variable
Create a variable name that tells the program's reader what the
variable represents and describes the data stored at that specific
memory location. For example, if you want to save a test score, a
variable name like score is easier to remember than a variable name like
val2.
Creating and Naming Variables
• If you don't intend to, don't use the same variable name twice.
• For variable names, avoid using accented characters.
Variables in C++ can only be created with data types like integers
or strings, and they can only contain data that can be presented to
them. This restriction on the data types of variables results in faster
development and more efficient program execution.
int - is used to store and process integer numbers or whole numbers (i.e., numbers without a decimal point).
- Ex: 24, -19, 2021, etc.
Creating and Naming Variables
Data Types Meaning
long - All numbers in the interval [-9223372036854775808, 9223372036854775807] can be stored in a long
variable.
float - is used to store and process single-precision real numbers (i.e., real numbers containing 6 to 7 digits after
the decimal point).
- Ex: 3.142, 9.8, 2.123456, 3.4E+38
double - is used to store and process double-precision real numbers (i.e., real numbers containing 15 to 16 digits
after the decimal point).
- Ex: 2.1234567898765
char - is used to store and process any symbol from the ASCII character set enclosed between a pair of single
quotations.
- Ex: ‘A’, ‘9’, ‘$’, ‘\n’, ‘\t’
Defining Variables
Variables are declared in Take a look at other examples of types and the
C++ by stating their type, variable names assigned to them. Consider the
followed by their name or comments that come after the double slash; these
identifier, followed by a describe how the variable should function.
semicolon. Example:
A variable declaration char letter; //letter will hold a single non-numeric character
informs the computer int price; //price will hold a whole number value
that there is only one int quantity; //quantity will hold a whole number value
variable of the specified double money; //money will hold numbers with decimal value
type and name. float area; //area will hold numbers with decimal value
Defining Variables
Simulate the given program. Notice Program Output
how variables are declared.
We use the equal (=) sign, Syntax for assigning values to variables:
which is an assignment <variable name> = value;
operator, to assign values
to variables.
Examples:
Explore its syntax and the letter = 'A';
examples that follow. num1 = 80;
money = 100.50
Assigning Values to Variables
Program