Loops

Loops allow you to execute the same code without rewriting it over and over.  This helps structure your program.  It's important to understand how while and for loops work in Lua, so let's jump right in. In C++, it will be even loopier!

While Loops

The while loop is a statement that keeps on looping over its code until a certain condition is met. The syntax of a while loop is as follows:

while (condition)

{

//code to be executed

}

The example below can be pasted into your "main.cpp" script file to see how while loops work.  When you run this example it will keep looping until the space key is pressed.  Even clicking on the game window won't close it because you have to press Space to exit the loop:

//Create a window

Window* window = Window::Create();

//Create a rendering context

Context* context = Context::Create(window);

//While loop

while (window->KeyHit(Key::Escape)==false)

{

        //Set the drawing color and clear the screen

        context->SetColor(0.0,0.0,0.0);

        context->Clear();

        //Set the drawing color and blend mode

        context->SetColor(1.0,1.0,1.0);

        context->SetBlendMode(Blend::Alpha);

        //Draw some text onscreen

        context->DrawText("PRESS ESCAPE TO END THE PROGRAM.",2,2);

        //Update the screen

        context->Sync();

}

For Loops

The for loop looks a lot like the while loop, but it's a little fancier.  A for loop has a defined starting and ending value.  The loop will be performed a certain number of times until the ending value is reached.. If you want to do something ten times you can use a for loop instead of writing the same code over and over again.

A for loop has the following syntax:

for (startValue, endValue, change)

{

//code to execute

}

The "change" part is a line of code you can execute to make the variable you are looping with change.  Paste the code below into the "Main.lua" and run the game to see how it works:

//For loop

for (int n=1; n<10; n++)

{

//Print out a string

Print("Loop iteration: "..n);

}

When run, the program above will print out the following text:

[quote]Loop iteration: 1

Loop iteration: 2

Loop iteration: 3

Loop iteration: 4

Loop iteration: 5

Loop iteration: 6

Loop iteration: 7

Loop iteration: 8

Loop iteration: 9

Loop iteration: 10[/quote]

Since we have control over the way the variable changes each iteration of the loop, we can do whatever we want with it:

//For loop

for (int n=1; n<10; n+=2)

{

//Print out a string

Print("Loop iteration: "..n);

}

Here is the printed output of the program:

Quote

Loop iteration: 1

Loop iteration: 3

Loop iteration: 5

Loop iteration: 7

Loop iteration: 9

We can even decrease the starting value each loop.  In that case, we would want to start value to be greater than the ending value, or else the loop would continue forever.  The following code will iterate backwards from 10 to one, subtracting one from the n variable each iteration:

--For loop

//For loop

for (int n=10; n>1; n-=2)

{

//Print out a string

Print("Loop iteration: "..n);

}

Here is the printed output of the above program:

[quote]Loop iteration: 10

Loop iteration: 9

Loop iteration: 8

Loop iteration: 7

Loop iteration: 6

Loop iteration: 5

Loop iteration: 4

Loop iteration: 3

Loop iteration: 2

Loop iteration: 1[/quote]

Break

You can exit a loop early at any time with the break keyword.  The example below will iterate through a loop until the fifth iteration, at which point it will encounter the break command and exit the loop early.

//For loop

for (int n=1; n<10; n++)

{

//Exit loop when 5 is reached

if (n==5) break;

}

Print("Loop iteration: "..n);

The printed output of this program looks like this:

[quote]Loop iteration: 1

Loop iteration: 2

Loop iteration: 3

Loop iteration: 4[/quote]

Conclusion

Loops are an important language used to structure code.  In fact your game itself is running in one big loop inside the script interpreter.  Every programming language in existence uses some variation of this idea.