Looping Commands
<< Click to Display Table of Contents >> Looping Commands |
Looping commands control execution flow of your script. If you wanted to print the word NinjaTrader 100 times, you could either use 100 lines of code or do the same using a looping command in only a few lines of code.
while (Boolean expression)
{
//Do something here
}
// Print NinjaTrader 100 times to the output window |
do
{
//Do something here
}
while (Boolean expression)
// Print NinjaTrader 100 times to the output window |
for (initializer; boolean expression; iterator)
{
//Do something here
}
// Print NinjaTrader 100 times to the output window |
foreach (type identifier in boolean expression)
{
// Do something here
}
// Count the number of oatmeal cookies in a cookie jar |
You can use the break command to exit a loop at any time. The following example is an infinite loop, but we will break out after the first Print statement.
// Exit the infinite loop after the first iteration |
The continue command will jump ahead to the next iteration of the loop. The following example will never print "NinjaTrader," because the continue command sends control back to the top of the loop each time.
// Never prints "NinjaTrader" |