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.

    Conditions

    ASPL 0.7.0 · Current release

    Compare 17, 18 and 19 with threshold 18, assigning equality to the upper branch.

    On this page

    What you will learn

    You will compare a value with a threshold, choose one of two messages, and explain what happens below, at, and above that threshold.

    Before you begin

    Read bindings and arithmetic for initialized names and integer values. This is another independent complete program, including its entry and normal status 0. Open it as a new Home draft without replacing earlier work, then choose Run explicitly.

    Choose a branch

    let age = 17 initializes a name with an integer. age >= 18 asks whether that value is greater than or equal to 18. A comparison produces a Boolean, a value that is either true or false, rather than a number to print.

    if / إذا tests the Boolean condition. When it is true, execution enters the first braced block. else / وإلا supplies the alternative block for a false condition. Exactly one of these blocks executes. After the chosen block, execution continues with the final return. Braces group each branch's instructions; indentation helps you see the groups but does not replace braces.

    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 {
        let age = 17;
        if age >= 18 {
            print("18 or over\n");
        } else {
            print("Under 18\n");
        }
        return 0;
    }
    
    Download lesson-conditions.aspl

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

    For 17, the comparison is false, so the lower branch prints Under 18\n. If it were true, the upper branch would print 18 or over\n. The strings include an explicit newline as in the first lesson. The final return 0 is outside both branches, so either choice ends normally with status 0.

    Predict all three cases

    Keep the threshold at 18. Predict each case before running it. Equality belongs deliberately to the upper branch because the operator is >=.

    Value of age Is age >= 18 true? Chosen branch Exact output
    17 No, it is below 18 else, lower Under 18\n
    18 Yes, it equals 18 if, upper 18 or over\n
    19 Yes, it is above 18 if, upper 18 or over\n

    > would exclude equality, so 18 would take the lower branch instead. = initializes or assigns a binding; == compares two values for equality. Here you need >= to include both equality and larger values.

    Put equality in the upper branch

    Change just the initial value of age from 17 to 18. Leave the comparison unchanged. Expected output is exactly 18 or over\n and normal status 0.

    Change age to 18. Explain why the upper message now prints.

    Hint: Change the integer in the binding declaration. Keep >= 18, which includes equality.

    Show worked answer

    The comparison 18 >= 18 is true, so only the first block executes and prints 18 or over followed by one newline. The lower message is skipped. The shared final return still gives 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 {
        let age = 18;
        if age >= 18 {
            print("18 or over\n");
        } else {
            print("Under 18\n");
        }
        return 0;
    }
    
    Download lesson-conditions-solution.aspl

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

    Now change 18 to 19 in the binding, keeping the threshold 18, and compare your third prediction. This checked complete version is supplied for comparison.

    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 {
        let age = 19;
        if age >= 18 {
            print("18 or over\n");
        } else {
            print("Under 18\n");
        }
        return 0;
    }
    
    Download lesson-conditions-above.aspl

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

    Both 18 and 19 choose the upper message for different reasons: equality and greater-than. Neither run prints both messages.

    Check your understanding

    Explain the three predictions without running first, then compare the actual lines. You are ready when you can point to the condition, the true block, the false block and the final return. The self-check stores no grade or progress and leaves every route open. Return to the Tutorial path or read the control-flow reference.

    Browse pages