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

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.

Related

Lua: Access the for loop variable from outside of the loop

I would like to use a for-loop in Lua, but be able to identify what the last iterated value was:
local i
for i=0,10 do
if i==5 then break end
end
print(i) --always prints nil
Is there some way to prevent "i" from being re-declared in the for loop block, instead of shadowing my upvalue of the same name?
Currently, I have to use a while loop to achieve the expected results, which defeats the clarity of the for loop's syntax. Is this just another Lua caveat one has to expect as part of its language quirks?
i is local to the for-loop, meaning that you cannot access is when the loop terminates.
If you want to know what the last iterated value was, you have to save it in another variable:
local last
for i = 0, 10 do
if i == 5 then
last = i
break
end
end
print(last) --> 5

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

CS50: For Loop Design

Currently doing the CS50 lectures, and in week 2, starting at about 56:45 on this video (https://video.cs50.net/2016/fall/lectures/2?t=56m50s) he mentions that the strlen function should be moved into the initialized variable section of the for loop, rather than stay in the conditions section, because leaving it in the conditions section makes the computer run the strlen function each time the for loop increments.
Ok, I get that, but the proposed solution -- to move it into the variable declaration section, doesn't solve the problem, does it? Because it's still in the for loop, it looks like the strlen function is still being checked each time the for loop iterates.
Am I wrong?
well,strlen function will not be called every time in the for loop because we stored the value of strlen before the for loop,
it will look something like this
`int n= strlen(s);
for(int i=0;i<n;i++)
{
code
}
`
once the value of strlen is stored in n ,computer just have to access n rather than accessing strlen again and again from the string.h library
Hope this answer helps :)
the 'initialize' section of a 'for()` loop is only run once.
The 'conditional' section of a for() loop is run at the beginning of each loop iteration.
The 'step' section of a for() loop is run at the end of each loop iteration.
This is why repetitive operations, like a call to strlen() should be moved to the initialization section (where is should be setting a local variable)

Why is continue inside a loop a bad idea?

Douglas Crockfod says that it is usually better to refactor the continue inside the loop.
Why is continue considered bad within a loop?
The use of continue would mean that you have insufficient conditions written in your while.
You should instead use if inside your while loop, or add the condition into the while loop.
Using goto, break, continue, throw, or return inside the loop body can all have the un-desired effect as well. Here's another example where the loop control and the loop body are tightly interwoven. Does it write 1, 2, and 3 as before? Are you sure?
int value = 1;
for (;;++value)
{
cout << value << endl;
if (value != 4)
continue;
else
break;
}
You might be thinking that advising you not to use return statements inside loop bodies is over zealous. Do I really mean that? Yes I do. Functions that return something should do so via a single return statement at the very end of the function. Here are some practical reasons why:
Link
Disclaimer: Not my material, I have referenced back to the source
The effect of continue is somehow comparable to a goto to the begin of the loop. It therefore makes your code more difficult to understand - like gotos.

How to make an infinite loop in Lua code?

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

Resources