Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=
) to assign the value 10 to a variable called x: Continue reading C++ Assignment Operators
Assignment operators are used to assign values to variables.
In the example below, we use the assignment operator (=
) to assign the value 10 to a variable called x: Continue reading C++ Assignment Operators
Operators are used to perform operations on variables and values.
In the example below, we use the +
operator to add together two values:
int x = 100 + 50;
Although the +
operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable:
int sum1 = 100 + 50; // 150 (100 + 50)
int sum2 = sum1 + 250; // 400 (150 + 250)
int sum3 = sum2 + sum2; // 800 (400 + 400)
C++ divides the operators into the following groups:
Here’s a real-life example of using different data types, to calculate and output the total cost of a number of items:
// Create variables of different data types
int items = 50;
double cost_per_item = 9.99;
double total_cost = items * cost_per_item;
char currency = '$';
// Print variables
cout << "Number of items: " << items << "\n";
cout << "Cost per item: " << cost_per_item << currency << "\n";
cout << "Total cost = " << total_cost << currency << "\n";
The string
type is used to store a sequence of characters (text). This is not a built-in type, but it behaves like one in its most basic usage. String values must be surrounded by double quotes : Continue reading C++ String Data Types
The char
data type is used to store a single character. The character must be surrounded by single quotes, like ‘A’ or ‘c’:
char myGrade = 'B';
cout << myGrade;
Alternatively, if you are familiar with ASCII, you can use ASCII values to display certain characters: Continue reading C++ Character Data Types
A boolean data type is declared with the bool
keyword and can only take the values true
or false
.
When the value is returned, true
= 1
and false
= 0
.
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun; // Outputs 1 (true)
cout << isFishTasty; // Outputs 0 (false)
Use int
when you need to store a whole number without decimals, like 35 or 1000, and float
or double
when you need a floating point number (with decimals), like 9.99 or 3.14515. Continue reading C++ Numeric Data Types
As explained in the Variables article , a variable in C++ must be a specified data type:
int myNum = 5; // Integer (whole number)
float myFloatNum = 5.99; // Floating point number
double myDoubleNum = 9.98; // Floating point number
char myLetter = 'D'; // Character
bool myBoolean = true; // Boolean
string myText = "Hello"; // String
You have already learned that cout
is used to output (print) values. Now we will use cin
to get user input.
cin
is a predefined variable that reads data from the keyboard with the extraction operator (>>
).
In the following example, the user can input a number, which is stored in the variable x
. Then we print the value of x
: Continue reading C++ User Input
Let’s get a bit more practical!
Often in our examples, we simplify variable names to match their data type (myInt or myNum for int
types, myChar for char
types, and so on). This is done to avoid confusion.
However, for a practical example of using variables, we have created a program that stores different data about a college student: Continue reading C++ Variables Examples