ASPL لباتASPL 0.7.0

Search documentation

Search starts with current English documentation · ASPL 0.7.0

Enter a complete name such as read_line, or words about a topic.

    العربية
    Source files

    Open one UTF-8 .aspl or .لبات file, up to 1 MiB, in a new draft. It stays in this browser and does not run automatically.

    Loops

    ASPL 0.7.0 · Current release

    Trace a mutable counter and sum 1 through 5 to 15, then explain termination.

    On this page

    What you will learn

    You will sum the integers 1 through 5, trace a mutable counter and accumulator, and explain why a loop stops.

    Before you begin

    Read bindings and arithmetic for mutable names and numeric formatting, and conditions for Boolean comparisons. This complete program includes its own entry function. Open it as a new Home draft, then choose Run explicitly. Earlier drafts stay available.

    Repeat while a condition is true

    var / متغير declares a mutable binding. Both counter and sum have type i64 / ص64, initialized to 1 and 0. The accumulator sum holds the running total. while / طالما tests counter <= 5 before each iteration, including the first. A true condition runs the braced body; a false condition continues after it.

    Inside the body, sum = sum + counter adds the current counter to the total. Then counter = counter + 1 advances to the next integer. Assignment replaces a binding's value. The order matters: add the current integer before advancing it.

    Browser and native · ASPL 0.7.0 · English source

    Execution: Browser and native · ASPL 0.7.0

    Prerequisites:
    • Read the example explanation in its lesson before running.

    Input: No input lines are required.

    fn start_main() -> i64 {
        var sum: i64 = 0;
        var counter: i64 = 1;
        while counter <= 5 {
            sum = sum + counter;
            counter = counter + 1;
        }
        print(format_i64(sum) + "\n");
        return 0;
    }
    
    Download lesson-loops.aspl

    Expected outcomes
    {
      "browser": {
        "check": "success",
        "class": "success",
        "output": "15\n",
        "processStatus": 0
      },
      "native": {
        "check": 0,
        "status": 0,
        "output": "15\n"
      }
    }

    The reserved start_main() -> i64 / ابدأ_الرئيسية() -> ص64 entry runs the program. After the loop, format_i64 / نسق_ص64 converts the total to text, and print / اطبع writes it with the explicit \n newline. Output is exactly 15\n. The final return 0 / أرجع 0 ends the program normally with process status 0, separately from the printed total.

    Trace the iterations

    Predict the total and the counter after each body execution before running.

    Iteration Counter before body Sum before body Sum after addition Counter after update
    1 1 0 1 2
    2 2 1 3 3
    3 3 3 6 4
    4 4 6 10 5
    5 5 10 15 6

    The next test is 6 <= 5, which is false. There is no sixth body execution. The counter update makes this loop finish. Without it, the condition stays true at 1 and execution cannot reach the print. Stop ends such a browser run; it does not make the loop complete normally. If the initial counter were 6, the body would run zero times and the sum would remain 0.

    Include one more integer

    Change only the upper bound from 5 to 6. Predict the extra iteration. Expected output is exactly 21\n, with normal status 0.

    Change the loop bound to 6. Predict the final sum and the counter that stops the loop.

    Hint: Keep the initial sum, counter and both updates. Change only the number in counter <= 5.

    Show worked answer

    The first five iterations still total 15. A sixth adds 6, giving 21, then advances the counter to 7. The test 7 <= 6 is false, so the loop ends. The program prints 21 and one newline, then returns status 0.

    Browser and native · ASPL 0.7.0 · English source

    Execution: Browser and native · ASPL 0.7.0

    Prerequisites:
    • Read the example explanation in its lesson before running.

    Input: No input lines are required.

    fn start_main() -> i64 {
        var sum: i64 = 0;
        var counter: i64 = 1;
        while counter <= 6 {
            sum = sum + counter;
            counter = counter + 1;
        }
        print(format_i64(sum) + "\n");
        return 0;
    }
    
    Download lesson-loops-solution.aspl

    Expected outcomes
    {
      "browser": {
        "check": "success",
        "class": "success",
        "output": "21\n",
        "processStatus": 0
      },
      "native": {
        "check": 0,
        "status": 0,
        "output": "21\n"
      }
    }

    Check your understanding

    Explain why both bindings are mutable, when the condition runs, and why the final counter is one past the bound. Compare your trace with the output after an explicit Run. No grade or progress is stored, and navigation stays open. Return to the Tutorial path or read the control-flow reference.

    Browse pages