Correct usage of break.
#1

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?
Reply
#2

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

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

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.
Reply
#5

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?
Reply
#6

Yes, just compile it and test it by yourself
Reply
#7

Solved this two hours ago, thanks for your assistance tho
Reply
#8

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.
Reply
#9

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++; }
Reply
#10

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


Forum Jump:


Users browsing this thread: 1 Guest(s)