Intro
Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
In Kin we have the following conditional statements:
Use niba
to specify a block of code to be executed, if a specified condition is true
Use nanone_niba
to specify a block of code to be executed, if the same condition is false
Use niba_byanze
to specify a new condition to test, if the first condition is false
Use gereranya
to specify multiple code blocks to be executed based on different values
Niba Statement
Use the niba
statement to specify a block of Kin code to be executed if a condition is true.
niba (condition) {
# Codes to execute if the condition is true
}
Note that
niba
is in lowercase letters. Uppercase letters (Niba
orNIBA
) will generate a Kin error.
Niba_byanze Statement
Use the niba_byanze
statement to specify a block of code to be executed if the condition is false.
niba (condition) {
# block of code to be executed if the condition is true
} niba_byanze {
# block of code to be executed if the condition is false
}
Nanone_niba Statement
Use Use the nanone_niba
statement to specify a new condition if the first condition is false.
niba (condition1) {
# block of code to be executed if condition1 is true
} nanone_niba (condition2) {
# block of code to be executed if the condition1 is false and condition2 is true
} niba_byanze {
# block of code to be executed if the condition1 is false and condition2 is false
}
Gereranya Statement (Switch)
Use the gereranya
statement to perform different actions based on different conditions. The gereranya statement provides a cleaner alternative to multiple niba_byanze
statements when testing a variable against many values.
gereranya (expression) {
usanze value1:
# code to execute when expression equals value1
usanze value2:
# code to execute when expression equals value2
usanze value3:
# code to execute when expression equals value3
ibindi:
# code to execute when expression doesn't match any case
}
Note: Unlike some other languages, Kin's gereranya statement doesn't require explicit break statements. Each case automatically stops execution after its block is completed.
Example
reka iletere = injiza_amakuru("shyiramo letere nyamuneka:")
gereranya(iletere){
usanze "a":
reka a = 1
reka b = 2
tangaza_amakuru(a+b)
tangaza_amakuru("Iyi letere ni a")
usanze "b":
tangaza_amakuru("Iyi letere ni b")
ibindi:
tangaza_amakuru("Iyi letere ntayo twize")
}