Raised This Month: $51 Target: $400
 12% 

Questions about "for" loop


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Catastrophe
Veteran Member
Join Date: Jul 2012
Location: somewhere between narnia
Old 11-04-2014 , 00:33   Questions about "for" loop
Reply With Quote #1

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 ?
__________________
You will find everything u need :-

Last edited by Catastrophe; 11-04-2014 at 00:34.
Catastrophe is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 11-04-2014 , 00:47   Re: Questions about "for" loop
Reply With Quote #2

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.
__________________
ddhoward is offline
Catastrophe
Veteran Member
Join Date: Jul 2012
Location: somewhere between narnia
Old 11-04-2014 , 03:33   Re: Questions about "for" loop
Reply With Quote #3

Ok thanks man.
__________________
You will find everything u need :-
Catastrophe is offline
GuskiS
Veteran Member
Join Date: Aug 2007
Location: Latvia
Old 11-04-2014 , 10:02   Re: Questions about "for" loop
Reply With Quote #4

Is returning in loop a good way? I have always broke it, then return.
__________________
Finished mods:
Trouble in Terrorist Town
MurderMod
The Hidden
Cowboys vs Indians
JailBreak Supreme
Survival Madness
GuskiS is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-04-2014 , 10:17   Re: Questions about "for" loop
Reply With Quote #5

Quote:
Originally Posted by GuskiS View Post
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.
__________________

Last edited by HamletEagle; 11-04-2014 at 10:18.
HamletEagle is offline
ghost95v
Senior Member
Join Date: Apr 2014
Location: somewhere in universe
Old 11-06-2014 , 01:36   Re: Questions about "for" loop
Reply With Quote #6

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

__________________

Last edited by ghost95v; 11-06-2014 at 01:39.
ghost95v is offline
Send a message via Skype™ to ghost95v
zmd94
Veteran Member
Join Date: Nov 2013
Location: Malaysia (9w2zow).
Old 11-06-2014 , 01:47   Re: Questions about "for" loop
Reply With Quote #7

Why you need it? I don't see that it can be useful in your situation. ;)
zmd94 is offline
ghost95v
Senior Member
Join Date: Apr 2014
Location: somewhere in universe
Old 11-06-2014 , 02:13   Re: Questions about "for" loop
Reply With Quote #8

Quote:
Originally Posted by zmd94 View Post
Why you need it? I don't see that it can be useful in your situation. ;)
ah, ok i thought yes
__________________
ghost95v is offline
Send a message via Skype™ to ghost95v
.Dare Devil.
Veteran Member
Join Date: Sep 2010
Old 11-06-2014 , 06:57   Re: Questions about "for" loop
Reply With Quote #9

Its fine to use return in loop. Other ways we could not use it there if it would not be fine.
.Dare Devil. is offline
simanovich
AlliedModders Donor
Join Date: Jun 2012
Location: Israel
Old 11-06-2014 , 07:33   Re: Questions about "for" loop
Reply With Quote #10

continue - skip to the next step.
break - breaks the loop.
return - breaks the loop and blocking from the rest of the fuction calling
__________________
simanovich is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:50.


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