Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Variable Declaration

From Groundhog Learning

Creating a named container in memory to store a value.

Details

To use a variable, you must first declare it. Declaring a variable tells the program to reserve memory to store a value and gives it a human-friendly name so it’s easy to reference, update, and understand within your code.

In many languages, the declaration also specifies the type of data the variable can hold, such as numbers, text, or logical values. However, not all languages require specifying the type—some allow you to create variables without declaring a type explicitly.

🤖 Pseudocode

score = 0       # example: declares a variable named 'score' with initial value 0

While the exact syntax varies between languages, the underlying concept is the same: creating a named space in memory to store information your program will use.

Referencing

Once a variable is declared, you can reference it by its name to read or update its value.

🤖 Pseudocode

score = 100         # assign value
score = score + 10  # update value
print(score)        # read/display value

Variable names are usually case-sensitive and must match the declared name exactly.

Initialization

A variable can be initialized at the time of declaration by assigning it an initial value. Initialization ensures the variable has a defined value before it is used.

🤖 Pseudocode

playerScore = 0        # initialized with 0
username = "Ze"        # initialized with text
isAlive = True         # initialized with a boolean

ℹ️ Initialization is optional in some languages, but using it helps prevent errors caused by undefined values.

Related Concepts

Related Actions