Waxaan qoraynaa oon dib uhabeyn ku wadnaa docs oo af-soomaali ku qoran, ka qeybqaadasho raac lifaaqaan...Contribution guidelines

Conditional Statements in Soplang

Conditional statements allow your program to make decisions based on conditions. Soplang provides intuitive Somali-first keywords for conditional logic.

If Statements (haddii)

The haddii statement executes a block of code if a specified condition is true. The keyword haddii means "if" in Somali.

if_statements.sop
// Basic if statement
door da = 18

haddii da >= 18 {
    qor("Waa qof weyn")
}  // Prints: "Waa qof weyn" ("Is an adult")

// If statement with multiple conditions
door qiime = 85

haddii qiime >= 60 && qiime < 90 {
    qor("Waa dhexdhexaad")
}  // Prints: "Waa dhexdhexaad" ("Is average")

If-Else Statements (haddii-kale)

The haddii-kale statement executes one block of code if a condition is true and another block if the condition is false. The keyword kale means "else" in Somali.

if_else_statements.sop
// If-else statement
door qiime = 45

haddii qiime >= 50 {
    qor("Waa guul")
} kale {
    qor("Waa dhicin")
}  // Prints: "Waa dhicin" ("Failed")

// If-else with boolean condition
door xaqiiqo = run  // run means true

haddii xaqiiqo {
    qor("Waa run")
} kale {
    qor("Waa been")
}  // Prints: "Waa run" ("Is true")

Else-If Statements (haddii-kale-haddii)

The haddii-kale-haddii statement allows you to test multiple conditions in sequence. In Soplang, this is written as haddii ... kale haddii ... kale.

else_if_statements.sop
// Else-if statement
door natiijo = 78

haddii natiijo >= 90 {
    qor("Darajada: A")
} kale haddii natiijo >= 80 {
    qor("Darajada: B")
} kale haddii natiijo >= 70 {
    qor("Darajada: C")
} kale haddii natiijo >= 60 {
    qor("Darajada: D")
} kale {
    qor("Darajada: F")
}  // Prints: "Darajada: C" ("Grade: C")

Switch Statements (doorasho)

The doorasho statement evaluates an expression, matching the expression's value to a xaalad (case) clause, and executes the associated code. The keyworddoorasho means "choice" in Somali.

switch_statements.sop
// Switch statement
door maalin = "Isniin"

doorasho maalin {
  xaalad "Isniin":
      qor("Waa maalinta koowaad ee toddobaadka")
      jebi
  xaalad "Talaado":
      qor("Waa maalinta labaad ee toddobaadka")
      jebi
  xaalad "Arbaco":
      qor("Waa maalinta saddexaad ee toddobaadka")
      jebi
  xaalad "Khamiis":
      qor("Waa maalinta afraad ee toddobaadka")
      jebi
  xaalad "Jimce":
      qor("Waa maalinta shanaad ee toddobaadka")
      jebi
  xaalad kale:
      qor("Waa fasax")
}  // Prints: "Waa maalinta koowaad ee toddobaadka" ("It's the first day of the week")

Note

The jebi keyword (meaning "break" in Somali) is required to prevent fall-through to the next case. Without it, execution would continue to the next case regardless of whether it matches.

Ternary Operator

The ternary operator is a shorthand way of writing an if-else statement. It takes three operands: a condition, a result for true, and a result for false.

ternary_operator.sop
// Ternary operator
door da = 20
door xaalad = da >= 18 ? "qof weyn" : "ilmo"

qor(xaalad)  // Prints: "qof weyn" ("adult")

// Ternary operator in an expression
door qiime = 75
qor("Imtixaanka waad " + (qiime >= 60 ? "baastay" : "dhacday"))
// Prints: "Imtixaanka waad baastay" ("You passed the exam")

Best Practices

  • Use Braces for Clarity

    Always use braces for conditional blocks, even for single-line statements. This makes your code more readable and less prone to errors when modifications are made.

    braces_practice.sop
    // Good practice
    haddii xaalad {
        qor("Waa run")
    }
    
    // Avoid this
    haddii xaalad
      qor("Waa run")  // Error-prone if more lines are added later
  • Keep Conditions Simple

    Complex conditions can be hard to understand and debug. Break them down into smaller, more manageable parts or use intermediate variables with descriptive names.

    simple_conditions.sop
    // Instead of this
    haddii da >= 18 && dakhli >= 30000 && !diiwaangashan {
        // Complex logic
    }
    
    // Do this
    door waa_qof_weyn = da >= 18
    door dakhli_ku_filan = dakhli >= 30000
    door u_baahan_diiwaan = !diiwaangashan
    
    haddii waa_qof_weyn && dakhli_ku_filan && u_baahan_diiwaan {
        // Clearer logic
    }
  • Avoid Deep Nesting

    Deeply nested conditional statements can make code hard to follow. Consider using early returns, guard clauses, or refactoring into separate functions.

    avoid_nesting.sop
    // Instead of deep nesting
    haddii xaalad1 {
        haddii xaalad2 {
            haddii xaalad3 {
                // Deeply nested code
            }
        }
    }
    
    // Better approach with early returns
    haddii !xaalad1 {
        soo_celi
    }
    
    haddii !xaalad2 {
        soo_celi
    }
    
    haddii !xaalad3 {
        soo_celi
    }
    
    // Main code here (not deeply nested)