Expressions in SAPL
Learn how to work with mathematical and boolean expressions to perform calculations and logical operations
Learning Objectives
- Understand what expressions are and how they're evaluated in SAPL
- Master arithmetic expressions for mathematical calculations
- Learn how to work with boolean expressions for logical operations
- Understand operator precedence and how to use parentheses
- Apply expressions in practical programming scenarios
Introduction to Expressions
Expressions are combinations of values, variables, operators, and function calls that can be evaluated to produce a result. They are fundamental building blocks in programming that allow you to perform calculations, make comparisons, and create complex logic.
In SAPL, expressions can be arithmetic (dealing with numbers), boolean (dealing with logical values), or a combination of both. Understanding how to create and use expressions is essential for writing effective SAPL programs.
Arithmetic Expressions
Arithmetic expressions involve mathematical operations on numeric values. SAPL supports all standard arithmetic operators for performing calculations.
Basic Operators
# መደመር
አሳይ(5 + 3) # 8
# መቀነስ
አሳይ(10 - 4) # 6
# ማባዛት።
አሳይ(6 * 7) # 42
# ማካፈል
አሳይ(20 / 5) # 4.0
# ቀሪ
አሳይ(17 % 5) # 2
# ገላጭ
አሳይ(2 ** 3) # 8Complex Expressions
# ኦፕሬተሮችን በማጣመር ላይ
አሳይ(6 * 6 + 8 - 7) # 37
# ቅንፎችን መጠቀም
አሳይ((10 + 5) * 2) # 30
# ከተለዋዋጮች ጋር
ቁጥር1 = 15
ቁጥር2 = 3
አሳይ(ቁጥር1 / ቁጥር2 + 2) # 7.0
# የተቀላቀሉ ስራዎች
አሳይ(2 + 3 * 4) # 14
አሳይ((2 + 3) * 4) # 20Operator Precedence
| Operator | Description | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 6 * 7 | 42 |
/ | Division | 20 / 5 | 4.0 |
% | Modulus (remainder) | 17 % 5 | 2 |
** | Exponentiation | 2 ** 3 | 8 |
Boolean Expressions
Boolean expressions evaluate to either እውነት (true) or ሀሰት (false). They are essential for controlling program flow with conditional statements and loops.
Comparison Operators
# እኩል ነው።
አሳይ(5 == 5) # እውነት
# እኩል አይደለም።
አሳይ(5 != 3) # እውነት
# ይበልጣል
አሳይ(10 > 5) # እውነት
# ያነሰ ነው።
አሳይ(10 < 20) # እውነት
# ይበልጣል ወይም እኩል ነው።
አሳይ(10 >= 10) # እውነት
# ያነሰ ወይም እኩል ነው።
አሳይ(5 <= 3) # ሀሰትLogical Operators
# አመክንዮአዊ እና
አሳይ(እውነት እና እውነት) # እውነት
አሳይ(እውነት እና ሀሰት) # ሀሰት
# አመክንዮአዊ ወይም
አሳይ(እውነት ወይም ሀሰት) # እውነት
አሳይ(ሀሰት ወይም ሀሰት) # ሀሰት
# አመክንዮአዊ አይደለም።
አሳይ(አይደለም እውነት) # ሀሰት
አሳይ(አይደለም ሀሰት) # እውነት
# የተጣመሩ አመክንዮአዊ አገላለጾች
አሳይ(እውነት እና (አይደለም ሀሰት)) # እውነት
አሳይ((5 > 3) ወይም (2 < 1)) # እውነት| Operator | Description | Example | Result |
|---|---|---|---|
እና | Logical AND | እውነት እና እውነት | እውነት |
ወይም | Logical OR | እውነት ወይም ሀሰት | እውነት |
አይደለም | Logical NOT | አይደለም እውነት | ሀሰት |
Combining Expressions
You can combine arithmetic and boolean expressions to create more complex logic. This is particularly useful in conditional statements and loops.
# አንድ ቁጥር በተወሰነ ክልል ውስጥ መሆኑን ማረጋገጥ
ቁጥር = 15
አሳይ(ቁጥር > 10 እና ቁጥር < 20) # እውነት
# አንድ ቁጥር እንኳ መሆኑን ማረጋገጥ
አሳይ(ቁጥር % 2 == 0) # ሀሰት
# ውስብስብ ሁኔታ
እድሜ = 25
ስም = "አበበ"
አሳይ(እድሜ >= 18 እና ስም == "አበበ") # እውነት
# በአመክንዮአዊ አውድ ውስጥ ስሌት
x = 5
y = 10
አሳይ((x + y) > 12) # እውነትOrder of Evaluation
String Expressions
SAPL also supports expressions with strings. You can concatenate strings using the + operator and repeat strings using the * operator.
# የሕብረቁምፊዎች ውህደት
ስም_መጀመሪያ = "አበበ"
ስም_መጨረሻ = "ከበደ"
ሙሉ_ስም = ስም_መጀመሪያ + " " + ስም_መጨረሻ
አሳይ(ሙሉ_ስም) # አበበ ከበደ
# የሕብረቁምፊ ድግግሞሽ
ምልክት = "-"
መስመር = ምልክት * 20
አሳይ(መስመር) # --------------------
# የሕብረቁምፊ ንጽጽር
ስም1 = "አበበ"
ስም2 = "ከበደ"
አሳይ(ስም1 == ስም2) # ሀሰት
አሳይ(ስም1 != ስም2) # እውነትExpressions with Variables
Variables can be used in expressions just like literal values. This allows you to create dynamic expressions that change based on the values of variables.
# ተለዋዋጮች በሂሳብ መግለጫዎች ውስጥ
ርዝመት = 10
ስፋት = 5
ዙሪያ = 2 * (ርዝመት + ስፋት)
ስፋት_ቦታ = ርዝመት * ስፋት
አሳይ("ዙሪያ: " , ዙሪያ) # ዙሪያ: 30
አሳይ("ስፋት ቦታ: " , ስፋት_ቦታ) # ስፋት ቦታ: 50
# ተለዋዋጮች በቦሊያን መግለጫዎች ውስጥ
እድሜ = 20
የአቅመ_አዳም_እድሜ = 18
አሳይ("ጎልማሳ ነው: " + str(እድሜ >= የአቅመ_አዳም_እድሜ)) # ጎልማሳ ነው: እውነት
# ተለዋዋጮችን በገለጻዎች ማዘመን
ቁጥር = 5
ቁጥር = ቁጥር + 1 # በአንድ መጨመር
አሳይ(ቁጥር) # 6
ቁጥር = ቁጥር * 2 # እሴቱን በእጥፍ ማሳደግ
አሳይ(ቁጥር) # 12Common Errors and Troubleshooting
Division by Zero
አሳይ(10 / 0)Error: Division by zero is not allowed in mathematics or programming. Always check that your divisor is not zero before performing division.
Type Mismatch
አሳይ("ቁጥር: " + 5 + 3)Error: This will output "ቁጥር: 53" instead of "ቁጥር: 8" because string concatenation takes precedence. Use parentheses to clarify:
አሳይ("ቁጥር: " + (5 + 3))Missing Parentheses
አሳይ(5 + 3 * 2 == 16)Error: This evaluates to ሀሰት because 5 + 3 * 2 equals 11, not 16. If you meant to add 5 and 3, then multiply by 2, use parentheses:
አሳይ((5 + 3) * 2 == 16)Best Practices
Use parentheses for clarity
Even when not strictly necessary, parentheses can make your expressions easier to read and understand.
Break down complex expressions
Instead of writing one long, complex expression, break it down into smaller parts using intermediate variables.
Be careful with operator precedence
When in doubt about the order of operations, use parentheses to make your intentions explicit.
Avoid unnecessary complexity
Simple, clear expressions are easier to understand and less prone to errors than complex ones.
Summary
Key Concepts Learned
- Expressions are combinations of values, variables, operators, and function calls
- Arithmetic expressions use operators like +, -, *, /, %, and ** for mathematical calculations
- Boolean expressions evaluate to እውነት (true) or ሀሰት (false) using comparison and logical operators
- String expressions allow for concatenation and repetition of text
- Operator precedence determines the order in which operations are performed
- Parentheses can be used to override the default operator precedence
Expressions are fundamental building blocks in SAPL programming. They allow you to perform calculations, make decisions, and create complex logic in your programs. By mastering expressions, you've taken another important step in your journey to becoming a proficient SAPL programmer.
