Skip to content

If Expressions

In Myst, if-else are also expressions, not statements. This means they can be assigned to variables, used as part of other expressions, and their result can be captured and used elsewhere in your code.

Basic Syntax

myst
တကယ်လို့ (condition) ဖြစ်ခဲ့လျှင် {
    // if block
} မဟုတ်လျှင် {
    // else block (optional)
}

If as an Expression

Unlike many other languages, if-else in Myst returns a value. You can assign the result of an တကယ်လို့ expression to a variable, or use it directly in another expression.

Example:

myst
အဖြေ = တကယ်လို့ (၁၀ > ၅) ဖြစ်ခဲ့လျှင် { "ကြီး" } မဟုတ်လျှင် { "ငယ်" }
ထုတ်ပြ(အဖြေ) // output => "ကြီး"

You can also use တကယ်လို့ expressions inside other expressions:

myst
ထုတ်ပြ("ရလဒ်: " + (တကယ်လို့ (၁ > ၂) ဖြစ်ခဲ့လျှင် { "A" } မဟုတ်လျှင် { "B" })) // output => ရလဒ်: "B"

TIP

The use of ဖြစ်ခဲ့လျှင် after the condition is optional. It is included for circumstantial and smooth Burmese reading, but you can omit it if you prefer. Both forms are valid and function identically.

Examples:

myst
// With 'ဖြစ်ခဲ့လျှင်'
အဖြေ = တကယ်လို့ (၁၀ > ၅) ဖြစ်ခဲ့လျှင် { "ကြီး" } မဟုတ်လျှင် { "ငယ်" }

// Without 'ဖြစ်ခဲ့လျှင်'
အဖြေ = တကယ်လို့ (၁၀ > ၅) { "ကြီး" } မဟုတ်လျှင် { "ငယ်" }

Both examples above are equivalent.

Optional Else Block

The မဟုတ်လျှင် (else) block is optional. If the condition is false and there is no မဟုတ်လျှင် block, the result of the expression will be ဗလာ (null).

Example:

myst
အဖြေ = တကယ်လို့ (၁ > ၂) ဖြစ်ခဲ့လျှင် { "A" }
ထုတ်ပြ(အဖြေ) // output => ဗလာ

Using If Expressions in Assignments and Other Expressions

You can use တကယ်လို့ expressions anywhere an expression is allowed:

myst
နှုတ်ဆက်စာ သည် "Hello, " + (တကယ်လို့ (မနက်) ဖြစ်ခဲ့လျှင် { "Good morning!" } မဟုတ်လျှင် { "Welcome!" })

Return Statement (ရလဒ်) in If Expressions

When using တကယ်လို့ expressions (like when assigning to a variable), you can use ရလဒ် within the if-else body. If you use ရလဒ်, it will immediately return the value and stop the execution of the current expression.

If you don't want to stop execution and just want to get the value, omit the ရလဒ် keyword. The last line of the expression in the if-else body will be returned as the value.

Example: Using ရလဒ် (stops execution):

myst
အဖြေ = တကယ်လို့ (၁ > ၀) ဖြစ်ခဲ့လျှင် {
    ရလဒ် "အပေါ်"
} မဟုတ်လျှင် {
    ရလဒ် "အောက်"
}
ထုတ်ပြ(အဖြေ) // This line will never be reached

Example: Without ရလဒ် (returns last line value):

myst
အဖြေ = တကယ်လို့ (၁ > ၀) ဖြစ်ခဲ့လျှင် {
    "အပေါ်" // This will be returned
} မဟုတ်လျှင် {
    "အောက်" // This will be returned
}
ထုတ်ပြ(အဖြေ) // output => အပေါ်

Example: Multiple lines without ရလဒ်:

myst
အဖြေ = တကယ်လို့ (၁ > ၀) ဖြစ်ခဲ့လျှင် {
    ထုတ်ပြ("အပေါ်ဖြစ်နေပါတယ်")
    "အပေါ်" // This will be returned (last line)
} မဟုတ်လျှင် {
    ထုတ်ပြ("အောက်ဖြစ်နေပါတယ်")
    "အောက်" // This will be returned (last line)
}

In the last example, the function will print the message and then return the string value.

Summary

  • တကယ်လို့ expressions in Myst are true expressions: they can be assigned to variables, used inside other expressions, and nested.
  • The မဟုတ်လျှင် block is optional; if omitted and the condition is false, the result is ဗလာ(null).
  • The use of ဖြစ်ခဲ့လျှင် after the condition is optional and included for smooth Burmese reading.
  • If you use ရလဒ် inside an if-expression, it will immediately return that value and stop further execution of the block or expression.
  • If you omit ရလဒ်, the value of the last line in the block will be used as the result.