Announcement

Collapse

The Infinity Blade Forums Have Moved

We've launched brand new Infinity Blade forums with improved features and revamped layout. We've also included a complete archive of the previous posts. Come check out the new Infinity Blade forums.
See more
See less

[solved] Continue; for an outer loop?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • [solved] Continue; for an outer loop?

    Hello,

    a short code example showing the problem:
    Code:
    local Actor a;
    local int i;
    
    foreach AllActors(class'Actor', a)
    {
    	for (i = 0; i < 10; i++)
    	{
    		if (i == 5)
    		{
    			`log("Continue");
    			//what I want to happen: skip the rest of the for loop and continue the iterator
    			//what actually happens: the for loop continues
    			continue;
    		}
    	}
    }
    I know it's possible in JAVA, is there similar functionality for Unrealscript?
    It's not a big deal to work around this issue (avoiding goto), just curious if I can be lazy and use something built-in


    Thank you.

  • #2
    Try break; instead of continue;
    That should just break it out of the for loop and not the foreach.

    Comment


    • #3
      Super fast response

      I my case there will be more code after the for loop that I do not want to run. Break would only skip the for and then run whatever comes after.

      Code:
      local Actor a;
      local int i;
      
      foreach AllActors(class'Actor', a)
      {
      	for (i = 0; i < 10; i++)
      	{
      		if (i == 5)
      		{
      			break;
      		}
      	}
      
      	//more stuff here, break doesn't prevent this to execute
      }
      So something like this would work, but I was hoping to avoid that (and use a similar syntax to java)
      Code:
      local bool bContinue;
      local Actor a;
      local int i;
      
      foreach AllActors(class'Actor', a)
      {
      	for (i = 0; i < 10; i++)
      	{
      		if (i == 5)
      		{
      			`log("break");
      			bContinue = true;
      			break;
      		}
      	}
      
      	if (bContinue)
      	{
      		`log("continue");
      		bContinue = false;
      		continue;
      	}
      
      	//more stuff here, only reached if bContinue was not set in the for loop
      }

      Thanks again.

      Comment


      • #4
        Use return.

        Code:
        local Actor a;
        local int i;
        
        foreach AllActors(class'Actor', a)
        {
        	for (i = 0; i < 10; i++)
        	{
        		if (a == something)
        		{
        			return; //function ends
        		}
        		if (i == 5)
        		{
        			break; //loop ends and below gets executed
        		}
        	}
        
        	//more stuff here
        }
        I'm not sure if I'm understanding you correctly. First you said you want "foreach" to continue after breaking from "for", now you're saying you wish to skip "foreach" as well?

        Comment


        • #5
          He does not want to skip the foreach, he wants to run the next iteration of the foreach without having to execute code that is after the for-loop but still within the foreach.

          Comment


          • #6
            Right. Now his post makes perfect sense. Cheers.

            Comment


            • #7
              My fault, bad explanation


              Thanks Crusha, that's exactly what I am looking for.

              Comment


              • #8
                Out of curiosity: how would that look in Java?

                Comment


                • #9
                  This may work (untested):

                  Code:
                  foreach AllActors(class'Actor', a)
                  {
                      for (i = 0; i < 10; i++)
                      {
                          if (i == 5)
                          {
                              //what I want to happen: skip the rest of the for loop and continue the iterator
                              Goto ContinueIterator;
                          }
                      }
                  
                  ContinueIterator:
                      // code here....
                  }

                  Comment


                  • #10
                    @Crusha:
                    PHP Code:
                    Iterator: foreach (Type nameIterable)
                    {
                        for (
                    int i 010i++)
                        {
                            if (
                    Conditions)
                            {
                                continue 
                    Iterator;    //steps out of the inner loop (int i = 0; i < 10; i++) and continues with Iterator
                            
                    }
                        }

                    @Spoof:
                    The label needs to be the last statement (after "code here"), but other than that I think it would work (haven't tested either).

                    Comment


                    • #11
                      Ah ok, yeah no real way to avoid it but you can condense that a bit.
                      Code:
                      local bool bSkip;
                      local Actor a;
                      local int i;
                      
                      foreach AllActors(class'Actor', a)
                      {
                      	bSkip = false;
                      
                      	for (i = 0; i < 10; i++)
                      	{
                      		if (i == 5)
                      		{
                      			`log("break");
                      			bSkip = true;
                      			break;
                      		}
                      	}
                      
                      	if (!bSkip)
                      	{
                      		// Stuff that you don't want to execute for that one here.
                      	}
                      
                      }

                      Comment


                      • #12
                        My instinct is to go with the boolean (which Angel_Mapper already pointed out twenty minutes ago), but I'd probably just use

                        if (bSkip) continue;

                        It's not.. any different.. and I'm really contributing nothing here, but.. I wanted to talk too. It's lonely over here! ):

                        Comment


                        • #13
                          Thank you very much everyone, I do like to hear all the different oppinions.


                          The difference performance-wise would be minimal, right? (comparing goto, bContinue and bSkip methods)

                          Comment


                          • #14
                            Goto should be the fastest, but unless you have hundreds of actors it won't matter. It all depends on your actual implementation, since this proof-of-concept has a trivial inner loop. I'm not sure what you'd gain from the boolean approach - to me it seems clumsy.

                            Comment


                            • #15
                              Note that if you break a for loop, the iteration variable will have a different value than after completing the for loop. For example:
                              Code:
                              foreach (...)
                              {
                                for (i = 0; i < 10; i++)
                                {
                                  if (...) break;
                                }
                                if (i < 10) continue;
                              }

                              Comment

                              Working...
                              X