bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Programming/Programming Concepts
Programming•Programming Concepts

Variables in Programming

Variables are one of the most basic and essential concepts in programming, used to store values.

What is a Variable?

A variable has a name, and you can store something in it.

The image below shows how we can think of a variable named favFruit , with the value 'apple' stored inside it.

Below is how we can create the favFruit variable, using Python code:

The code above creates a variable named favFruit , and the equal sign = is used to store the value 'apple' inside the variable.

The reason for giving a variable a name is to be able to use it later in the code, and also to know what value it holds.

Creating a Variable

Below is the code for creating the favAnimal variable in different programming languages.

Runnable example

favAnimal = 'turtles'

Variables can hold different types of data, like whole numbers, decimal numbers, or text.

Note

When creating a variable in programming languages like C/C++ and Java, we must tell the computer what type of data the variable holds. To do that we need to write for example int in front of the variable name, if the variable holds a whole number (integer).

Doing Things with Variables

Like we have just seen in the previous example, a value can be stored in a variable. And if you run the example code above, you see how a variable is printed.

We can do other things with variables as well, like math operations, or put variables with text strings together.

Add a Variable to a String

To use a variable in a string, you can add it to the string, like this:

Runnable example

a = 'Jane'
print('Hello, my name is ' + a)

Add Two String Variables Together

You can add two string variables together to form a sentence, using the + operator, like this:

Runnable example

a = 'Jane'
b = 'My name is '
print(b + a)

Add Two Number Variables

If the variables are numeric values, you can perform mathematic operations on them, like adding two numbers:

Runnable example

a = 2
b = 3
print(a + b)

or other mathematical operations, like division:

Runnable example

a = 12
b = 3
print(a / b)

Another way to add two variables, is to make an extra variable c to hold the sum, and present the answer with a text string:

Runnable example

a = 2
b = 3
c = a + b
print('The sum a + b is ' + str(c))

Note

The + operator is used to both add numbers, and to put strings together. In Python and C++ we need to convert a number to a string before we can put it together with a string.

Incrementing a Variable

We can create a variable, and update the value by adding 1 to it, like this:

Runnable example

a = 2
a = a + 1
print(a)

Incrementing a variable is a common operation in programming, and it's often used in loops. It is so common in fact, that many programming languages have a shorthand for it, like ++ in C/C++ and Java, or += in Python.

The code below shows how to increment a variable in different programming languages, using shorthand.

Runnable example

a = 2
a += 1
print(a)

Decrementing a Variable

If we want to decrement a variable, we can do that in a similar way as incrementing. And the number we want to decrement by can be any number, not just 1.

The code below shows how to decrement a variable by 3 in different programming languages, using shorthand.

Runnable example

a = 5
a -= 3
print(a)

Using a Variable in an if Statement

We can use a variable in an if statement , as part of the condition, like this:

Runnable example

temperature = 25
print('Temperature: ' + str(temperature) + '�C')

if temperature > 20:
  print('It is warm')
else:
  print('It is not warm')

The Variable Name

There are certain rules that apply when naming a variable. Some rules are programming-language specific, others apply to all programming languages:

  • A variable name cannot contain spaces.
  • A variable name cannot start with a number.
  • A variable name cannot be a reserved word like if, else, for, function etc.

For readability, it is common to use camelCase or snake_case when naming variables, so instead of myfavanimal , we can use myFavAnimal or my_fav_animal .

Previous

What is Programming ?

Next

Constants in Programming

This chapter

Overview
25

Lessons

105m

Read time

1. Understanding The Concepts of Programming2. What is Programming ?3. Variables in Programming4. Constants in Programming5. If Statements in Programming6. Arrays in Programming7. Loops in Programming8. Functions in Programming9. Recursion in Programming10. Scope in Programming11. Strings in Programming12. Data Types in Programming13. Type Casting in Programming14. Operators in Programming15. Arithmetic Operators in Programming16. Assignment Operators in Programming17. Comparison Operators in Programming18. Logical Operators in Programming19. Bitwise Operators in Programming20. Comments in Programming21. Input and Output in Programming22. Bits and Bytes in Programming23. Binary Numbers in Programming24. Hexadecimal Numbers in Programming25. Boolean Algebra

On this page

What is a Variable?Creating a VariableDoing Things with VariablesAdd a Variable to a StringAdd Two String Variables TogetherAdd Two Number VariablesIncrementing a VariableDecrementing a VariableUsing a Variable in an if StatementThe Variable Name