It's better to teach the rule first, then the exception.
also I did tell him that last time but it happened again so I'm clarifying for his further usage until he gets better
...except that it's the other way around.
I find that it's better to explain what an infinite loop is, if the reader is unaware, and to teach why this stuff is required, rather than issuing blanket statements.
Honestly, I sometimes feel as if the world is all gone quite mad
[...]
Here are some while loop examples:
int NotInfiniteLoop()
int x = Rand(100)+1; int n; int n2;
while(n < x ) ++n2; // This while loop runs until the condition is met (evaluates false).
return n2;
}
void InfiniteLoop()
{
int n;
while(1 == 1) // This condition always evaluates true, so the loop never ends.
{
++n; // Will increase forever. ZC seems to hang, because this loop runs infinitely in ONE FRAME of GAME ACTION!!
// ZC does not actually crash, but it is never given the chance to advance a frame.
}
}
void InfiniteLoopWithWait()
{
int n;
while(1 == 1) //this condition always evaluates true, so the loop never ends.
{
++n; Waitframe();
// Will increase forever. but only ONE INCREMENT *PER FRAME* of game action!
// Now, ZC does not seem to hang, but this loop never exits.
}
}
void InfiniteLoopWithBreakCondition()
{
int n;
while(1 == 1) // This condition always evaluates true, so the loop never ends.
// We usually express this as while(true0 or while(1), as in either case, the condition in the statement
// always evaluates as 'true'. In this case, i used an *expression* to facilitate the point.
{
++n;
if ( n > 600 ) break; // No longer infinite, because the loop will break.
}
}
int InfiniteLoopWithReturn()
{
int n;
while(true) // This condition always evaluates true, so the loop never ends.
{
n += Rand(60);
if ( n > 600 ) return n; // No longer infinite, because the loop will break when it returns a value! }
}
int NonInfiniteLoopWithWait
{
bool a = true;
while(a) //runs until a == false
{
if ( Link->HP <= 0 )
{
a = false; // Link's HP falling beneath '1' causes the loop to break by unsetting the condition.
}
Waitframe(); // Each loop cycle occurs in ONE FRAME of GAME ACTION!
}
return Link->HP;
}