Swahili borrows its syntax from Javascript, Python and Ruby. Like Javascript, Swahili is case sensitive. as such the declarations below are different variables.
wacha b = 5
wacha B = 3
Swahili uses //
for single line comments and /* */
for multiline comments
// This is a single line comment
/* This
is
a
Multiline
Comment
*/
Variables are used as symbolic names for values within the program. The names of variables, called identifiers must begin with either an underscore ( _
), or an alphabetic character in either upper or lower case. Subsequent characters can be alphanumeric or an underscore. Hence:
wacha _jina = "John" // valid
wacha Jina = "John" // valid
wacha jina = "John" // valid
wacha 0_jina = "John" // invalid
Swahili is case sensitive, hence, Neno
and neno
are different variables.
You cannot use Swahli keywords as variable names.
wacha kweli = uwongo // will raise an error because kweli is a Swahili keyword
To declare a variable, we use the keyword wacha
, followed by the identifier name and an initialization. You have to initialize your variables when you declare them. If you want a variable to be empty on declaration you intitalize it to null (tupu).
wacha jina // will raise an error
wacha jina = "" // is valid
wacha jina = tupu // initializes jina to null
- An attempt to access an undeclared variable results in a Runtime error.
- You can not perform basic arithmetic and boolean operations on variables with a null value.
Variables you declare within a global scope (outside of any function) cannot be redeclared, ie,
wacha x = 3
wacha x = 5 // this line will raise an error
x = 5 // this will not raise an error
- Variables declared inside a function block are only available for that block. For instance:
wacha a = "Outside block"
shughuli block_example() {
wacha a = "Inside Block"
andika(a)
}
block_example() // => "Inside Block"
andika(a) // => "Outside block"
- There are 8 Data structures and Types in Swahili.
- Boolean
Hali
:kweli
that represents true anduwongo
that represents false - null
tupu
: Denotes a null value - Number
Nambari
: Could be both Integers and floats. For example42
or3.14159
- Strings
Jina
: A sequence of characters that represents a text value. For exampleHabari
- Lists
Orodha
: An ordered collection of items. Can hold multiple data types: for example[1,2,3]
or[1, "orodha", kweli]
- Regular Expressions
RegEx
: These are patterns used to match character combinations in strings. - Date
Tarehe
: Used to represent a date object. - Object
Kamusi
: Works like a dictionary by storing items as key/value pairs
- Boolean
- A list literal is a collection of zero or more expressions, each of which represents a list element enclosed in square brackets. The length of a list is set to the number of arguments specified.
- The following creates a list of
vinywaji
wacha vinywaji = ["chai", "kahawa ya Uhabeshi", "Kahawa ya Kenya" ]
- Lists in Swahili are zero based, which means that the first element has the index of zero.
- Unlike most languages access a specific element in an array we put the index after a forward slash. For example
wacha vinywaji = ["chai", "kahawa ya Uhabeshi", "Kahawa ya Kenya" ]
andika(vinywaji/0) // => "chai"
andika(vinywaji/2) // => "Kahawa ya Kenya"
- To append an element to the end of a list we use the plus sign(
+
) followed by the element we want to add. For Example
vinywaji = vinywaji + "soda"
andika(vinywaji) // => ["chai", "kahawa ya Uhabeshi", "Kahawa ya Kenya", "soda" ]
- To delete an element in a list we use the
-
followed by the index of the element. For Example:
vinywaji = vinywaji - 3
andika(vinywaji) // => ["chai", "kahawa ya Uhabeshi", "Kahawa ya Kenya" ]
- We can combine 2 lists into one using an asterisk (
*
) - For Example:
wacha vyakula = ["maandazi", "wali", "mahamri"]
wacha vyakula_vinywaji = vinywaji * vyakula
andika(vyakula_vinywaji) // => ["chai", "kahawa ya Uhabeshi", "Kahawa ya Kenya", "maandazi", "wali", "mahamri"]
- The boolean type has 2 literal values:
kweli
anduwongo
- The Number type contains both floats and integers.
- Swahili deals only with numbers in base ten, ie,
decimal number system
- Examples of numeric literals are:
1,100, 3.14159, 2.71828
- A string literal is zero or more characters enclosed in double(
" "
) quotation marks. Swahili does not support use of single quotation marks to denote strings, henceandika('Habari Dunia')
is invalid butandika("Habari Dunia")
is valid.
- In addition to ordinary characters you can also add special characters to strings.
- For Example
wacha a = "habari \n yako"
- The charaters
\n
add a new line. The list of special characters is shown below
Character | Meaning |
---|---|
\n | New line |
\t | Tab space |
" | Double quote |
- These are patterns used to match character combinations in strings.
- To declare a RegExp we use the RegEx() function. The function takes in 2 arguments; A RegEx pattern and a RegEx flag.
RegEx("pattern", "flag")
- For Example:
wacha a = RegEx("ab+c", "g")
andika(a) // => /ab+c/g
- Used to represent a date object. The keyword used to initialize a date is
Tarehe()
.
wacha s = Tarehe()
An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}
).
For example:
wacha mtu = {
jina: "Wendo",
umri: 21,
urefu : 180
}
This then allows us to get specific values by checking their keys. For instance mtu.jina
would return "Wendo"