lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Lucas Zawacki once stated:
> Just here expressing my -1
> 
> All this code with labeled break looks very ugly and confusing with no
> great benefit, very unlike the rest of Lua.
> 
> I see the intent of providing a "generic" solution to the continue
> issue, but to be honest I can't even remember having used continue at
> all for some months now (programming mostly in C, C++ and Ruby).

  There have been a few instances where I would have liked to have a
"continue" in Lua.  In C, "continue" for the following loops works as
follows:

	x = 0;
	while(x < 50)
	{
	  body1();
	  if (x < 30) continue;
	  body2();
	  x++;
	}

		x = 0;
	CONTINUE:
		if (!(x < 50)) goto done;
		body1();
		if (x < 30) goto CONTINUE;
		body2();
		x++;
		goto CONTINUE;
	done:

	/*---------------------------*/

	x = 0;
	do
	{
 	  body1();
	  if (x < 30) continue;
	  body2();
	  x++;
	} while(x < 50);

		x = 0;
	CONTINUE:
		body1();
		if (x < 30) goto CONTINUE;
		body2();
		x++;
		if (x < 50) goto CONTINUE;

	/*----------------------------*/
	
	for(x = 0 ; x < 50 ; x++)
	{
	  body1();
	  if (x < 30) continue;
	  body2();
	}

		x = 0;
	start:
		if (!(x < 50)) goto done;
		body1();
		if (x < 30) goto CONTINUE;
		body2();
	CONTINUE:
		x++;
		goto start;
	done:

  The only anomaly here is for(;;) in which case "continue" evaluates the
increment and then the test.  The other two just jump to the start of their
respsective loops.  The equivalent in Lua are "while do ... end",
"repeat ... until" and "for" (two variants).  If you keep the similar
behavior of "continue" in C into Lua, then:

	while x < 50 do
	  body1()
	  if x < 30 then continue end
	  body2()
	  x = x + 1
	end

the "continue" will jump to the conditional test;

	x = 0
	repeat
	  body1()
	  if x < 30 then continue end
	  body2()
	  x = x + 1
	until x >= 50

the "continue" will jump to the start of the block and for for:

	for i = 1 , 50 do
	  body1()
	  if x < 30 then continue end
	  body2()
	end

	for name,value in pairs(table) do
	  body1()
	  if value ~= nil then continue end
	  body2()
	end

"continue" will continue with the next loop (x gets incremented; name,value
get updated with the next pair of values).  It seems straightforward to me,
but then, I didn't write Lua to begin with.

  -spc