Introduction to Variables

In this lesson we'll get acquainted with variables in C++. Variables are the basic building blocks of every program, because they allow us to store and work with data.

Imagine a variable as a box where you can store some value:

  • The box has its name (variable name)
  • It can contain a certain type of value (number, text, ...)
  • We can change the value in the box at any time
  • We can look at what the box contains at any time

For example in mathematics we use variables like x or y. In programming it's similar, but we can give them any meaningful name:

int age = 25;           // Variable for age
double height = 180.5;  // Variable for height
string name = "Karel";  // Variable for name

Variables allow us to:

  • Store calculation results
  • Remember user input
  • Track program state
  • Work with data repeatedly

In the following exercises we'll learn how to create variables, change their values, and work with different data types.

Instructions

Look at the sample code and its output. When you're ready, continue to the next exercise where we'll start working with variables!

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/6

Introduction to Variables | Start Coder