Print Statements in SAPL
Learn how to display output in SAPL using the አሳይ() function
Learning Objectives
- Master the use of the
አሳይ()function for displaying output - Learn how to print different data types
- Understand how to format output for better readability
- Learn how to print multiple values in a single statement
Introduction
The ability to display output is fundamental to any programming language. In SAPL, the አሳይ() function (which means "show" in Amharic) is used to display information to the user. This tutorial will cover everything you need to know about printing output in SAPL.
Basic Printing
The አሳይ() function takes one or more arguments and displays them in the console. Let's start with some basic examples:
# ጽሁፍ ለማሳየት
አሳይ("ሰላም ልዑል!")
# ቁጥር ለማሳየት
አሳይ(100)
# የስሌት ውጤት ለማሳየት
አሳይ(5 + 7)Output:
ሰላም ልዑል!
100
12
As you can see, አሳይ() can display strings, numbers, and the results of expressions. Each call to አሳይ()automatically adds a newline at the end, so subsequent calls start on a new line.
Printing Variables
You can also use አሳይ() to display the values of variables:
ስም = "አበበ"
እድሜ = 25
ቁጥር = 3.14
አሳይ(ስም)
አሳይ(እድሜ)
አሳይ(ቁጥር)Output:
አበበ
25
3.14
String Concatenation
To combine text and variables in a single output, you can use string concatenation with the + operator:
ስም = "አበበ"
እድሜ = 25
አሳይ("ስም: " + ስም)
አሳይ("እድሜ: " + እድሜ)
አሳይ("ሰላም, ስሜ " + ስም + " ይባላል እና " + እድሜ + " ዓመቴ ነው።")Output:
ስም: አበበ
እድሜ: 25
ሰላም, ስሜ አበበ ይባላል እና 25 ዓመቴ ነው።
Important Note
አሳይ(25 + "years") will result in an error. Instead, convert the number to a string first: አሳይ(25 + " years").Printing Multiple Arguments
The አሳይ() function can take multiple arguments separated by commas. Each argument will be printed with a space in between:
አሳይ("ስም:", "አበበ")
አሳይ("እድሜ:", 25)
አሳይ("ቁጥሮች:", 1, 2, 3, 4, 5)
አሳይ("ሰላም", "ዓለም", "!", 2023)Output:
ስም: አበበ
እድሜ: 25
ቁጥሮች: 1 2 3 4 5
ሰላም ዓለም ! 2023
This approach is often cleaner than using string concatenation, especially when mixing different data types.
Special Characters and Formatting
SAPL supports special escape sequences for formatting output:
አሳይ("መስመር 1 \n መስመር 2 \n መስመር 3")
አሳይ("ስም: \t አበበ \n እድሜ: \t 25")
አሳይ("አበበ አለ: 'ሰላም ዓለም!'")
Output:
መስመር 1 መስመር 2 መስመር 3 ስም: አበበ እድሜ: 25 አበበ አለ: "ሰላም ዓለም!" ይህ ባክስላሽ ነው: \
| Escape Sequence | Description |
|---|---|
\n | Newline |
\t | Tab |
\" | Double quote |
\' | Single quote |
\\ | Backslash |
Common Errors and Troubleshooting
Type Error in Concatenation
አሳይ(25 + "years")Error: Cannot concatenate a number and a string directly. Convert the number to a string by adding an empty string or a space:
አሳይ(25 + " years")Unmatched Quotation Marks
አሳይ("ሰላም ዓለም!)Error: Missing closing quotation mark. The correct syntax is:
አሳይ("ሰላም ዓለም!")Incorrect Escape Sequences
አሳይ("መስመር 1\nመስመር 2")Error: In SAPL strings, escape sequences need two backslashes. The correct syntax is:
አሳይ("መስመር 1\\nመስመር 2")Best Practices
Use descriptive output messages
Instead of just
አሳይ(ስም), useአሳይ("ስም: " + ስም)to make the output more readable.Use multiple arguments instead of concatenation when possible
አሳይ("ስም:", ስም)is cleaner thanአሳይ("ስም: " + ስም)Use escape sequences for formatting complex output
Tabs and newlines can make your output more organized and easier to read.
Add debug prints during development
Temporary
አሳይ()statements can help you understand what your code is doing.
Summary
Key Concepts Learned
- How to use the
አሳይ()function to display output - How to print different data types including strings, numbers, and variables
- How to concatenate strings and variables for formatted output
- How to use multiple arguments in a single
አሳይ()call - How to use special characters and escape sequences for formatting
The አሳይ() function is one of the most frequently used functions in SAPL. Mastering it will allow you to create informative and well-formatted output for your programs, which is essential for both debugging and user interaction.
