Difference between revisions of "Loop"

From Mario Fan Games Galaxy Wiki
(Filled out the basics. Anyone want to expand?)
 
m
Line 1: Line 1:
A loop in programming is a set of code that is run repeatedly, either until a condition has been met(like the program ending) or it has been run a certain number of times. In C-like languages, such as C, C++, and GML, it is written as either a for loop(with an initializer, condition, and increment, usually for running a loop a certain number of times), or a while loop(i.e. while a condition is not true, do the loop).
+
A '''loop''' is a construct that repeatedly executes a set of code, either until a condition has been met (such as the program ending) or it has been run a certain number of times. Many languages provide a few different types of loops that are used for different purposes. Common loop types are ''for'' loops, ''while'' loops, ''do while'' or ''repeat'' loops, and ''foreach'' loops.
 +
 
 +
=== For ===
 +
'''For''' loops run a certain number of times. There are different implementations, but usually they contain an initialization statement, a comparison statement, and a step value. Sometimes a for loop doesn't take an expression but instead an ending value.
 +
 
 +
=== Foreach ===
 +
'''Foreach''' loops are often used in conjunction with [[iteration|iterable]] objects. Rather than having the three different statements of a normal ''for'' loop, a list of variables to use for returned values and an expression for the iterable object or a generator function for that object.
 +
 
 +
=== While ===
 +
A '''while''' loop executes as long as its expression evaluates to a true value. They are often used to implement "infinite" loops in cases where it is unknown exactly how many times the code needs to execute.
 +
 
 +
=== Repeat/Do While ===
 +
'''Do while''' loops are similar to ''while'' loops, except their conditional statement is at the end of the loop body and therefore guarantees the loop will execute at least once. '''Repeat''' or '''repeat until''' loops treat their conditional statement differently; they loop as long as the condition is ''false'' rather than true. However, they also guarantee the loop body runs at least once.
 +
 
 +
== [[C++]] ==
 +
 
 +
== [[GML]] ==
 +
 
 +
== [[Lua]] ==
 +
<source lang="lua" enclose="div">
 +
for i = 1, 10 do print(i) end -- for
 +
for i, v in ipairs {'a', 'b', 'c', 'd', 'e'} do print(i, v) end -- foreach
 +
 
 +
local i = 0
 +
while i < 10 do print(i) i = i + 1 end -- while
 +
 
 +
local i = 0
 +
repeat print(i) i = i + 1 until i == 10 -- repeat-until
 +
</source>

Revision as of 03:34, 30 March 2010

A loop is a construct that repeatedly executes a set of code, either until a condition has been met (such as the program ending) or it has been run a certain number of times. Many languages provide a few different types of loops that are used for different purposes. Common loop types are for loops, while loops, do while or repeat loops, and foreach loops.

For

For loops run a certain number of times. There are different implementations, but usually they contain an initialization statement, a comparison statement, and a step value. Sometimes a for loop doesn't take an expression but instead an ending value.

Foreach

Foreach loops are often used in conjunction with iterable objects. Rather than having the three different statements of a normal for loop, a list of variables to use for returned values and an expression for the iterable object or a generator function for that object.

While

A while loop executes as long as its expression evaluates to a true value. They are often used to implement "infinite" loops in cases where it is unknown exactly how many times the code needs to execute.

Repeat/Do While

Do while loops are similar to while loops, except their conditional statement is at the end of the loop body and therefore guarantees the loop will execute at least once. Repeat or repeat until loops treat their conditional statement differently; they loop as long as the condition is false rather than true. However, they also guarantee the loop body runs at least once.

C++

GML

Lua

for i = 1, 10 do print(i) end -- for
for i, v in ipairs {'a', 'b', 'c', 'd', 'e'} do print(i, v) end -- foreach

local i = 0
while i < 10 do print(i) i = i + 1 end -- while

local i = 0
repeat print(i) i = i + 1 until i == 10 -- repeat-until