SA-MP Forums Archive
continue and return 0 and return 1 and break work? - 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: continue and return 0 and return 1 and break work? (/showthread.php?tid=607131)



continue and return 0 and return 1 and break work? - MBilal - 15.05.2016

Well i have question is continue and return 0; work same?
Код:
UnoccupiedVehicle(vehicleid)
{
  foreach(Player, i)
  {
      if(IsPlayerInVehicle(i,vehicleid)) return 0;
  }
  return 1;
}
Like this i tried that

Код:
UnoccupiedVehicle(vehicleid)
{
  foreach(Player, i)
  {
      if(IsPlayerInVehicle(i,vehicleid)) continue;
  }
  return 1;
}
look like both function work same?

I know continue skip that thing and move to code at top where it start for next iteration.
and break can break the loop and don't stop execute data after that break.

But kindly difference between those.
return 1 between break?
and
return 0 between continue?

what is difference?


Re: continue and return 0 and return 1 and break work? - zPain - 15.05.2016

You should use the one with return.

Break will abruptly exit the loop. Continue will ignore the rest of the current iteration and move on to the the next one.


Re: continue and return 0 and return 1 and break work? - MBilal - 15.05.2016

Код:
UnoccupiedVehicle(vehicleid)
{
  foreach(Player, i)
  {
      if(IsPlayerInVehicle(i,vehicleid)) continue;
  }
  return 1;
}
Why this code didn't skip my id when i was in vehicle?

using return 0;

its skipping my id why?


Re: continue and return 0 and return 1 and break work? - KevinReinke - 15.05.2016

Using continue in a loop will skip the code under the keyword and go back to the start of the loop. Returning a value in a loop will break the loop, exit the function and return a value.

Quote:
Originally Posted by MBilal
Посмотреть сообщение
Код:
UnoccupiedVehicle(vehicleid)
{
  foreach(Player, i)
  {
      if(IsPlayerInVehicle(i,vehicleid)) continue;
  }
  return 1;
}
Why this code didn't skip my id when i was in vehicle?

using return 0;

its skipping my id why?
Because you aren't inside the specified vehicle ID, most likely.


Re: continue and return 0 and return 1 and break work? - MBilal - 15.05.2016

Quote:
Originally Posted by KevinReinke
Посмотреть сообщение
Using continue in a loop will skip the code under the keyword and go back to the start of the loop. Returning a value in a loop will break the loop, exit the function and return a value.



Because you aren't inside the specified vehicle ID, most likely.
I was inside vehicle i tested my self.

the code with return 0 work.
but the code with continue not work.

I want to know why continue not working and return 0 working?


Re: continue and return 0 and return 1 and break work? - zPain - 15.05.2016

You're supposed to return 0/false when you detect someone inside the vehicle. Continue is starting a new iteration rather than stopping the current one, which is leading the function to ultimately return 1.


Re: continue and return 0 and return 1 and break work? - KevinReinke - 15.05.2016

Quote:
Originally Posted by MBilal
Посмотреть сообщение
I was inside vehicle i tested my self.

the code with return 0 work.
but the code with continue not work.

I want to know why continue not working and return 0 working?
Try using this:

UnoccupiedVehicle(GetPlayerVehicleID(playerid));


Re: continue and return 0 and return 1 and break work? - MBilal - 15.05.2016

Quote:
Originally Posted by zPain
Посмотреть сообщение
You're supposed to return 0/false when you detect someone inside the vehicle. Continue is making the loop start a new iteration rather than stopping the current one, which is leading the function to ultimately return 1.
Thank all