Introducing Variables

Learn about the integer variable, the assignment operator, and the memory allocation.

In this lesson, we will learn about the concept of variables.

Table printing: Why use variables?

In the printing table example (from the previous lesson), we saw that we had to replace all the numbers explicitly with the number whose table we wanted to print. So if we wrote the code to print the table of 34 and then later we wanted to print the table of 67, we had to replace the number 34 with 67 throughout our code.

This is where variables come in and help make everything super easy for us. We can simply store the number in a variable and use that variable in our code. So when we want to print the table of a different number, we simply change the variable’s value. Let’s see how we can create, update and use variables in C++.


What is a variable?

A variable is a named location in the computer’s memory that stores some value. We can fetch or update the value by using the variable’s name.

For example, we create a variable named num in which 72 is stored, as shown in the image below.

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy