Identity Operators in SwaziLang
What Are Identity Operators?
You already know how to compare values using comparison operators (>, <, sawa, etc.) and how to combine conditions using logical operators (na, au, !).
But there are some special operators that do unique things in SwaziLang. These are identity operators - they help you check specific relationships or create special syntax patterns.
Quick Reference
| Operator/Keyword | Purpose | Status |
|---|---|---|
sawa | Check equality | Already covered in Comparisons |
sisawa | Check inequality | Already covered in Comparisons |
ni | Syntax sugar for assignments in conditions | We'll cover this now |
katika | Loop membership checking | We'll cover this with loops later |
The ni Keyword - Syntax Sugar for Conditions
The ni keyword is a special operator that lets you assign a condition's result to a variable and use it in the same statement. It's syntax sugar - a shortcut that makes code more readable.
How ni Works
Basic syntax:
kama x ni condition :
// x is available here as kweli or sikweliWhat ni does:
- Evaluates the condition on the right
- Assigns the result (
kweliorsikweli) to the variable on the left - Makes that variable available in the scope of the statement
Simple Example
data age = 20;
// Without ni:
data is_adult = age >= 18;
kama is_adult {
chapisha "Wewe ni mtu mzima";
}
// With ni - cleaner!
kama age ni age >= 18 {
chapisha "Wewe ni mtu mzima";
}Wait, that looks a bit weird because we're using age twice. Let me show a better example:
Better Example: Making a Variable Available
data temperature = 37.5;
// This creates a variable `is_fever` and uses it
kama is_fever ni temperature > 37.5 {
chapisha "Una homa";
}
// Now `is_fever` is available in this block as kweli or sikweliThis is equivalent to:
data temperature = 37.5;
data is_fever = temperature > 37.5;
kama is_fever {
chapisha "Una homa";
}But more concise!
The Power of ni: Multiple Levels
You can nest ni assignments:
data score1 = 85;
data score2 = 90;
kama avg ni (score1 + score2) / 2 {
chapisha `Average: ${avg}`;
}
// Or with a condition:
kama high_average ni ((score1 + score2) / 2) > 80 {
chapisha "Both students performed well";
}Here, the variable high_average gets the result of whether the average is greater than 80.
When to Use ni
Use ni when:
- You want to make the result available in a scope
- You want cleaner, more readable code
- You're checking a result and want to reference it
data password = "mypass";
data min_length = 8;
// Check if password is long enough and use that result
kama password_ok ni password.herufi >= min_length {
chapisha "Password accepted";
}The Flow with ni
kama result ni condition :
// Inside this block, result is kweli or sikweli based on condition
// You can use result here
vinginevyo:
// Here, result is the opposite value
// So if it was kweli above, it's sikweli hereReal-World Examples
Example 1: User Registration
data email = "user@example.com";
data min_age = 18;
data age = 25;
kama email_valid ni email.kuna("@") {
chapisha "Email format valid";
}
kama age_valid ni age >= min_age {
chapisha "Age requirement met";
}Example 2: File Type Checking
data filename = "photo.jpg";
kama ni_image ni filename.huishaNa(".jpg") au
filename.huishaNa(".png") au
filename.huishaNa(".gif") {
chapisha "File is an image";
}
vinginevyo {
chapisha "File is not an image";
}Example 3: Access Control
data user_role = "editor";
kama is_editor ni user_role sawa "editor" {
chapisha "Editor access granted";
}
kama is_admin ni user_role sawa "admin" {
chapisha "Admin access granted";
}Example 4: Range Checking
data score = 85;
kama in_range ni (score >= 0) na (score <= 100) {
chapisha "Score is valid";
}
vinginevyo {
chapisha "Score out of range";
}Understanding the Scope
One important thing about ni: the variable you create is only available within that block:
kama is_valid ni password.herufi > 8 {
chapisha is_valid; // kweli (available here)
}
chapisha is_valid; // Error! is_valid doesn't exist outside the blockIf you need the variable outside the block, create it separately:
data is_valid = password.herufi > 8;
kama is_valid {
chapisha is_valid; // kweli
}
chapisha is_valid; // Still works - exists in outer scopeni with Logical Operators
You can use ni with complex conditions:
data age = 20;
data has_license = kweli;
kama can_drive ni (age >= 18) na has_license {
chapisha "Can drive";
}Future: katika Operator
Note: The katika operator is used to check if a value is "in" a collection (like an array). For example:
// We'll learn this when we study loops and arrays
kwa item katika items {
// Do something with each item
}For now, just know it exists and we'll cover it in detail when we learn about loops and arrays.
Comparison: With and Without ni
Scenario: Checking Login
Without ni:
data username = "hassan";
data password = "pass123";
data username_correct = username sawa "hassan";
data password_correct = password sawa "pass123";
data login_ok = username_correct na password_correct;
kama login_ok {
chapisha "Login successful";
}With ni:
data username = "hassan";
data password = "pass123";
kama login_ok ni (username sawa "hassan") na (password sawa "pass123") {
chapisha "Login successful";
}Both work the same way, but ni is more concise.
Practice Challenges
Challenge 1: Simple Condition with ni
Use ni to check if a number is positive:
Try it first
data number = 42;
kama is_positive ni number > 0 {
chapisha "Number is positive";
}Challenge 2: String Validation with ni
Check if a username is valid (not empty AND at least 3 characters):
Solution
data username = "ali";
kama username_valid ni (username.herufi > 0) na (username.herufi >= 3) {
chapisha "Username is valid";
}Challenge 3: Multiple Conditions
Check if someone can vote (18+) and is registered:
Solution
data age = 20;
data registered = kweli;
kama can_vote ni (age >= 18) na registered {
chapisha "Can vote";
}Challenge 4: Complex Logic with ni
Check if a password is strong: at least 8 chars AND contains a number:
Solution
data password = "MyPass123";
kama is_strong ni (password.herufi >= 8) na
(password.kuna("1") au password.kuna("2") au password.kuna("3")) {
chapisha "Password is strong";
}Important Notes
1. ni Creates a Local Variable
The variable only exists in that specific scope:
kama result ni 5 > 3 {
chapisha result; // Works
}
chapisha result; // Doesn't work2. ni Is Just Syntax Sugar
It's more readable but not required:
// These are equivalent:
kama x ni condition { }
kama condition { }Use ni when it makes your code clearer!
3. Use Parentheses for Complex Conditions
// Clear
kama result ni (a > 5) na (b < 10) {
}
// Confusing
kama result ni a > 5 na b < 10 {
}Reflection Questions
When would
nimake your code more readable? (Hint: when the condition name is meaningful)Why do you think the variable created with
nidoesn't exist outside the block?How is
nidifferent from just assigning to a variable normally? When would you use each?
Some thoughts
When you have a meaningful name like
is_adultorcan_vote, usingnimakes the intent clear right in the condition statement.Because it's scope-limited - it only applies to that specific block. Once the block ends, the decision is made and the variable isn't needed anymore.
nicreates a variable only for that scope, while normal assignment creates it in the outer scope. Usenifor temporary condition checks, normal assignment for variables you need to reference later.
What's Next?
You now understand identity operators, particularly the ni syntax sugar! Next, we'll explore:
- Ternary Operators - Making quick decisions in one line
You're almost through all the operators! After ternary operators, you'll have a complete understanding of how to work with values and make decisions in SwaziLang.
Remember: ni is syntax sugar that makes your conditions more readable by creating a named variable for the result!