SA-MP Forums Archive
Correct usage of break. - 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: Correct usage of break. (/showthread.php?tid=562443)



Correct usage of break. - Alvin007 - 08.02.2015

Hello all.
I'm having a few problems with my script, i searched everywhere in the script, still i'm thinking that i did'nt use the "break" correctly.

For example
PHP код:
PlayaInfo[playerid][0][1]==0
So ..
PHP код:
for(new i=0i<=3i++)
        {
            if(
PlayaInfo[playerid][0][i]==0) break;
            
thebiz=i;
        } 
The loop above will return a value of '1' and place it in the array 'thebiz" ?
Or no?


Re: Correct usage of break. - Schneider - 08.02.2015

thebiz=i; will never get executed, since you used 'break' before it and 'break' stops the loop.


Re : Correct usage of break. - Alvin007 - 08.02.2015

PHP код:
for(new i=0i<=3i++)
        {
            if(
PlayaInfo[playerid][0][i]==0)
            {
            
thebiz=i;
            break;
            }
        } 
Like this?


Re: Correct usage of break. - PowerPC603 - 08.02.2015

pawn Код:
for (new i; i < 100; i++)
{
    if (i == 50) break;

    // Do something else here
}
Take this little example.
Normally, the loop would run 100 times, right?
Since the for-loop is told to run from 0 to 99, it should run 100 times.

But in the code is some part where it checks the value of i, and if it's equal to 50, it executes "break".
So in fact, the loop will only run 51 times (from 0 to 50).
The code below the if-statement will only be executed 50 times (from 0 to 49), because when it's running the 51st run, i will be equal to 50, the loop is exited and the code below the if-statement won't be executed anymore.



The code you posted will run from 0 to 3, that's 4 times.
Unless either these array-indices hold a value of 0, then it will be exited:
PlayaInfo[playerid][0][0]
PlayaInfo[playerid][0][1]
PlayaInfo[playerid][0][2]
PlayaInfo[playerid][0][3]

If none of these holds 0, the loop will end by itself and variable "thebiz" will hold the value 3.

If PlayaInfo[playerid][0][1] holds the value 0, thebiz will hold 0, as i was 0 the last time before the loop was exited.

Basically:
- if PlayaInfo[playerid][0][0] holds 0, thebiz will hold it's previous value, whatever is was because "break" will prevent thebiz from taking over the value of i, as the loop is exited before the code is reached.
- if PlayaInfo[playerid][0][1] holds 0, thebiz will hold 0.
- if PlayaInfo[playerid][0][2] holds 0, thebiz will hold 1.
- if PlayaInfo[playerid][0][3] holds 0, thebiz will hold 2.
- if none of these hold 0, thebiz will hold 3.


Re : Correct usage of break. - Alvin007 - 09.02.2015

Thanks for clarifying
Could you please tell me if it works this way.
Example:
PHP код:
PlayaInfo[playerid][0][2]=0
PHP код:
for(new i=0i<=3i++) 
        { 
            if(
PlayaInfo[playerid][0][i]==0
            { 
            
thebiz=i
            break; 
            } 
        } 
This look will stop when i=2 right?
And thebiz will have the number 2?


AW: Correct usage of break. - Nero_3D - 09.02.2015

Yes, just compile it and test it by yourself


Re : Correct usage of break. - Alvin007 - 09.02.2015

Solved this two hours ago, thanks for your assistance tho


Respuesta: Correct usage of break. - CuervO - 09.02.2015

For the record, something which no one specified: you can use continue to "break" a loop on the current iteration and continue on the next one.

If in your prime example you used continue instead of break it would've done what you had intended; however, the loop will still run even after you find your value, so for efficency you use break once you find the value so it doesn't run anymore.


Re: Correct usage of break. - Vince - 09.02.2015

My programming teacher taught us that break is evil (I disagree, it has it purposes) but in this case it might be better solved with a while loop.

pawn Код:
new i = 0;
while(PlayaInfo[playerid][0][i] != 0 && i <= 3) { i++; }



Re : Correct usage of break. - Alvin007 - 09.02.2015

Yeah While and Do While are the ones that must be used in this case.
And thanks for explanation CuervO