Variables in SAPL

Learn how to store, access, and manipulate data using variables

Learning Objectives

  • Understand what variables are and why they're important
  • Learn how to declare and initialize variables in SAPL
  • Explore different data types that can be stored in variables
  • Learn how to modify variable values and perform operations with variables

Introduction to Variables

Variables are one of the most fundamental concepts in programming. They allow you to store data in memory and refer to it by a name. Think of variables as labeled containers that hold values which can be accessed and modified throughout your program.

In SAPL, variables are particularly powerful because they can store various types of data, including numbers, text (strings), boolean values (true/false), and more complex data structures.

Declaring Variables

In SAPL, you can create a variable by assigning a value to it using the equals sign (=). Unlike some other programming languages, you don't need to declare the variable type explicitly—SAPL automatically determines the type based on the value you assign.


ቁጥር = 42        
ስም = "አብርሃም"    
ነጥብ = 3.14      
እውነት_ነው = እውነት  


አሳይ(ቁጥር)
አሳይ(ስም)
አሳይ(ነጥብ)
አሳይ(እውነት_ነው)
SAPL

Output:

42
አብርሃም
3.14
እውነት

Variable Naming Rules

When naming variables in SAPL, you need to follow these rules:

  • Variable names can contain letters (including Amharic characters), numbers, and underscores.
  • Variable names must start with a letter or an underscore, not a number.
  • Variable names are case-sensitive (ስም and ስም are considered different variables).
  • Variable names cannot be the same as SAPL keywords (like አሳይ, እውነት, etc.).

ስም = "አበበ"
_ስም = "ከበደ"
ስም1 = "አልማዝ"
የተማሪ_ስም = "ፍቅርተ"


# 1ስም = "ሰሎሞን"     
# አሳይ = "ሰሎሞን"      
# ስም-ሙሉ = "ሰሎሞን"
SAPL

Data Types

SAPL supports several data types that can be stored in variables:

Numbers

ቁጥር = 42
ነጥብ = 3.14

Integers (whole numbers) and floating-point numbers (decimals) are both stored as number types in SAPL.

Strings

ስም = "አበበ"
መልእክት = 'ሰላም ዓለም!'

Text values enclosed in single or double quotes. Strings can contain letters, numbers, symbols, and spaces.

Booleans

እውነት_ነው = እውነት
ሐሰት_ነው = ሐሰት

Logical values that can be either እውነት (true) or ሐሰት (false). Used for conditional logic.

Lists

ቁጥሮች = [1, 2, 3, 4, 5]
ስሞች = ["አበበ", "ከበደ", "አልማዝ"]

Collections of items enclosed in square brackets. Lists can contain items of different types.

Modifying Variables

One of the key features of variables is that their values can be changed during program execution. You can assign a new value to an existing variable at any time:


ቁጥር = 10
አሳይ("ቁጥር: " + ቁጥር)

ቁጥር = 20
አሳይ("አዲስ ቁጥር: " + ቁጥር)


ቁጥር = "ሃያ"
አሳይ("አሁን ቁጥር የሚከተለው ነው: " + ቁጥር)
SAPL

Output:

ቁጥር: 10 
አዲስ ቁጥር: 20
አሁን ቁጥር የሚከተለው ነው: ሃያ

Operations with Variables

You can perform various operations with variables depending on their data type:


ቁጥር1 = 10
ቁጥር2 = 5

ድምር = ቁጥር1 + ቁጥር2
ልዩነት = ቁጥር1 - ቁጥር2
ብዜት = ቁጥር1 * ቁጥር2
ክፍፍል = ቁጥር1 / ቁጥር2

አሳይ("ድምር: " + ድምር)
አሳይ("ልዩነት: " + ልዩነት)
አሳይ("ብዜት: " + ብዜት)
አሳይ("ክፍፍል: " + ክፍፍል)

ስም_መጀመሪያ = "አበበ"
ስም_መጨረሻ = "ከበደ"
ሙሉ_ስም = ስም_መጀመሪያ + " " + ስም_መጨረሻ

አሳይ("ሙሉ ስም: " + ሙሉ_ስም)

ሁኔታ1 = እውነት
ሁኔታ2 = ሐሰት

አሳይ("ሁኔታ1 እና ሁኔታ2: " + (ሁኔታ1 እና ሁኔታ2))
አሳይ("ሁኔታ1 ወይም ሁኔታ2: " + (ሁኔታ1 ወይም ሁኔታ2))
SAPL

Output:

ድምር: 15 ልዩነት: 5 ብዜት: 50 ክፍፍል: 2 ሙሉ ስም: አበበ ከበደ ሁኔታ1 እና ሁኔታ2: ሐሰት ሁኔታ1 ወይም ሁኔታ2: እውነት

Variable Scope

The scope of a variable refers to the region of your code where the variable is accessible. In SAPL, variables have different scopes depending on where they are defined:


ዓለም_ቁጥር = 100


ስራ ምሳሌ():
    ውስጥ_ቁጥር = 50
    አሳይ("ውስጥ ቁጥር: " + ውስጥ_ቁጥር)
    አሳይ("ዓለም ቁጥር: " + ዓለም_ቁጥር)

ምሳሌ()

# አሳይ(ውስጥ_ቁጥር)

አሳይ("ዓለም ቁጥር እንደገና: " + ዓለም_ቁጥር)
SAPL

Constants

In programming, constants are values that should not change during program execution. While SAPL doesn't have a specific syntax for declaring constants, it's a common convention to use ALL_UPPERCASE names for values that should be treated as constants:


ፒ_ዋጋ = 3.14159
ግራቪቲ = 9.8
ዝቅተኛ_እድሜ = 18

ዙር = 2 * ፒ_ዋጋ * 5
አሳይ("ዙር: " + ዙር)

እድሜ = 16
አሳይ("በቂ እድሜ አለው: " + (እድሜ >= ዝቅተኛ_እድሜ))
SAPL

Output:

ዙር: 31.4159 በቂ እድሜ አለው: ሐሰት

Best Practices

  • Use descriptive variable names

    Choose names that clearly indicate what the variable represents. For example, use የተማሪ_ስም instead of just ስ.

  • Be consistent with naming conventions

    If you start using snake_case (words_separated_by_underscores), stick with it throughout your code.

  • Initialize variables before using them

    Always assign a value to a variable before trying to use it in operations or display it.

  • Keep variable types consistent

    While SAPL allows changing a variable's type, it's generally clearer to maintain consistent types.

  • Use ALL_UPPERCASE for constants

    This makes it clear which values should not be changed during program execution.

Summary

Key Concepts Learned

  • Variables are used to store and manipulate data in your programs
  • In SAPL, variables are created using the assignment operator (=)
  • Variables can store different types of data: numbers, strings, booleans, and more
  • Variable names must follow specific rules and should be descriptive
  • Variables can be modified and used in various operations
  • Variable scope determines where in your code a variable can be accessed

Variables are one of the most fundamental building blocks in programming. They allow you to store, access, and manipulate data throughout your program. By mastering variables, you've taken a significant step in your journey to becoming proficient in SAPL programming.