[Answered]Loops / Using "break"
#1

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

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

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

All questions answered?
Reply
#5

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

Quote:

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

So yeah as the loop ends when using break.
Reply
#7

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

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

.
Reply
#10

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


Forum Jump:


Users browsing this thread: 1 Guest(s)