Lesson 3 • Beginner

Variables and Data Types

20 minutes
Foundation

📦 What are Variables?

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.

Visual: How Variables Work

Variable Name
age
📦 Box
25
Value Stored
Variable Name
name
📦 Box
"Alice"
Value Stored
Variable Name
isActive
📦 Box
true
Value Stored
Real-World Analogy

Imagine you have a box labeled "age". You can put the number 25 inside it. Later, you can:

  • Look at what's in the box (read the value)
  • Change what's in the box (update the value)
  • Use what's in the box for calculations

That's exactly how variables work in programming!

🔤 Declaring Variables

iPseudo offers three flexible ways to declare variables:

Style 1: Using var Keyword (Recommended)

Example: Traditional var Style
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
Output
Name: Alice Age: 20 GPA: 3.75 Enrolled: true

Style 2: Using Variable Keyword

Algorithm VariableKeywordExample

Variable productName = "Laptop"
Variable productPrice = 999.99
Variable inStock = true

Print productName, "costs $", productPrice

Endalgorithm

Style 3: Using 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
Which Style to Use?
  • var - Quick and simple, great for most cases
  • Variable - More readable, self-documenting
  • Declare As - Explicit types, professional style
All three styles work perfectly. Choose what feels most comfortable to you!

Data Types

Data types define what kind of data a variable can hold. iPseudo supports several fundamental types:

Data Types at a Glance

🔢
Integer
Whole Numbers
42, -17, 0, 1000
✓ Counting, ages, years
💰
Float
Decimal Numbers
3.14, 19.99, -2.5
✓ Prices, measurements
📝
String
Text Data
"Hello", "Alice"
✓ Names, messages, text
🔘
Boolean
True/False
true, false
✓ Yes/no, on/off, flags

1. Integer (Whole Numbers)

Integers are whole numbers without decimal points.

Integer Examples
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:

  • Counting items
  • Age, year, day numbers
  • Array indices
  • Loop counters

2. Float (Decimal Numbers)

Floats are numbers with decimal points, used for precise calculations.

Float Examples
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:

  • Money and prices
  • Measurements and distances
  • Scientific calculations
  • Percentages and ratios

3. String (Text)

Strings are sequences of characters enclosed in quotes.

String Examples
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
Output
Full Name: John Smith Email: john@example.com Hello, World!

Use strings for:

  • Names and text
  • Messages and descriptions
  • Email addresses and URLs
  • Any textual data

4. Boolean (True/False)

Booleans have only two possible values: true or false.

Boolean Examples
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

Visual: Boolean States

true
✅ Yes, On, Enabled, Active
false
❌ No, Off, Disabled, Inactive

Use booleans for:

  • Yes/No questions
  • On/Off states
  • Condition results
  • Flags and switches

🔄 Updating Variables

Once created, you can change a variable's value:

Updating Variables
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
Output
Initial score: 0 After first update: 10 After second update: 25 After increment: 30

Visual: Variable Lifecycle

📦
1. CREATE
score = 0
Initialize variable
🔄
2. UPDATE
score = 10
Change value
3. USE
Print score
Read & display

Variable Naming Rules

✅ Valid Variable Names:

  • age - Simple lowercase
  • studentName - camelCase (recommended)
  • first_name - snake_case
  • age2 - Can include numbers
  • totalScore - Descriptive

❌ Invalid Variable Names:

  • 2age - Can't start with number
  • student-name - No hyphens
  • first name - No spaces
  • total$ - No special characters
Best Practice: Descriptive Names
Use meaningful names that describe what the variable represents:
  • studentAge is better than a
  • totalPrice is better than tp
  • isActive is better than flag

Real-World Example

Complete Example: Shopping Cart
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
Output
================================ SHOPPING RECEIPT ================================ Product: Laptop Price: 999.99 Quantity: 2 -------------------------------- Subtotal: $1999.98 Tax: $159.9984 ================================ TOTAL: $2159.9784 ================================

📋 Quick Reference

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

Visual: How Variables Are Stored in Memory

💾 Computer Memory
studentName
"Alice"
String
studentAge
20
Integer
studentGPA
3.75
Float
isEnrolled
true
Boolean
Memory Concept:
Each variable gets its own space in memory with a name (address) and a value!

Key Takeaways

  • ✅ Variables store data that your program can use and modify
  • ✅ Use var, Variable, or Declare As to create variables
  • ✅ Choose the right data type for your data (Integer, Float, String, Boolean)
  • ✅ Use descriptive names that explain what the variable represents
  • ✅ Variables can be updated throughout your program
  • ✅ Follow naming rules (no spaces, no special characters, can't start with numbers)

💪 Practice Exercises

Try These Exercises

Exercise 1: Personal Profile
Create variables for your name, age, city, and favorite hobby. Display them in a formatted profile.
Exercise 2: Temperature Converter
Create a variable for temperature in Celsius. Convert it to Fahrenheit using the formula: F = C × 9/5 + 32. Display both values.
Exercise 3: Circle Calculator
Use a variable for radius. Calculate and display the circle's area (π × r²) and circumference (2 × π × r). Use 3.14159 for pi.
Exercise 4: Restaurant Bill
Create variables for meal price, tip percentage (15%), and tax rate (8%). Calculate and display the subtotal, tip, tax, and final total.
Great Progress!
You now understand how to store and work with data using variables! In the next lesson, we'll learn how to get input from users and make your programs interactive.