Variables are containers that store data in your programs. Think of them as labeled boxes where you can put values and retrieve them later. Variables are fundamental to programming because they allow your programs to work with data dynamically.
Imagine you have a box labeled "age". You can put the number 25 inside it. Later, you can:
That's exactly how variables work in programming!
iPseudo offers three flexible ways to declare variables:
var
Keyword (Recommended)Algorithm VarExample
var studentName = "Alice"
var studentAge = 20
var studentGPA = 3.75
var isEnrolled = true
Print "Name:", studentName
Print "Age:", studentAge
Print "GPA:", studentGPA
Print "Enrolled:", isEnrolled
Endalgorithm
Variable
KeywordAlgorithm VariableKeywordExample
Variable productName = "Laptop"
Variable productPrice = 999.99
Variable inStock = true
Print productName, "costs $", productPrice
Endalgorithm
Declare As
(Typed)Algorithm TypedVariables
Declare name As String
Declare age As Integer
Declare salary As Float
Set "Bob" To name
Set 30 To age
Set 50000.50 To salary
Print name, "is", age, "years old"
Endalgorithm
Data types define what kind of data a variable can hold. iPseudo supports several fundamental types:
Integers are whole numbers without decimal points.
Algorithm IntegerExample
var age = 25
var temperature = -5
var studentCount = 150
var year = 2025
Print "Age:", age
Print "Temperature:", temperature, "°C"
Print "Students:", studentCount
Print "Year:", year
Endalgorithm
Use integers for:
Floats are numbers with decimal points, used for precise calculations.
Algorithm FloatExample
var price = 19.99
var temperature = 98.6
var pi = 3.14159
var percentage = 87.5
Print "Price: $", price
Print "Body temp:", temperature, "°F"
Print "Pi ≈", pi
Print "Score:", percentage, "%"
Endalgorithm
Use floats for:
Strings are sequences of characters enclosed in quotes.
Algorithm StringExample
var firstName = "John"
var lastName = "Smith"
var email = "john@example.com"
var message = "Hello, World!"
# String concatenation
var fullName = firstName + " " + lastName
Print "Full Name:", fullName
Print "Email:", email
Print message
Endalgorithm
Use strings for:
Booleans have only two possible values: true
or false
.
Algorithm BooleanExample
var isStudent = true
var hasLicense = false
var isAdult = true
Print "Is student:", isStudent
Print "Has license:", hasLicense
Print "Is adult:", isAdult
# Booleans from comparisons
var age = 20
var canVote = age >= 18
Print "Can vote:", canVote
Endalgorithm
Use booleans for:
Once created, you can change a variable's value:
Algorithm UpdateVariables
# Create variable
var score = 0
Print "Initial score:", score
# Update variable
score = 10
Print "After first update:", score
# Update again
score = 25
Print "After second update:", score
# Increment variable
score = score + 5
Print "After increment:", score
Endalgorithm
age
- Simple lowercasestudentName
- camelCase (recommended)first_name
- snake_caseage2
- Can include numberstotalScore
- Descriptive2age
- Can't start with numberstudent-name
- No hyphensfirst name
- No spacestotal$
- No special charactersstudentAge
is better than a
totalPrice
is better than tp
isActive
is better than flag
Algorithm ShoppingCart
# Product details
var productName = "Laptop"
var productPrice = 999.99
var quantity = 2
var taxRate = 0.08
# Calculate totals
var subtotal = productPrice * quantity
var tax = subtotal * taxRate
var total = subtotal + tax
# Display receipt
Print "================================"
Print " SHOPPING RECEIPT"
Print "================================"
Print "Product:", productName
Print "Price:", productPrice
Print "Quantity:", quantity
Print "--------------------------------"
Print "Subtotal: $", subtotal
Print "Tax: $", tax
Print "================================"
Print "TOTAL: $", total
Print "================================"
Endalgorithm
Data Type | Example Values | Use Cases |
---|---|---|
Integer | 42 , -10 , 0 |
Counting, age, years |
Float | 3.14 , 99.99 , -2.5 |
Prices, measurements |
String | "Hello" , "John" |
Names, text, messages |
Boolean | true , false |
Yes/no, on/off states |
var
, Variable
, or Declare As
to create variables