All about Loops and Nests... (A Primer)

Read before posting.
User avatar
TJ
Posts: 6586
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1907 times

All about Loops and Nests... (A Primer)

Postby TJ » 24 Apr 2010

FOR

Code: Select all

For Counter = IValue To FValue
Begin
    Instruction1;
    Instruction2;
End;

IValue = initial value
FValue = final value

or:

Code: Select all

For Counter = IValue DownTo FValue
Begin
    Instruction1;
    Instruction2;
End;


example

Code: Select all

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


Code: Select all

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





-------------------------------------------------------
Has this post been helpful to you?
Last edited by TJ on 05 May 2010, edited 1 time in total.

User avatar
TJ
Posts: 6586
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1907 times

Postby TJ » 24 Apr 2010

WHILE

Code: Select all

index = 0;

While
ArrayToSearch[index] < Highest_A( ArrayToSearch, Array_Size ){condition}
Begin
    index = index + 1;  {instruction to repeat untill condition is true}
End;

IndexOfHighestArray = index;


.

User avatar
TJ
Posts: 6586
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1907 times

Postby TJ » 24 Apr 2010

SWITCH (a cascade)

Code: Select all

Switch ( expression )
Begin
   Case case-expression:  statements;

   Default: statements;
End;



example:

Code: Select all

Switch(Value1)
Begin

    Case 1 to 5:
        Value2 = Value2 + 1;

    Case 10, 20, 30:
        Value3 = Highest(High,10);

    Case is > 40:
        Value3 = Value3 + 1;

    Default:
        Value5 = Value5 + 1;

End;



.

User avatar
TJ
Posts: 6586
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1907 times

Postby TJ » 24 Apr 2010

WHILE

Code: Select all

While Condition1 = True
Begin
    Instruction1;
    Instruction2;
    Instruction3;
End;



example

Code: Select all

BarNum = 0;

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


.

User avatar
TJ
Posts: 6586
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1907 times

Postby TJ » 24 Apr 2010

REPEAT

Code: Select all

HighPriceSum = 0;
BarNum  = -1;

Repeat
    BarNum  = BarNum  + 1;
    HighPriceSum = HighPriceSum + High[ BarNum ];
Until BarNum  = 9;


.

bowlesj3
Posts: 2028
Joined: 21 Jul 2007
Has thanked: 174 times
Been thanked: 405 times

Postby bowlesj3 » 24 Apr 2010

BREAK

You can put the BREAK command into a FOR loop or a WHILE loop.

Code: Select all

variables:
   X(0);

   For X = 1 to 100
      begin

          {instructions}
          {instructions}
          {instructions}

          if condition1 = true then
             BREAK;
      end;

   Print(File("C:\A_ForLoopTest.txt"), "X=", " " , X, " " , " ");



.

User avatar
TJ
Posts: 6586
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 971 times
Been thanked: 1907 times

Re: All about Loops and Nests... (A Primer)

Postby TJ » 28 May 2015



Return to “MultiCharts FAQ”