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

TJ
Posts: 6973
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1995 times

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

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.

TJ
Posts: 6973
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1995 times
WHILE

Code: Select all

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

.

TJ
Posts: 6973
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1995 times

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;`

.

TJ
Posts: 6973
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1995 times
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;`

.

TJ
Posts: 6973
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1995 times
REPEAT

Code: Select all

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

.

bowlesj3
Posts: 2088
Joined: 21 Jul 2007
Has thanked: 197 times
Been thanked: 416 times
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, " " , " "); `

.

TJ
Posts: 6973
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1995 times