SA-MP Forums Archive
break in loop - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: break in loop (/showthread.php?tid=526243)



break in loop - Jack_Leslie - 16.07.2014

'break' in this loop doesn't seem to be doing what i want it to do.

pawn Код:
for(new i = 1; i < sizeof(HouseData); i++)
            {
                if(HouseData[i][Created] == 0) houseid = i;
                break;
            }
So obviously I have it start at 1, because I don't want a houseid to be 0, the first time it works fine and houseid ends up being 1, but after that, it keeps being 0, instead of 2, 3 etc.

If I don't use break, the houseid starts from 499 and descends (housedata has a limit of 500)


Re: break in loop - Vince - 16.07.2014

That's because the break is unconditional and will always break at the first iteration. Use braces.


Re: break in loop - Jack_Leslie - 16.07.2014

Quote:
Originally Posted by Vince
Посмотреть сообщение
That's because the break is unconditional and will always break at the first iteration. Use braces.
Unsure if I've done it right, because when the code executed it, it stopped executing and I couldn't input anymore commands (like froze the game, but I could still control the game..)

pawn Код:
for(new i = 1; i < sizeof(HouseData); i++)
            {
                while (HouseData[i][Created] == 0)
                {
                    houseid = i;
                }
            }



Re: break in loop - Stanford - 16.07.2014

pawn Код:
for(new i = 1; i < sizeof(HouseData); i++)
{
     if(HouseData[i][Created] == 0)
     {
               houseid = i;
               break;
     }
}



Re: break in loop - Jack_Leslie - 16.07.2014

Quote:
Originally Posted by Stanford
Посмотреть сообщение
pawn Код:
for(new i = 1; i < sizeof(HouseData); i++)
{
     if(HouseData[i][Created] == 0)
     {
               houseid = i;
               break;
     }
}
Worked fine, thank you.