[snip]
int iLoop;
for (iLoop = 1; iLoop <= 5; iLoop++)
{
if(Random(2)+1 == 2) iLoop--;
}
[snip]The lexicon does not seem to cover this type of subject regarding the for loop.
TIA!
int iLoop;
for (iLoop = 1; iLoop <= 5; iLoop++)
{
if(Random(2)+1 == 2) iLoop--;
}
[snip]
int iLoop;
for(iLoop=5; iLoop > 0; iLoop--)
{
if(Random(2)+1 == 2)
{
do something
}
}
int iLoop = 5;
for(; iLoop-- > 0;)
{
if(Random(2))
{
do something
}
}
Modifié par ShaDoOoW, 06 septembre 2010 - 01:24 .
Modifié par Invisig0th, 06 septembre 2010 - 01:39 .
Well there is a matter of efficiency, its not less characters, but less commands which matter. Anyway this does not make any difference at performancy, the gain is too small there.Invisig0th wrote...
Dark Defiance,
Most of the experienced professional programmers I know (myself included) prefer the first of these two ways. The second is harder to read/understand at a glance, and can easily lead to errors. Just because one can write something with fewer characters does not mean it is a good idea, or that it has any benefits at all (other than showing off).
Since you also used the syntax of the first one in your own code, it's probably best to stick with what you are familiar with.
Incorrect. Using abbreviated syntax like this does not produce fewer commands to the processor. The FOR loop will be processed the exact same way at the machine language level, regardless of which particular way you choose to type it. So the gain is not "small", because there is actually no gain at all.ShaDoOoW wrote...
Well there is a matter of efficiency, its not less characters, but less commands which matter. Anyway this does not make any difference at performancy, the gain is too small there.
Modifié par Invisig0th, 06 septembre 2010 - 02:24 .
I know too well that nobody can persuade you, because you have always right, so I won't lose my time.Invisig0th wrote...
bull****s
Modifié par ShaDoOoW, 06 septembre 2010 - 06:04 .
Profanity - impressive. Very respectful to the original poster.ShaDoOoW wrote...
Invisig0th wrote...
bull****s
Modifié par Invisig0th, 06 septembre 2010 - 03:10 .
int iLoop;
for (iLoop = 1; iLoop <= 5; iLoop++)
{
if(Random(2)+1 == 2) iLoop--;
}
Modifié par Dark Defiance, 06 septembre 2010 - 03:13 .
Eh, yes, you can, it won't raise infinite loop, I was wrong, the example confused me...Dark Defiance wrote...
I'm sorry, but now I am confused. According to the 1.69 lexicon under
Home > Lyceum > Primer > Conditionals and Loops > for Loop
"If expr2 evaluates to FALSE the loop will break. "
And the code I wrote:int iLoop; for (iLoop = 1; iLoop <= 5; iLoop++) { if(Random(2)+1 == 2) iLoop--; }
Doesn't it mean "if iLoop is less than or equal to 5" continue the loop? So if iLoop reached 6, it would break the loop?
But my main question was can I decrement the loop from within it? In the case above say iLoop was equal to 4 (4th loop), the if statement equaled 2, it would decrement iLoop to equal 3? So when the loop cycles, it would be on its 3rd loop rather then 5th. Poor example? Did I just confuse anyone? lol.
TIA
Dark Defiance wrote...
I'm sorry, but now I am confused. According to the 1.69 lexicon under
Home > Lyceum > Primer > Conditionals and Loops > for Loop
"If expr2 evaluates to FALSE the loop will break. "
And the code I wrote:int iLoop; for (iLoop = 1; iLoop <= 5; iLoop++) { if(Random(2)+1 == 2) iLoop--; }
Doesn't it mean "if iLoop is less than or equal to 5" continue the loop? So if iLoop reached 6, it would break the loop?
But my main question was can I decrement the loop from within it? In the case above say iLoop was equal to 4 (4th loop), the if statement equaled 2, it would decrement iLoop to equal 3? So when the loop cycles, it would be on its 3rd loop rather then 5th. Poor example? Did I just confuse anyone? lol.
TIA
int iLoop;
for (iLoop = 1; iLoop <= 5;)
{
if(Random(2)+1 == 2)
iLoop--;
else
iLoop++;
}