Statements in SwaziLang
What is a Statement?
A statement is a single instruction that tells the computer to perform a specific action. Think of statements as sentences in a programming language - each statement is a complete instruction that does something.
Just like how you give someone instructions step by step ("Open the door", "Turn on the light", "Sit down"), a computer program is made up of statements that execute one after another.
Basic Statement Structure
In SwaziLang, statements are written one per line, and each statement performs one action. Here's what makes a statement:
- It's a complete instruction
- It performs a specific action
- It usually ends at the end of the line
- Statements are executed in order, from top to bottom
And Remember I said one statement per line, didn't mean you should always write a statement in a single line, No, You can write a statement in multiple lines, well you can and you should especially those involving blocks of other statements. You will see more in examples later.
Types of Statements
1. Declaration Statements
These statements create, or intruduce something new to be stored.
eg.
data jina = "John"
data umri = 252. Assignment Statements
These statements change the value of existing variables(declared variables).
jina = "Mary"
umri = 303. Expression Statements
These statements perform calculations or operations.
jumla = 10 + 20
matokeo = umri * 24. Output Statements
These statements display information to the user.
chapisha("Hello, World!")
chapisha(jina)5. Control Flow Statements
These statements control how the program runs (we'll learn more about these later).
kama umri > 18 {
chapisha("You are an adult")
}Simple Examples
Let's look at a program with multiple statements:
// This is a simple program with several statements
data jina = "Amina" // Statement 1: Declare a variable
data umri = 22 // Statement 2: Declare another variable
chapisha("Habari!") // Statement 3: Print a greeting
chapisha(jina) // Statement 4: Print the name
chapisha("Una miaka:") // Statement 5: Print text
chapisha(umri) // Statement 6: Print the ageOutput:
Habari!
Amina
Una miaka:
22Each line is a statement that executes one after another, from top to bottom.
Statement vs Expression
It's important to understand the difference:
Statement: A complete instruction that performs an action
- Example:
data x = 5
- Example:
Expression: A piece of code that produces a value
- Example:
5 + 3(produces 8)
- Example:
An expression can be part of a statement:
data jumla = 10 + 5 // "10 + 5" is an expression, the whole line is a statementThis is like telling the computer, "Hey 10 + 5 resolves to something(expression) take that and store it in variable called jumla" — That sentences is statement, an instruction.
So in short a quick note, An expression prodeuces a value, while a statement gives an action.
Key Points to Remember
- One action at a time: Each statement does one thing
- Order matters: Statements execute from top to bottom
- Clarity is important: Write clear statements that are easy to understand
- Build complexity gradually: Combine simple statements to create complex programs
Practice Exercise
Try writing a program with these statements:
- Create a variable for your name
- Create a variable for your favorite number
- Print a greeting
- Print your name
- Print your favorite number
// Your code here
data jina = "Your Name"
data nambari = 7
chapisha "Habari! Jina langu ni:"
chapisha jina
chapisha "Nambari yangu favorite ni:"
chapisha nambariWhat's Next?
Now that you understand statements, you're ready to learn about:
- Variables and data types in detail
- Different types of expressions
- Control flow statements (conditions and loops)
- Functions and more complex program structures
Remember: Every program is just a collection of statements working together to accomplish a task!