This document discusses variables in C++. It defines a variable as a means of storing data for a program. Variables come in different types like integers, decimals, characters, and more to store different kinds of information. Common variable types include int, double, char, bool, and string. Variables are declared with their type and name, and assigned values using the equal sign. Variables can also be initialized by declaring and assigning a value simultaneously. The document provides examples and rules for declaring, assigning, and initializing variables in C++.
2. What is a variable?
Variables
are a means of storing data for
the program.
Variables come in different types and
store different kinds of information:
Integers (whole numbers)
Decimal numbers
Characters
And more…
3. Common Types
int
– a positive or negative integer
double – a positive or negative decimal
char – a single character
bool – a true or false value
string – a string of text
The above names are case sensitive
Int, Double, Bool, or Char won’t work
4. Declaring a variable
To
declare a variable, you write its type
and its name (which you give it).
Examples:
int students; could be an integer variable
that stores the number of students in a class
string name; could store the class title
bool full; could store whether the class is full
or not
5. Assigning values
We
use the equal sign to assign values to
a variable. Examples:
students = 30;
name = “CS111”;
full = false;
Strings must be contained within double
quotes.
char variables must be contained within
single quotes.
6. Initializing
Sometimes
we declare variables and then
later assign their values, but sometimes we
want to give an initial value right away.
This is called initializing. Examples:
int students = 30;
string name = “CS111”;
bool full = false;
7. More on variable names
C++
rules for variable names:
Must start with a letter, not a number
Must have NO space in it
No special characters (only letters,
numbers, and underscore).
Must not be a C++ keyword, such as: return,
if, else, int, double, etc.