The for loop

From GMod Wiki

Revision as of 15:31, 10 February 2011 by garry :D (Talk | contribs)
Jump to: navigation, search

Now I'll teach you how to use loops. Loops are statements in scripting that let you repeat a code block several times. Each time a code block in a loop runs, it's called looping.

There are two types of loops: The for loop, and the while loop. The for loop is the easiest to explain, and most people use it so I'll start with it.

I'll show you how to use the for loop. A for loop is a type of loop that loops a specific number of times. People prefer to use the for loop, it's much faster to write and there's less of a chance things will go wrong.

 
for i=1,10 do 
	Msg("i is "..i.."\n") 
end 

The example above, simply enough, says, "starting at one and going to ten, tell us what i is."

Lets take this apart...

The word for tells lua that we want to make a loop.

You should be familiar with the next part. i=1,10 is split into two parts. The first does two things - it makes a variable called i and sets it to 1, and it tells the loop we want to use it to keep track of what loop we're on. Each time the loop repeats, it adds one onto i. So when the code is run for the first time, i is 1. Before starting the second loop, 1 is added to i. So, when the code is run for the second time, i is 2. With me so far?

The second part of i=1,10 is the part that says ,10. This tells the for loop that once i is over this number to stop repeating.

Keep in mind that if i is 1, then the next number is always the number of times that the loop will loop. So if the loop has i=1,10 that means the loop will repeat 10 times.

The next part of the loop, do, tells lua that you want the code block between do and end to be repeated until the loop is finished.

Then of course like I said there's the code block. The code block will be automatically run each time the loop, er, loops.

Lastly there's the word 'end'. End tells lua that it's the end of the code block, and that anything past it isn't part of the loop's code block.

This may be a little confusing at first, so let me try to simplify it. This:

 
Msg("Hello!\n") 
Msg("Hello!\n") 
Msg("Hello!\n") 
Msg("Hello!\n") 
Msg("Hello!\n") 
Msg("Hello!\n") 
Msg("Hello!\n") 
Msg("Hello!\n") 
Msg("Hello!\n") 
Msg("Hello!\n") 

Can be written like this:

 
for i=1,10 do 
	Msg("Hello!\n") 
end 

This:

 
Msg("1\n") 
Msg("2\n") 
Msg("3\n") 
Msg("4\n") 
Msg("5\n") 
Msg("6\n") 
Msg("7\n") 
Msg("8\n") 
Msg("9\n") 
Msg("10\n") 

Can be written like this:

 
for i=1,10 do 
	Msg(i.."\n") 
end 

As you can see, for loops can save you a lot of time.

You can use a variable for the number of times. See below:

 
loopHowManyTimes=40 
for i=1,loopHowManyTimes do 
	Msg("Looping at number "..i.."\n") 
end 

You can also set a "step" which is a number that will be added to the variable we want to increase (or decrease) in our for loop

 
for i = 0, 20, 2 do --Increase i by 2 until it reaches 20 or the closest number to the limit.
	print(i)
end
 

Returns:

 
0
2
4
6
8
10
12
14
16
18
20
 

You can use also use steps to make a decreasing for loop

 
for i = 30, 0, 2 do --Decrease i by 2 until it reaches 0
	print(i)
end
 

Returns:

 
30
28
26
24
22
20
18
16
14
12
10
8
6
4
2
0
 

You can loop through a table if you want, too!

 
myTable={} 
myTable[1]="Jeff" 
myTable[2]="Josh" 
myTable[3]="Alan" 
myTable[4]="Ryan" 
myTable[5]="Andrew" 
for i=1,5 do 
	Msg("myTable's "..i.." slot is "..myTable[i].."\n") 
end 
 
//So give this a try!
 
//Open notepad and type the following:
 
--Simple loop 
for i=1,10 do 
	Msg(i.."\n") 
end 
 
--Loop number of times variable says 
howManyLoops=3 
for i=1,howManyLoops do 
	Msg("On loop "..i.." out of "..howManyLoops.."\n") 
end 
 
--Loop through a table 
colors={} 
colors[1]="red" 
colors[2]="green" 
colors[3]="blue" 
colors[4]="orange" 
colors[5]="purple" 
colors[6]="yellow" 
 
for i=1,6 do 
	Msg("Color "..i.." is "..colors[i].."\n") 
end 

Save this as loops.lua and open it up in Garry's Mod.


Yarin Kaul Icon ArrowSquare32 left.png Arithmetic in Lua

Yarin Kaul Icon ArrowSquare32.png Back to Lua Tutorial Series

More tricks with conditions Yarin Kaul Icon ArrowSquare32 right.png


Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox