Programming Basics
Introduction to Programming
Programming is the process of creating a set of instructions that tell a computer how to perform a task. These instructions are called programs, and they are written in special languages called programming languages.
Key Programming Concepts
- Sequence - Instructions executed in order
- Selection - Making decisions in code
- Iteration - Repeating actions
- Variables - Storing and manipulating data
- Functions - Reusable blocks of code
Variables and Data Types
Variables store data that can change during program execution. Spindle supports different data types:
# Numbers (integers and decimals)
score <- 95
temperature <- 98.6
DISPLAY("Score: ")
DISPLAY(score)
DISPLAY("Temperature: ")
DISPLAY(temperature)
# Strings (text)
name <- "Alice"
message <- "Hello World"
DISPLAY("Name: ")
DISPLAY(name)
DISPLAY("Message: ")
DISPLAY(message)
# Booleans (true/false)
is_student <- 1
has_passed <- 0
DISPLAY("Is student: ")
DISPLAY(is_student)
DISPLAY("Has passed: ")
DISPLAY(has_passed)
# Lists (arrays)
numbers <- [1, 2, 3, 4, 5]
names <- ["Alice", "Bob", "Charlie"]
DISPLAY("Numbers: ")
DISPLAY(numbers)
DISPLAY("Names: ")
DISPLAY(names)
Control Structures
Control structures determine the flow of your program. They allow you to make decisions and repeat actions.
If Statements (Selection)
If statements allow your program to make decisions based on conditions. They check if something is true and execute different code accordingly.
Basic If Statement
The simplest form checks one condition and executes code if it's true.
# Basic if statement
age <- 20
IF (age >= 18) {
DISPLAY("You are an adult.")
}
If-Else Statement
If-else statements provide an alternative when the condition is false.
# If-else statement
age <- 16
IF (age >= 18) {
DISPLAY("You are an adult.")
} ELSE {
DISPLAY("You are a minor.")
}
Multiple Conditions
You can check multiple conditions using ELSE IF statements.
# Multiple conditions
score <- 85
IF (score >= 90) {
grade <- "A"
} ELSE IF (score >= 80) {
grade <- "B"
} ELSE IF (score >= 70) {
grade <- "C"
} ELSE {
grade <- "F"
}
DISPLAY("Grade: " + grade)
Loops (Iteration)
Loops allow you to repeat a block of code multiple times. This is essential for processing lists and performing repetitive tasks.
For Loops (REPEAT)
For loops repeat a specific number of times. In Spindle, we use the REPEAT statement.
# Counting from 1 to 5
REPEAT 5 TIMES {
DISPLAY(i)
}
Looping Through Lists
You can use loops to process each item in a list.
# Looping through a list
fruits <- ["apple", "banana", "orange"]
i <- 1
REPEAT LENGTH(fruits) TIMES {
DISPLAY(fruits[i])
i <- i + 1
}
While Loops (REPEAT UNTIL)
While loops continue until a condition becomes false.
# Counting down from 5
count <- 5
REPEAT UNTIL (count <= 0) {
DISPLAY(count)
count <- count - 1
}
Functions (Procedures)
Functions are reusable blocks of code that perform specific tasks. They help organize your code and avoid repetition.
Creating Functions
In Spindle, we use the PROCEDURE keyword to define functions.
# Simple function with no parameters
PROCEDURE GREET() {
DISPLAY("Hello, World!")
}
# Calling the function
GREET()
Functions with Parameters
Functions can accept input values called parameters.
# Function with parameters
PROCEDURE GREET_PERSON(name) {
DISPLAY("Hello, " + name + "!")
}
# Calling the function with different names
GREET_PERSON("Alice")
GREET_PERSON("Bob")
Functions with Return Values
Functions can return values using the RETURN statement.
# Function that returns a value
PROCEDURE ADD_NUMBERS(a, b) {
result <- a + b
RETURN result
}
# Using the returned value
sum <- ADD_NUMBERS(5, 3)
DISPLAY("Sum: " + sum)
Common Programming Patterns
Programming patterns are common solutions to recurring problems. Understanding these patterns helps you write better code.
Counter Pattern
The counter pattern is used to count occurrences of something in a list.
# Counting occurrences of a number
numbers <- [1, 2, 2, 3, 2, 4, 5]
count <- 0
i <- 1
REPEAT LENGTH(numbers) TIMES {
IF (numbers[i] == 2) {
count <- count + 1
}
i <- i + 1
}
DISPLAY("Number 2 appears " + count + " times")
Accumulator Pattern
The accumulator pattern is used to build up a value by adding to it in a loop.
# Summing even numbers
numbers <- [1, 2, 3, 4, 5, 6]
sum_even <- 0
i <- 1
REPEAT LENGTH(numbers) TIMES {
IF ((numbers[i] % 2) == 0) {
sum_even <- sum_even + numbers[i]
}
i <- i + 1
}
DISPLAY("Sum of even numbers: " + sum_even)
Search Pattern
The search pattern is used to find a specific item in a list.
# Finding an element in a list
names <- ["Alice", "Bob", "Charlie"]
search_name <- "Bob"
found <- 0
i <- 1
REPEAT LENGTH(names) TIMES {
IF (names[i] == search_name) {
found <- 1
BREAK
}
i <- i + 1
}
IF (found == 1) {
DISPLAY("Name " + search_name + " found")
} ELSE {
DISPLAY("Name " + search_name + " not found")
}
Debugging Techniques
Debugging is the process of finding and fixing errors in your code. Here are some common strategies.
Using DISPLAY for Debugging
Adding DISPLAY statements helps you see what's happening in your program.
PROCEDURE CALCULATE_AVERAGE(scores) {
# Debug: Show input
DISPLAY("Input scores: " + scores)
IF (LENGTH(scores) == 0) {
DISPLAY("Warning: Empty score list")
RETURN 0
}
total <- 0
i <- 1
REPEAT LENGTH(scores) TIMES {
total <- total + scores[i]
i <- i + 1
}
# Debug: Show total
DISPLAY("Sum of scores: " + total)
average <- total / LENGTH(scores)
DISPLAY("Calculated average: " + average)
RETURN average
}
# Test the function
test_scores <- [85, 90, 78, 92]
result <- CALCULATE_AVERAGE(test_scores)
Common Debugging Strategies
- Add DISPLAY statements to track variable values
- Check for off-by-one errors in loops
- Verify correct data types are being used
- Test edge cases (empty lists, zero, negative numbers)
- Use meaningful variable names
- Break complex problems into smaller parts
Practice Exercises
Practice is essential for learning programming. Try these exercises to reinforce your understanding.
Exercise 1: Temperature Converter
Write a function that converts Celsius to Fahrenheit using the formula: F = (C × 9/5) + 32
PROCEDURE CELSIUS_TO_FAHRENHEIT(celsius) {
fahrenheit <- (celsius * 9/5) + 32
RETURN fahrenheit
}
# Test the function
temperatures <- [0, 100, 37]
i <- 1
REPEAT LENGTH(temperatures) TIMES {
temp <- temperatures[i]
fahrenheit <- CELSIUS_TO_FAHRENHEIT(temp)
DISPLAY(temp + "°C = " + fahrenheit + "°F")
i <- i + 1
}
Exercise 2: List Average
Write a function that finds the average of numbers in a list.
PROCEDURE CALCULATE_AVERAGE(numbers) {
IF (LENGTH(numbers) == 0) {
RETURN 0
}
total <- 0
i <- 1
REPEAT LENGTH(numbers) TIMES {
total <- total + numbers[i]
i <- i + 1
}
RETURN total / LENGTH(numbers)
}
# Test the function
test_numbers <- [1, 2, 3, 4, 5]
average <- CALCULATE_AVERAGE(test_numbers)
DISPLAY("Average: " + average)
Exercise 3: Vowel Counter
Write a function that counts vowels in a string.
PROCEDURE COUNT_VOWELS(text) {
vowels <- "aeiouAEIOU"
count <- 0
i <- 1
REPEAT LENGTH(text) TIMES {
char <- text[i]
j <- 1
REPEAT LENGTH(vowels) TIMES {
IF (char == vowels[j]) {
count <- count + 1
BREAK
}
j <- j + 1
}
i <- i + 1
}
RETURN count
}
# Test the function
test_string <- "Hello World"
vowel_count <- COUNT_VOWELS(test_string)
DISPLAY("Vowels in '" + test_string + "': " + vowel_count)