Difference between revisions of "Repetition - 2017"
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
− | Repeats a section of code until a set of parameters are met (or one parameter). | + | Repeats a section of code until a set of parameters are met (or one parameter). |
+ | |||
+ | ==For Loop== | ||
+ | the for loop will run a given number of times (often called definite): | ||
+ | |||
From Warren; Line 16: | From Warren; Line 16: | ||
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
Line 8: | Line 12: | ||
</syntaxhighlight> <br /> | </syntaxhighlight> <br /> | ||
The for loop creates a new entry in the Rabbits array, using the Rabbit class, until the variable r reaches the RabbitCount variable.<br /><br /> | The for loop creates a new entry in the Rabbits array, using the Rabbit class, until the variable r reaches the RabbitCount variable.<br /><br /> | ||
+ | |||
From Simulation; Line 88: | From Simulation; Line 88: | ||
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
Line 45: | Line 50: | ||
} | } | ||
} | } | ||
+ | </syntaxhighlight><br /> | ||
+ | This loop creates a set of y values for every x value to create a 'square' of values in a multi-dimensional array. | ||
+ | |||
+ | ==While Loop== | ||
+ | A while loop will check the condition first, so may never run but also you don't know when it will stop. | ||
+ | |||
+ | <syntaxhighlight lang="csharp"> | ||
+ | private void CompressRabbitList(int DeathCount) | ||
+ | { | ||
+ | if (DeathCount > 0) | ||
+ | { | ||
+ | int ShiftTo = 0; | ||
+ | int ShiftFrom = 0; | ||
+ | while (ShiftTo < RabbitCount - DeathCount) | ||
+ | { | ||
+ | while (Rabbits[ShiftFrom] == null) | ||
+ | { | ||
+ | ShiftFrom++; | ||
+ | } | ||
+ | if (ShiftTo != ShiftFrom) | ||
+ | { | ||
+ | Rabbits[ShiftTo] = Rabbits[ShiftFrom]; | ||
+ | } | ||
+ | ShiftTo++; | ||
+ | ShiftFrom++; | ||
+ | } | ||
+ | RabbitCount = RabbitCount - DeathCount; | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==Do While Loop== | ||
+ | This will always run once, and then check the condition to see if it should continue | ||
+ | |||
+ | <syntaxhighlight lang="csharp"> | ||
+ | do | ||
+ | { | ||
+ | Mate = Rnd.Next(0, RabbitCount); | ||
+ | } | ||
+ | while ((Mate == r) || (Rabbits[Mate].IsFemale())); | ||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 06:27, 26 May 2017
Repeats a section of code until a set of parameters are met (or one parameter).
For Loop
the for loop will run a given number of times (often called definite):
From Warren; Line 16:
for (int r = 0; r < RabbitCount; r++)
{
Rabbits[r] = new Rabbit(Variability);
}
The for loop creates a new entry in the Rabbits array, using the Rabbit class, until the variable r reaches the RabbitCount variable.
From Simulation; Line 88:
for (int x = 0; x < LandscapeSize; x++)
{
for (int y = 0; y < LandscapeSize; y++)
{
if (Landscape[x, y].Warren != null)
{
if (ShowDetail)
{
Console.WriteLine("Warren at (" + x + "," + y + "):");
Console.Write(" Period Start: ");
Landscape[x, y].Warren.Inspect();
}
if (FoxCount > 0)
{
FoxesEatRabbitsInWarren(x, y);
}
if (Landscape[x, y].Warren.NeedToCreateNewWarren())
{
CreateNewWarren();
}
Landscape[x, y].Warren.AdvanceGeneration(ShowDetail);
if (ShowDetail)
{
Console.Write(" Period End: ");
Landscape[x, y].Warren.Inspect();
Console.ReadKey();
}
if (Landscape[x, y].Warren.WarrenHasDiedOut())
{
Landscape[x, y].Warren = null;
WarrenCount--;
}
}
}
}
This loop creates a set of y values for every x value to create a 'square' of values in a multi-dimensional array.
While Loop
A while loop will check the condition first, so may never run but also you don't know when it will stop.
private void CompressRabbitList(int DeathCount)
{
if (DeathCount > 0)
{
int ShiftTo = 0;
int ShiftFrom = 0;
while (ShiftTo < RabbitCount - DeathCount)
{
while (Rabbits[ShiftFrom] == null)
{
ShiftFrom++;
}
if (ShiftTo != ShiftFrom)
{
Rabbits[ShiftTo] = Rabbits[ShiftFrom];
}
ShiftTo++;
ShiftFrom++;
}
RabbitCount = RabbitCount - DeathCount;
}
}
Do While Loop
This will always run once, and then check the condition to see if it should continue
do
{
Mate = Rnd.Next(0, RabbitCount);
}
while ((Mate == r) || (Rabbits[Mate].IsFemale()));