A loop in a loop, breaking both loops

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

A loop in a loop, breaking both loops

Post by Sheepolution »

Code: Select all

for i=1,10 do
  	_j = 0
 	for j=1,10 do
 			print(j)
 			_j = j
 		if (j==4) then
 			
 			break
 		end
 	end
 	if (_j~=10) then
 		break
 	end
 end
This is what I have right now. It works, but I feel like I'm overdoing stuff. Something too do this easier? Is there a Lua function I'm missing maybe?
ferdielance
Prole
Posts: 6
Joined: Tue May 07, 2013 10:35 am

Re: A loop in a loop, breaking both loops

Post by ferdielance »

Depending on your needs, the following may work more simply, or may not be practical at all.

* A: Put the whole double loop structure inside a function, and use return to get out when the time comes. (Not good in situations where having a function call every time you do the double loop is too costly)

* B: Use while for the outer loop (while counter<= value and looping == true do) and set "looping" to false in the inner loop when the time comes. This feels neater to me than having a separate break outside.
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: A loop in a loop, breaking both loops

Post by Lafolie »

Why are you doing this? Sounds like a while loop would be much more useful.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
User avatar
adnzzzzZ
Party member
Posts: 305
Joined: Sun Dec 26, 2010 11:04 pm
Location: Porto Alegre, Brazil

Re: A loop in a loop, breaking both loops

Post by adnzzzzZ »

You can always use goto to break from nested loops. I'm ready
User avatar
slime
Solid Snayke
Posts: 3159
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: A loop in a loop, breaking both loops

Post by slime »

adnzzzzZ wrote:You can always use goto to break from nested loops. I'm ready
goto is only in Lua 5.2 and LuaJIT, not regular 5.1 (which is what love uses by default.)
User avatar
Sheepolution
Party member
Posts: 264
Joined: Mon Mar 04, 2013 9:31 am
Location: The Netherlands
Contact:

Re: A loop in a loop, breaking both loops

Post by Sheepolution »

ferdielance wrote:Depending on your needs, the following may work more simply, or may not be practical at all.

* A: Put the whole double loop structure inside a function, and use return to get out when the time comes. (Not good in situations where having a function call every time you do the double loop is too costly)

* B: Use while for the outer loop (while counter<= value and looping == true do) and set "looping" to false in the inner loop when the time comes. This feels neater to me than having a separate break outside.

So something like this?

Code: Select all

i, loop = 1, true
 while (i<10 and loop) do
 	for j=1,10 do
 		print(j)
 		if (j==4) then
 			loop = false
 			break
 		end
 	end
 	i=i+1
 end
It does indeed look cleaner.
Lafolie wrote:Why are you doing this? Sounds like a while loop would be much more useful.
I'm trying to make a tile-based platformer. I need to check all tiles, and if I hit a solid one, both loops need to break.
User avatar
Jasoco
Inner party member
Posts: 3726
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: A loop in a loop, breaking both loops

Post by Jasoco »

There's a better way to do collision detection with a grid. A simpler way that doesn't require you to do this.

How is your level tile layout formatted? Are you using a grid like you should?
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests