Conditionals in SAPL
Learn how to make decisions in your code using conditional statements
Learning Objectives
- Understand the concept of conditional statements and their importance in programming
- Learn how to use if statements to execute code based on conditions
- Master if-else statements for handling alternative scenarios
- Explore if-elif-else chains for multiple conditions
- Apply conditional statements to solve practical programming problems
Introduction to Conditionals
Conditional statements are programming constructs that allow your code to make decisions and execute different blocks of code based on whether certain conditions are true or false. They are fundamental to creating programs that can respond to different situations and user inputs.
In real life, we make decisions based on conditions all the time: "If it's raining, I'll take an umbrella," or "If the store is open, I'll buy groceries; otherwise, I'll order food." Conditional statements in programming work in a similar way, allowing your programs to make decisions and adapt their behavior accordingly.
SAPL supports several types of conditional statements, including if statements, if-else statements, and if-elif-else chains. In this tutorial, we'll explore each of these and learn how to use them effectively.
If Statement
The simplest form of conditional statement is the if statement. It executes a block of code only if a specified condition is true.
Basic If Statement
In SAPL, the if statement uses the ከሆነ keyword, which means "if". The syntax is:
ሁኔታ ከሆነ:
# እውነት ከሆነ የሚከናወን ኮድHere's a simple example:
ሂሳብ = 85
ሂሳብ > 80 ከሆነ:
አሳይ("ምርጥ ውጤት!")Output:
ምርጥ ውጤት!
In this example, the message "ምርጥ ውጤት!" (Excellent score!) is displayed because the condition ሂሳብ > 80 is true.
Multiple Conditions
You can use logical operators (እና, ወይም, አይደለም) to create more complex conditions:
እድሜ = 25
ብድር_ውስጥ = ሀሰት
እድሜ >= 18 እና !ብድር_ውስጥ ከሆነ:
አሳይ("ብድር ለማግኘት ብቁ ነዎት።")Output:
ብድር ለማግኘት ብቁ ነዎት።
Indentation Matters
If-Else Statement
The if-else statement allows you to execute one block of code if a condition is true and another block if the condition is false.
Basic If-Else Statement
In SAPL, the if-else statement uses the ከሆነ and ሌላ keywords. The syntax is:
ሁኔታ ከሆነ:
# እውነት ከሆነ የሚከናወን ኮድ
ሌላ:
# ሐሰት ከሆነ የሚከናወን ኮድ
Here's an example:
እድሜ = 15
እድሜ >= 18 ከሆነ:
አሳይ("አንተ ጎልማሳ ነህ።")
ሌላ:
አሳይ("አንተ ልጅ ነህ።")Output:
አንተ ልጅ ነህ።
In this example, since እድሜ is 15, which is less than 18, the condition እድሜ >= 18 is false, so the code in the ሌላ block is executed.
Practical Example: Grade Calculator
Let's create a simple grade calculator using if-else statements:
ውጤት = 75
ውጤት >= 90 ከሆነ:
አሳይ("ደረጃ: A")")
ሌላ:
ውጤት >= 80 ከሆነ:
አሳይ("ደረጃ: B")
ሌላ:
ውጤት >= 70 ከሆነ:
አሳይ("ደረጃ: C")
ሌላ:
ውጤት >= 60 ከሆነ:
አሳይ("ደረጃ: D")
ሌላ:
አሳይ("ደረጃ: F")Output:
ደረጃ: C
If-Elif-Else Chain
When you have multiple conditions to check, you can use an if-elif-else chain. This allows you to check several conditions in sequence and execute the code block associated with the first true condition.
If-Elif-Else Chain
In SAPL, the if-elif-else chain uses the ከሆነ, ካልሆነ, and ሌላ keywords. The syntax is:
ሁኔታ ከሆነ:
# እውነት ከሆነ የሚከናወን ኮድ
ሁኔታ1 ካልሆነ:
# ሁኔታ1 እውነት ካልሆነ የሚከናወን ኮድ
ሁኔታ2 ካልሆነ:
# ሁኔታ2 እውነት ካልሆነ የሚከናወን ኮድ
ሌላ:
# ሁኔታ ሁሉም እውነት ካልሆነ የሚከናወን ኮድHere's an example:
ሂሳብ = 50
ሂሳብ > 40 ከሆነ:
አሳይ("ሂሳብ 40 ይበልጣል")
ሂሳብ < 40 ካልሆነ:
አሳይ("ሂሳብ 40 ያንሳል")
ሌላ:
አሳይ("ሂሳብ 40 ነው")Output:
ሂሳብ 40 ይበልጣል
In this example, since ሂሳብ is 50, which is greater than 40, the condition ሂሳብ > 40 is true, so the code in the first block is executed. The other conditions are not checked.
Practical Example: BMI Calculator
Let's create a BMI (Body Mass Index) calculator using an if-elif-else chain:
ክብደት = 70 # በኪ.ግ
ቁመት = 1.75 # በሜትር
# የውፍረት ልኬት ስሌት
ውሌስ = ክብደት / (ቁመት * ቁመት)
አሳይ("ውሌስ: " + ውሌስ)
ውሌስ < 18.5 ከሆነ:
አሳይ("ክብደት ማነስ")
ውሌስ >= 18.5 እና ውሌስ < 25 ካልሆነ:
አሳይ("መደበኛ ክብደት")
ውሌስ >= 25 እና ውሌስ < 30 ካልሆነ:
አሳይ("ከመጠን በላይ ክብደት")
ሌላ:
አሳይ("ከመጠን በላይ ውፍረት")Output:
ውሌስ: 22.86 መደበኛ ክብደት
Order Matters
Nested Conditionals
You can place conditional statements inside other conditional statements, creating what's called a nested conditional. This allows for more complex decision-making logic.
እድሜ = 25
ፍቃድ_አለው = እውነት
እድሜ >= 18 ከሆነ:
አሳይ("አንተ ጎልማሳ ነህ።")
ፍቃድ_አለው ከሆነ:
አሳይ("መኪና መንዳት ትችላለህ።")
ሌላ:
አሳይ("መኪና ለመንዳት ፍቃድ ያስፈልግሃል።")
ሌላ:
አሳይ("አንተ ልጅ ነህ።")
አሳይ("መኪና መንዳት አትችልም።")Output:
አንተ ጎልማሳ ነህ። መኪና መንዳት ትችላለህ።
In this example, we first check if the person is an adult. If they are, we then check if they have a license to determine if they can drive. If they're not an adult, we simply inform them that they can't drive.
Conditional Expressions (Ternary Operator)
SAPL supports a compact way to write simple if-else statements using a conditional expression, also known as the ternary operator. This allows you to assign a value to a variable based on a condition in a single line.
እድሜ = 20
ሁኔታ = ""
እድሜ >= 18 ከሆነ:
ሁኔታ = "ጎልማሳ"
ሌላ:
ሁኔታ = "ልጅ"
አሳይ(ሁኔታ)
ሁኔታ = "ጎልማሳ" if እድሜ >= 18 else "ልጅ"
አሳይ(ሁኔታ)Output:
ጎልማሳ ጎልማሳ
Both approaches produce the same result, but the conditional expression is more concise. It's particularly useful for simple conditions where you're assigning one of two values to a variable.
Common Errors and Troubleshooting
Incorrect Indentation
እድሜ = 20
እድሜ >= 18 ከሆነ:
አሳይ("ጎልማሳ")Error: The code inside the if statement is not indented. The correct syntax is:
እድሜ = 20
እድሜ >= 18 ከሆነ:
አሳይ("ጎልማሳ")Missing Colon
እድሜ = 20
እድሜ >= 18 ከሆነ
አሳይ("ጎልማሳ")Error: The colon (:) after the condition is missing. The correct syntax is:
እድሜ = 20
እድሜ >= 18 ከሆነ:
አሳይ("ጎልማሳ")Using = Instead of ==
እድሜ = 20
እድሜ = 18 ከሆነ:
አሳይ("እድሜህ 18 ነው")Error: The = operator is used for assignment, not comparison. For equality comparison, use ==. The correct syntax is:
እድሜ = 20
እድሜ == 18 ከሆነ:
አሳይ("እድሜህ 18 ነው")Best Practices
Keep conditions simple and readable
If a condition is becoming complex, consider breaking it down into smaller, more manageable parts.
Use meaningful variable names
Clear variable names make your conditions easier to understand and maintain.
Be consistent with indentation
Use the same indentation style (typically 4 spaces) throughout your code.
Consider the order of conditions
In if-elif-else chains, conditions are checked in order. Place more specific or common conditions first for better performance.
Use conditional expressions for simple cases
For simple if-else statements that assign a value to a variable, consider using a conditional expression for more concise code.
Summary
Key Concepts Learned
- Conditional statements allow your code to make decisions and execute different blocks of code based on conditions
- If statements (
ከሆነ) execute a block of code only if a condition is true - If-else statements (
ከሆነ/ሌላ) provide an alternative when the condition is false - If-elif-else chains (
ከሆነ/ካልሆነ/ሌላ) allow for multiple conditions to be checked in sequence - Nested conditionals enable more complex decision-making logic
- Conditional expressions provide a concise way to write simple if-else statements
Conditional statements are a fundamental part of programming, allowing your code to make decisions and adapt to different situations. By mastering conditionals in SAPL, you've gained an essential skill that will be used in virtually every program you write.
