SA-MP Forums Archive
[Answered]Loops / Using "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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: [Answered]Loops / Using "break" (/showthread.php?tid=124203)



[Answered]Loops / Using "break" - DeathOnaStick - 29.01.2010

Heyho!
I've got a little question here. First an example:
pawn Код:
Var1=4;
Var2=4;

Var3=0;

while(Var1==Var2)
{
if(Var3>=Var2)break;
Var3++;
}

Var4=Var3;
1. Will the code after the loop be executed, when the loop is stopped?
2. Does it work to use "return 0;" for stopping a loop?
3. If yes, will the code after the loop be executed?
4. Is there a other way to stop the loop, except changing one of the "while"-conditions (here Var1 or Var2)?

Thanks,
DeathOnaStick


Re: [Question]Loops / Using "break" - ray187 - 29.01.2010

This is an endless loop as
Var1=Var2
is always true (you assign Var2 to Var1).

What you want to do is
Var1==Var2
which compares both values.

The code after the loop is executed when the loop has ended.

"break;" is the command to stop a loop.


Re: [Question]Loops / Using "break" - DeathOnaStick - 29.01.2010

Quote:
Originally Posted by ray187
This is an endless loop as
Var1=Var2
is always true (you assign Var2 to Var1).

What you want to do is
Var1==Var2
which compares both values.

The code after the loop is executed when the loop has ended.

"break;" is the command to stop a loop.
True, i just forgot putting the "=" double .



Re: [Question]Loops / Using "break" - ray187 - 29.01.2010

All questions answered?


Re: [Question]Loops / Using "break" - DeathOnaStick - 29.01.2010

Does the code after the loop gets executed when i stop it with "break"?


Re: [Question]Loops / Using "break" - ray187 - 29.01.2010

Quote:

The code after the loop is executed when the loop has ended.

So yeah as the loop ends when using break.


Re: [Question]Loops / Using "break" - DeathOnaStick - 29.01.2010

Does the loop end, too, when i use "return 0;" instead of break?


Re: [Question]Loops / Using "break" - Rac3r - 29.01.2010

I think returning 0 wouldn't really help. Break will terminate the loop, return will terminate the function.

Example:
Код:
foreach(Player,i)
{
  if( i != -1)break; // will break
}
SetPlayerHealth(playerid, 0.0); // and set player health to 0
Код:
foreach(Player,i)
{
  if( i != -1)return 0; // will halt
}
SetPlayerHealth(playerid, 0.0); // set player health to 0 will not be called



Re: [Question]Loops / Using "break" - ray187 - 30.01.2010

.


Re: [Question]Loops / Using "break" - MadeMan - 30.01.2010

break and return both end the loop.

With break the code after the loop will be executed, but with return the code after the loop will not be executed.