Loop#

for and while loop syntax allows the program to repeatedly execute instructions inside the loop until a certain number of iterations is reached or the logical conditions are no longer met.

break and continue syntax allows the program to exit the loop midway.

#return syntax allows the program to exit the current script execution.

for Loop#

The for loop uses a numerical variable to count, and is given an initial value and a final value. The for loop is used with the reserved word To or DownTo.

Used with To#

When used with To, the value of Counter starts from IValue. Each time the loop executes, the value of Counter is incremented by one. When Counter is greater than FValue, the loop exits and no longer executes.

1
2
3
4
For Counter = IValue To FValue Begin
  Instruction_1;
  Instruction_2;
End;
Syntax ElementDescription
CounterNumerical variable, used as the loop counter
IValueInitial value of the counter
FValueFinal value of the counter

Used with DownTo#

When used with DownTo, the value of Counter starts from IValue. Each time the loop executes, the value of Counter is decremented by one. When Counter is less than FValue, the loop exits and no longer executes.

1
2
3
4
For Counter = IValue DownTo FValue Begin
  Instruction_1;
  Instruction_2;
End;

Example#

Used with To: From bar 0 to bar 9, accumulate the highest price into HighPriceSum.

1
2
3
4
5
Variables: BarBackNo(0), HighPriceSum(0);

For BarBackNo = 0 To 9 Begin
  HighPriceSum = HighPriceSum + High[BarBackNo];
End;

Used with DownTo: From bar 9 to bar 0, accumulate the highest price into HighPriceSum.

1
2
3
4
5
Variables: BarBackNo(0), HighPriceSum(0);

For BarBackNo = 9 DownTo 0 Begin
  HighPriceSum = HighPriceSum + High[BarBackNo];
End;

while Loop#

The while loop evaluates the logical expression before each execution. If the result is true, the instructions inside the begin/end block are executed; if the result is false, the loop exits.

Syntax#

1
2
3
4
While Logical_Expression Begin
  Instruction_1;
  Instruction_2;
End;
Syntax ElementDescription
Logical_ExpressionLogical expression, evaluated before each loop iteration
InstructionInstructions executed when the logical expression is true

Example#

From bar 0 to bar 9, accumulate the highest price into HighPriceSum. This is equivalent to the for loop used with To example above.

1
2
3
4
5
6
7
Variables: BarBackNo(0), HighPriceSum(0);

BarBackNo = 0;
While BarBackNo < 10 Begin
  HighPriceSum = HighPriceSum + High[BarBackNo];
  BarBackNo = BarBackNo + 1;
End;

Exiting Loops and Scripts#

The for and while loops will continue to execute until the conditions are no longer met. However, sometimes you need to exit the loop midway. PowerLanguage provides the break and continue instructions to exit loops midway, and the #return instruction to exit the script.

break#

When the program executes to break, it exits and ends the entire loop.

Syntax#

1
break;

Example#

for loop with break: Iterate dayCount from 0 to 6. When dayCount equals 2, the entire loop ends. At this point, the value of total is 2.

1
2
3
4
5
6
Variables: dayCount(0), total(0);

For dayCount = 0 To 6 Begin
  If dayCount = 2 Then break;
  total = total + 1;
End;

while loop with break: Equivalent to the for loop example above.

1
2
3
4
5
6
7
8
9
Variables: dayCount(0), total(0);

dayCount = 0;
While dayCount < 7 Begin
  If dayCount = 2 Then break;

  total = total + 1;
  dayCount = dayCount + 1;
End;

continue#

When the program executes to continue, it skips the remaining instructions in the current iteration and proceeds to the next iteration. continue is different from break; continue only skips the current iteration and does not end the entire loop.

Syntax#

1
continue;

Example#

for loop with continue: Iterate dayCount from 0 to 6. When dayCount equals 2, the accumulation of total is skipped. The final value of total is 6.

1
2
3
4
5
6
Variables: dayCount(0), total(0);

For dayCount = 0 To 6 Begin
  If dayCount = 2 Then continue;
  total = total + 1;
End;

When using continue in a while loop, pay special attention: if the counter increment instruction is after continue, continue will skip the increment, which may cause an infinite loop. The solution is to increment the counter before continue.

while loop with continue: When dayCount equals 2, increment dayCount first and then execute continue to avoid an infinite loop. This is equivalent to the for loop example above. The final value of total is 6.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Variables: dayCount(0), total(0);

dayCount = 0;
While dayCount < 7 Begin
  If dayCount = 2 Then Begin
    dayCount = dayCount + 1;
    continue;
  End;

  total = total + 1;
  dayCount = dayCount + 1;
End;

#return#

The #return instruction is similar to break, but #return ends the current script execution, not just the loop.

When the program executes to #return, the current script execution ends, and according to the script execution mechanism, the script is re-executed when the next execution condition is met (Ex: bar completion).

Syntax#

1
#return;

Reference#

https://www.multicharts.com/trading-software/index.php?title=Category:Comparisons_and_Loops

https://www.multicharts.com/trading-software/index.php?title=Return

https://www.multicharts.com/trading-software/index.php?title=While

https://www.multicharts.com/trading-software/index.php?title=For

© 2026 CodeReindeer. All rights reserved.