Skip to content

Variables and Assignment

Variables are named storage locations that hold values in your program. They’re fundamental to any programming task.

Simply assign a value to create a variable:

name = "Alice"
age = 30
price = 19.99
isActive = true

Optionally specify the variable type:

number age = 30
string name = "Alice"
boolean isActive = true
array numbers = [1, 2, 3]

Explicit types make your code more readable and help prevent type-related errors.

Declare a variable without initializing it:

define counter
define userName
define isValid
counter = 0
userName = "John"
isValid = true

This is useful when you need to declare a variable before you know its value.

// Good use case: variable will be set conditionally
define message
if score >= 90
message = "Excellent!"
else if score >= 70
message = "Good job!"
else
message = "Keep trying!"
output message
x = 5
name = "Bob"
isComplete = false

Variables can be assigned the value of other variables:

original = 42
copy = original // copy now equals 42
original = 100 // changing original doesn't affect copy
output copy // still 42
a = 10
b = 20
c = 30
// Swap values using a temporary variable
temp = a
a = b
b = temp
// Now a = 20 and b = 10
age = 25
firstName = "John"
user_name = "alice123"
totalPrice = 99.99
isValid = true
count2 = 5

Camel Case (recommended):

firstName = "Alice"
totalPrice = 99.99
isLoggedIn = true
studentCount = 30

Snake Case:

first_name = "Alice"
total_price = 99.99
is_logged_in = true
student_count = 30

Best Practices:

  • Use descriptive names: studentCount instead of sc
  • Start with a lowercase letter
  • Use camelCase or snake_case consistently
  • Avoid single-letter names except for loop counters

Variables must follow specific naming rules. Here are common mistakes to avoid:

Starting with numbers:

2ndPlace = "Alice" // Invalid - starts with number
1stStudent = "Bob" // Invalid - starts with number
99problems = "Jay-Z" // Invalid - starts with number

Using kebab-case (hyphens):

user-name = "Bob" // Invalid - hyphens not allowed
first-name = "Alice" // Invalid - hyphens not allowed
total-price = 99.99 // Invalid - hyphens not allowed
is-valid = true // Invalid - hyphens not allowed

Using dots or other special characters:

first.name = "Charlie" // Invalid - dots not allowed
user@name = "Dave" // Invalid - @ not allowed
total$ = 100 // Invalid - $ not allowed
price% = 0.15 // Invalid - % not allowed
user#id = 123 // Invalid - # not allowed

Spaces in names:

first name = "Alice" // Invalid - spaces not allowed
total price = 99.99 // Invalid - spaces not allowed
user count = 10 // Invalid - spaces not allowed

Starting with special characters:

_private = "secret" // Valid, but discouraged
$value = 100 // Invalid - $ not allowed
#count = 5 // Invalid - # not allowed
@user = "alice" // Invalid - @ not allowed

Reserved keywords:

if = 5 // Invalid - 'if' is a keyword
while = 10 // Invalid - 'while' is a keyword
for = 3 // Invalid - 'for' is a keyword
function = "test" // Invalid - 'function' is a keyword
return = true // Invalid - 'return' is a keyword

Remember: Use camelCase or snake_case, start with a letter, and avoid special characters (except underscore).

Variables defined inside functions or blocks:

function calculateTotal takes in price and quantity {
subtotal = price * quantity // Local to this function
tax = subtotal * 0.1 // Local to this function
total = subtotal + tax // Local to this function
return total
}
// subtotal, tax, and total don't exist outside the function
// Counter variable
count = 0
count = count + 1
count++ // Same as above
// Accumulator variable
sum = 0
for i from 1 to 10
sum = sum + i
// Flag variable
found = false
for each item in items
if item equals target
found = true
jump

Variables can be reassigned to new values:

score = 0
output "Score: {score}" // 0
score = 10
output "Score: {score}" // 10
score = score + 5
output "Score: {score}" // 15
score++
output "Score: {score}" // 16

Variables can hold different types at different times:

value = 42 // number
output value // 42
value = "hello" // now a string
output value // "hello"
value = true // now a boolean
output value // true

Note: While this is allowed, it’s generally better to keep variables with consistent types for clarity.

Shorthand for updating variables:

counter = 10
counter += 5 // counter = counter + 5 (15)
counter -= 3 // counter = counter - 3 (12)
counter *= 2 // counter = counter * 2 (24)
counter /= 4 // counter = counter / 4 (6)
counter %= 4 // counter = counter % 4 (2)
counter ^= 3 // counter = counter ^ 3 (8)
count = 5
count++ // count = count + 1 (6)
count-- // count = count - 1 (5)
// Useful in loops
for i from 0 to 10
sum += i
count++
// Good: initialized
sum = 0
count = 0
message = ""
isReady = false
// Risky: uninitialized
define total
total = total + 10 // Error if total has no initial value
// Numbers: start at 0
count = 0
sum = 0
// Strings: start as empty
message = ""
name = ""
// Booleans: start with explicit value
isActive = false
isComplete = false
// Arrays: start empty
items = []
numbers = []
a = 10
b = 20
temp = a
a = b
b = temp
output "a = {a}, b = {b}" // a = 20, b = 10
total = 0
prices = [19.99, 29.99, 9.99, 39.99]
for each price in prices
total += price
output "Total: ${total}"
positiveCount = 0
numbers = [5, -3, 8, -1, 12, -7]
for each num in numbers
if num > 0
positiveCount++
output "Positive numbers: {positiveCount}"
hasErrors = false
values = [5, 10, -3, 20]
for each value in values
if value < 0
hasErrors = true
jump
if hasErrors
output "Error: negative values found"
numbers = [23, 45, 12, 67, 34]
max = numbers[0]
min = numbers[0]
for each num in numbers
if num > max
max = num
if num < min
min = num
output "Max: {max}, Min: {min}"
output "Enter temperature in Fahrenheit: "
input fahrenheit
fahrenheit = convert fahrenheit to number
celsius = (fahrenheit - 32) * 5 / 9
output "{fahrenheit}°F = {celsius}°C"
output "Enter three test scores:"
input score1
input score2
input score3
score1 = convert score1 to number
score2 = convert score2 to number
score3 = convert score3 to number
total = score1 + score2 + score3
average = total / 3
output "Average: {average}"
if average >= 90
output "Grade: A"
else if average >= 80
output "Grade: B"
else if average >= 70
output "Grade: C"
else
output "Grade: F"
output "Principal amount: "
input principal
principal = convert principal to number
output "Interest rate (as decimal, e.g., 0.05 for 5%): "
input rate
rate = convert rate to number
output "Time in years: "
input time
time = convert time to number
interest = principal * rate * time
total = principal + interest
output "Interest: ${interest}"
output "Total amount: ${total}"
  1. Use meaningful names

    // Bad
    x = 30
    // Good
    studentAge = 30
  2. Initialize before use

    // Bad
    define total
    total = total + 10 // Error!
    // Good
    total = 0
    total = total + 10
  3. Keep scope minimal

    // Only declare variables where needed
    for i from 1 to 10
    temp = i * 2 // temp only exists in loop
    output temp
  4. Use constants for fixed values

    constant TAX_RATE = 0.08
    constant MAX_USERS = 100
    price = 50
    tax = price * TAX_RATE
  5. Be consistent with naming

    // Pick one style and stick with it
    firstName = "Alice" // camelCase
    lastName = "Smith" // camelCase
    totalPrice = 99.99 // camelCase