Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
Oussama Essamadi edited this page Mar 24, 2024 · 2 revisions

LangKama supports looping constructs to facilitate repeated execution of code blocks based on a condition. This section describes how to use loop statements in LangKama and addresses common error scenarios.

Loop statement

LangKama uses the cook until syntax for loop statements, which repeatedly executes a block of code as long as the given condition is true. The condition is checked before each iteration of the loop.

hear me out index is 0.
hear me out sum is 0.

cook until (index = 10) {
  sum is sum + index.
  index is index + 1.
}

reda sum.  bs Outputs 45, the sum of numbers 0 through 9

This loop initializes two variables, index, and sum, then iteratively adds index to sum until index equals 10, demonstrating a common use case for loops in aggregating or modifying values over time.

Error Handling in Loops

ExpectedOpenBraceError

Like conditionals, loops in LangKama require an open brace { immediately following the condition to define the scope of the loop. Missing the opening brace results in a syntax error.

cook until (W)
  loncina("this should not be reached").
}
bs This will raise an ExpectedOpenBraceError due to the missing '{'

Ensuring that every loop statement is properly enclosed in braces helps maintain clear and error-free code, especially in complex or nested loop scenarios.

Clone this wiki locally