DO

Category: Instructions

The DO instruction in ProBuilder language is used within looping constructs such as FOR/NEXT and WHILE loops. Its primary purpose is to clearly delineate the start of the actions that should be executed within each iteration of the loop. This helps in organizing the code and making the loop’s structure more readable and maintainable.

Syntax

The syntax for using the DO instruction is straightforward. It is placed after the loop declaration and before the actions that are to be executed in each iteration:


FOR variable = start TO end DO
    // Actions to execute
NEXT

Similarly, it can be used in a WHILE loop as follows:


WHILE condition DO
    // Actions to execute
WEND

Example

Here is a simple example using the DO instruction within a FOR/NEXT loop. This example counts how many times the closing price of a stock is less than its opening price over the last 20 bars:


Value = 0
FOR i = 1 TO 20 DO
    IF (Close[i] < Open[i]) THEN
        Value = Value + 1
    ELSE
        BREAK
    ENDIF
NEXT
RETURN Value

In this example, the loop iterates from 1 to 20. Within each iteration, it checks if the closing price is less than the opening price. If true, it increments the Value by 1. If not, it exits the loop using the BREAK instruction.

Additional Information

  • The DO instruction itself does not perform any operations but serves as a structural marker in the code.
  • Using DO is optional but recommended for clarity, especially in complex loops with multiple conditions and actions.
  • The BREAK instruction within the loop provides a way to exit the loop prematurely based on a condition, enhancing control over the loop execution.

Understanding and using the DO instruction effectively can help in writing clearer and more efficient ProBuilder scripts, especially when dealing with repetitive tasks or conditions that need to be checked multiple times.

Related Instructions:

  • BREAK instructions
  • DOWNTO instructions
  • FOR instructions
  • NEXT instructions
  • TO instructions
  • Logo Logo
    Loading...