AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Questions about "for" loop (https://forums.alliedmods.net/showthread.php?t=251017)

Catastrophe 11-04-2014 00:33

Questions about "for" loop
 
How does the for loop respond too "continue", "break" and "return" ?

If i want to stop looping and jump to the code after the loop after some condition is satisfied which one should i use.

Example:

PHP Code:

public myfunc(id)
{
          for(new 
1<= max_playersi++)
          {
                   if(
some condition that was satisfied)
                   {
                   
want to stop looping 
                   
return/continue/break ?
                   }
          }

          
Still want to execute this code(id)
          {

          }



If i want to jump to the next loop in the same condition then which one ?

Similarly if i want to just return the value and stop executing the ENTIRE function then ?

ddhoward 11-04-2014 00:47

Re: Questions about "for" loop
 
return will stop the entire function "myfunc"

continue will stop the current iteration of the loop, and begin the next one

break will stop the loop immediately, and continue on with the rest of the function.

Catastrophe 11-04-2014 03:33

Re: Questions about "for" loop
 
Ok thanks man.

GuskiS 11-04-2014 10:02

Re: Questions about "for" loop
 
Is returning in loop a good way? I have always broke it, then return.

HamletEagle 11-04-2014 10:17

Re: Questions about "for" loop
 
Quote:

Originally Posted by GuskiS (Post 2220019)
Is returning in loop a good way? I have always broke it, then return.

If you refer to this
Code:
break return
It's invalid, the compiler will throw a warning, because you have no reason to do it.
I don't see a problem in using "return" in a loop, if you don't want to continue with the function, if the loop reach something, return is just fine. This all depends on what you need.

Code:
loop {     if( iterator % 3 == 0 )     {          break     } } //some code here //end of public

By using break the block of code outside the loop will still execute. If you will use return it won't.

ghost95v 11-06-2014 01:36

Re: Questions about "for" loop
 
should i "break" somewhere the loop here ?. ( this is blocking some items for others after 1 user bought one time in 1 map )

PHP Code:

else  if (itemid == g_survivor)
        {
            if(
has_mod[player])
            {
                
chat_color(player"!g[StreetZM]!y You !galready have bought !ymod in this map!")
                return 
ZP_PLUGIN_HANDLED;
            }
            if(
has_survivor[player])
            {
            
chat_color(player"!g[ZP]!y It!g was a Survivor!y in this map!")
            return 
ZP_PLUGIN_HANDLED;
            }
            
zp_make_user_survivor(player)
            
has_mod[player] = true
            
for (player 1player <= g_maxplayersplayer++)
            {
            
has_survivor[player] = true
            
}
        }
else  if (
itemid == g_sniper)
        {
            if(
has_mod[player])
            {
                
chat_color(player"!g[StreetZM]!y You !galready have bought !ymod in this map!")
                return 
ZP_PLUGIN_HANDLED;
            }
            if(
has_sniper[player])
            {
            
chat_color(player"!g[StreetZM]!y You !galready have bought mod !yin this map!")
            return 
ZP_PLUGIN_HANDLED;
            }
            
zp_make_user_sniper(player)
            
has_mod[player] = true
            
for (player 1player <= g_maxplayersplayer++)
            {
            
has_sniper[player] = true
            
}
        }
        
g_buyable false    
        
return PLUGIN_CONTINUE



zmd94 11-06-2014 01:47

Re: Questions about "for" loop
 
Why you need it? I don't see that it can be useful in your situation. ;)

ghost95v 11-06-2014 02:13

Re: Questions about "for" loop
 
Quote:

Originally Posted by zmd94 (Post 2220674)
Why you need it? I don't see that it can be useful in your situation. ;)

ah, ok :P i thought yes :D

.Dare Devil. 11-06-2014 06:57

Re: Questions about "for" loop
 
Its fine to use return in loop. Other ways we could not use it there if it would not be fine.

simanovich 11-06-2014 07:33

Re: Questions about "for" loop
 
continue - skip to the next step.
break - breaks the loop.
return - breaks the loop and blocking from the rest of the fuction calling


All times are GMT -4. The time now is 18:14.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.