Difference between revisions of "While"

From MultiCharts
Jump to navigation Jump to search
(Created page with "Used in combination with Begin and End to form a conditional loop statement that will execute a set of instructions repeatedly as long as a logical expression is true....")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
Used in combination with [[Begin]] and [[End]] to form a conditional loop statement that will execute a set of instructions repeatedly as long as a logical expression is true. If the logical expression is not true, the instructions will not be executed.
 
Used in combination with [[Begin]] and [[End]] to form a conditional loop statement that will execute a set of instructions repeatedly as long as a logical expression is true. If the logical expression is not true, the instructions will not be executed.
  
[[Begin]] and [[End]] statements are used to group instructions for conditional execution; a [[Begin]] must always be followed by an [[End.]]
+
[[Begin]] and [[End]] statements are used to group instructions for conditional execution; a [[Begin]] must always be followed by an [[End]].
  
[[Begin]] should not be followed by a semicolon (;), code lines within an instruction group should end with a semicolon (;), and [[End]] should be followed by a semicolon (;).
+
== Usage ==
 
==== Usage ====
 
 
<syntaxhighlight>While E Begin
 
<syntaxhighlight>While E Begin
I1;
+
  I1;
I2;
+
  I2;
I3;
+
  I3;
 
End;</syntaxhighlight>
 
End;</syntaxhighlight>
  
Where: [[E]] - a true/false expression
+
Where:  
  
[[I]] - conditional instructions
+
:'''E''' - a true/false expression.
 +
:'''I''' - conditional instructions.
  
==== Example ====
+
== Notes ==
Add the high prices of the last 10 bars to the [[HighPriceSum]] variable:
+
* [[Begin]] should not be followed by a semicolon (;), code lines within an instruction group should end with a semicolon (;), and [[End]] should be followed by a semicolon (;).
 +
* Important: if the loop doesn't contain code that will cause the logical expression to evaluate to false, you'll be stuck in an ''infinite loop'', which will cause MultiCharts to freeze. For example, the code below '''is not correct''':
 +
<syntaxhighlight>
 +
while (value1 < 10) begin
 +
 
 +
Print("Value1 is: ", value1);
 +
 
 +
end;
 +
value1 = value1 + 1; // This should be placed in the while-loop, not outside of it
 +
</syntaxhighlight>
 +
 
 +
== Example ==
 +
Add the high prices of the last 10 bars to the ''HighPriceSum'' variable:
 +
 
 +
<syntaxhighlight>BarBackNo = 0;
 +
 
 +
While BarBackNo < 10 Begin
 +
 
 +
  HighPriceSum = HighPriceSum + High[BarBackNo];
 +
  BarBackNo = BarBackNo + 1;
  
<syntaxhighlight>BarBackNo=0;
 
While BarBackNo<10 Begin
 
HighPriceSum=HighPriceSum+High[BarBackNo];
 
BarBackNo=BarBackNo+1;
 
 
End;</syntaxhighlight>
 
End;</syntaxhighlight>
  
 
[[Category:Comparisons and Loops]]
 
[[Category:Comparisons and Loops]]

Latest revision as of 11:22, 19 February 2012

Used in combination with Begin and End to form a conditional loop statement that will execute a set of instructions repeatedly as long as a logical expression is true. If the logical expression is not true, the instructions will not be executed.

Begin and End statements are used to group instructions for conditional execution; a Begin must always be followed by an End.

Usage

While E Begin
  I1;
  I2;
  I3;
End;

Where:

E - a true/false expression.
I - conditional instructions.

Notes

  • Begin should not be followed by a semicolon (;), code lines within an instruction group should end with a semicolon (;), and End should be followed by a semicolon (;).
  • Important: if the loop doesn't contain code that will cause the logical expression to evaluate to false, you'll be stuck in an infinite loop, which will cause MultiCharts to freeze. For example, the code below is not correct:
while (value1 < 10) begin

	Print("Value1 is: ", value1);

end;	
value1 = value1 + 1; 	// This should be placed in the while-loop, not outside of it

Example

Add the high prices of the last 10 bars to the HighPriceSum variable:

BarBackNo = 0;

While BarBackNo < 10 Begin

  HighPriceSum = HighPriceSum + High[BarBackNo];
  BarBackNo = BarBackNo + 1;

End;