What is a variable?
are a means of storing data for
Variables come in different types and
store different kinds of information:
Integers (whole numbers)
– 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
Declaring a variable
declare a variable, you write its type
and its name (which you give it).
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
use the equal sign to assign values to
a variable. Examples:
students = 30;
name = “CS111”;
full = false;
Strings must be contained within double
char variables must be contained within
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;
More on variable names
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.