A named container in a computer program where you can store, retrieve and change a value.
Details
Variables are like labeled boxes: you can store something inside, check what’s there, and update it. They make programs flexible by allowing you to work with data that isn’t known ahead of time.
Consider keeping track of a player’s score:
🤖 Pseudocode
score = 0 # store the initial value score = 10 # change the value "Current Score:" score # read the current value
A variable creates a space in memory called score to hold the player’s score value. This allows the game to update the score as it changes and display it to the player.
Before you can use a variable, you must first declare a variable in your program. You can also initialize it with a starting value, and once declared, you can reference it anywhere in your code to read or update its value.
Naming variables is surprisingly hard — and very important. A variable name is more than just a label; it communicates the purpose of the value stored in memory.
Poorly chosen names can make code confusing, difficult to read, and hard to maintain, especially in larger programs. Good variable names help both you and others understand what the code is doing at a glance.
Check Related Actions for details on how to perform different actions with variables.
Related Concepts
- Has: Data Type
- Used in: Variable Declaration
- Used in: Naming Variables
Related Actions
- Used in: Perform Arithmetic Operation
- Used in: Check Odd or Even
- Used in: Assign a Value to a Variable
- Used in: Declare a Variable
- Used in: Increment or Decrement a Variable
- Used in: Print a Variable