How to make an infinite loop in Lua code? - loops

I have three local functions that I want to use forever in memory:
proxy:PlayerParamRecover();
proxy:PlayerRecover();
proxy:EnableInvincible(10000,true);
I'm not sure how to add them in an infinite loop.

You want a while loop:
while true do
proxy:PlayerParamRecover()
proxy:PlayerRecover()
proxy:EnableInvincible(10000,true)
end
Additional information here
Note that, since the while loop will always have control of the program after entering that loop, any code you write after it won't ever execute. Infinite loops are only useful in extreme cases - make sure that what you want to do warrants it.

There are two ways to use infinite loop:
repeat
-- do something
until false
-- or --
while true do
-- do something
end

If you wanted to say "Hello" in the command bar every second, infinitely, or something like that, you would use the format below:
while true do
-- whatever
end
For example,
while true do
print("Hello")
wait(1)
end

Related

While loop using turtle graphic defined function

I'm using a while loop with user-defined functions to make drawings in turtle graphics based on a number of different conditions. For some reason, the while loop completely ignores the iterator making it an infinite loop when it shouldn't be. If you take out the functions that make the graphics, the loop works the way it should. Why doesn't the loop work with the functions for the graphics?
I need to see your code to have a better understanding of what's happening. Other than that, have you given your loop an end case that will keep it from looping? for example
i=0
while i<10:
print(i)
i+=1
the i variable is incremented at the end of every loop, the loop stops once it exceeds 10.
My guess is that the functions you have that make your graphics are not connected to your while loops stoping case.ex:
continue=True
while continue==True:
if #user action == desired output:
#insert graphic function
continue==False
else:
#enter whatever default action you'd like

How can i continue the loop after getting value from function?

as i write in title, I have small question it's about loop!
i've small loop, ok?
i want pause loop in each value and call a function and wait a response from function, if function give any value i want to continue the loop!
anyone have idea or suggest to help me at this?
please don't give codes includes/or needs LUA Librares
Function calls inside loops will block by default in Lua (and any other language I can think of). So you do not have to worry about that. The loop won't continue as long the function doesn't return a value.
function is_done(x)
if x == 5 then
return true
end
return false
end
for i=1,10 do
if is_done(i) then
print('done!')
break
end
end
In the above example the loop breaks (stops) when i is equal to 5.

Require code to perform this loop in SPSS including looping variable names

I am looking for the spss code to perform the following:
I have three variables: ResponseID and Q1 and Q2 that needs to be copied throughout my data set, I have already included variables for them - it starts with VAR00002, VAR00003 and VAR00004 several times throughout my data set. I now want to populate them, I therefore have to include the name of the variable in the loop and it needs to carry on doing this for the first set of three, the second set (VAR00005,VAR00006 and VAR00007) etc. (depending on the condition included in the Do IF). Then there is also the Else IF (and another Do If) included afterwards.
Loop # = 1 to 27
Do IF (Q[#(23)+2]=2).
COMPUTE (VAR0000(#+1))=ResponseID.
COMPUTE (VAR0000(#+2))=Q1.
COMPUTE (VAR0000(#+3))=Q2.
End if.
Else If.
Do If.
Q[(#-1)*(23)+3])=2.
DELETE VARIABLES Q[#(23)+3] TO Q623.
End If.
End Else If.
I am not sure what you need to do, but take a look at DO REPEAT, which allows you to repeat a set of transformations in a loop.
Niether I understand exactly what you are trying to do, perhaps look into LOOP / VECTOR combination

What is the iteration error in the loop?

loop
if rising_edge (CLOCK) then
fcounter := fcounter+1;
end if;
A<=fcounter(6); --fa=fclock/2^6
if rising_edge (A) then
counter_A:= counter_A+1;
end if;
CIKIS<=A; --40 consecutive "1" consignment to DIN for STARTUP RESET
exit when counter_A=101000; --40
end loop;
ISE gives "ERROR:Xst:1312 - Loop has iterated 64 times. Use "set -loop_iteration_limit XX" to iterate more." What does that mean and what should i do to get rid of it?
Unbounded loops are not generally synthesizable outside of generating constants. The error is telling you that the loop has iterated beyond the preset limit that guards against infinite loops. Increasing the limit will not fix anything because what you have described doesn't map naturally to hardware with the current crop of synthesis tools.
You need to restructure your logic to inhibit that block of code with an enable that is deactivated when the counter reaches its terminal value rather than use a loop.
Synthesizable constructs can't have compile time unbounded loops like this, they are only to be used in testbench code. The reason is that when they expand into hardware, something like this would require infinite resources (which you don't have). You seem to be trying to write this like a piece of software. When using a HDL, you need to be thinking of how it translates into hardware (assuming you plan to synthesize). Instead you might look at using a process where all activity happens inside an "if rising_edge(clock)", and use signals to explicitly use flip flops as opposed to variables which could go either way. It isn't entirely clear to me if you're trying to count when your fcounter rolls over 40 times (what you currently do) or when your fcounter exceeds 0111111 for 40 clock cycles, but you can use a signal indicating the state for either case instead of an exit statement, and an if/elsif statement on that signal to do something else when you don't want to have process counting. Sorry for the poor formatting, short on time.

Why is for(;;) used?

I have seen code (in C) which contains something similar to:
for(;;){
}
How would this work, and why is it used in any instance?
It is the idiomatic way to have an infinite loop.
In the C Programming Language book, by Kernighan & Ritchie book it was introduced in section 3.5:
for (;;) {
...
}
is an infinite loop, presumably to be broken by other means, such as a break or return.
is an infinite loop something like
while(true)
{}
This is equivalent to an infinite loop, as many other have explained. However, few of them explained why this executes as an infinite loop.
A for loop can be broken down into three parts:
for(initialization(s); condition(s); looping command(s))
None of these fields are actually required. Without a condition provided, there's nothing to stop the command from running. This is because the for loop looks for a false statement. Without conditions provided, nothing is false, therefore the loop runs indefinitely.
Therefore to cause a for loop to be infinite, all you need is to not provide a condition. This means that this is also a valid infinite loop:
for(int i = 0;; i++)
printf("Iteration: %i\n", i);
For readability, and to make sure that the second semi-colon isn't a typo, some programmers might put a space between them, or put true as the condition.
Honestly, I prefer while(true), as this is a clear infinite loop. Using while(1) works as well, but '1' is an integer, not a boolean. While it is equivalent to true, it does not always mean true.
Between these three types of infinite loops, for(;;) has the fewest characters (7). while(1) comes second at 8 characters, and while(true) at 11.
I suppose that certain programmers prefer a low byte count over a readable program, but I wouldn't recommend using for(;;). While equivalent, I believe that using while(true) is better practice.
A for loop needs three expressions, which are separated by semicolons, and are completely optional:
An initialization (e.g. int i=0)
A condition (e.g. i < 10)
An afterthought (e.g. i++)
In this case, the three expressions are empty, and thus there's no condition that will make the loop stop, thus creating an infinite loop, unless a flow control instruction like break (which will exit the loop) or return is used.
Its an infinite loop. Its equivalent to:
while (true) {
}
The C# compiler directly translates for (; ;) into the exact same construct as while (true).
Infinite loop
same as
while(true){}
the code for(;;){} or while(true){} or while(1){} all represent infinite loops.
An infinite loop is something to be expected in a software system that is expected to run and "unlimited" amount of time. Every OS has at least one - it's how a background task or idle task is implemented.
Real Time systems use infinite loops as well because the system has to handle events which are asynchronous;
Basically anything that runs software uses infinite loops in one way or another.
I don't know why no one answered why people do this instead of while(true): It's because while(true) will often generate a compilation warning that the condition of the loop is constant.
This kind of infinite loop can be used in microcontroleurs. In your main function, you initialize the basic functions of your microcontroleur, then put a while (1).
void MAIN(void)
{
Init_Device();
while(1);
}
The microcontroleur will then do nothing but wait for interrupts of internal or external devices (like a timer overflow, or UART data ready to be read), and react to these interrupts.

Resources