Using PlantUML, the following results in an arrow into nowhere as exit for the loop. How do I get rid of the arrow to nowhere to get an endless loop?
start
while (Always) is ( )
:do something;
endwhile ( )
You can hide the arrow:
while (Always)
:do something;
endwhile
-[hidden]->
detach
Related
My general understanding of a basic while loop (in other languages) is the while loop will break out itself when the variable is no longer true. This does not seem to be happening in Pine Script v5.
Example:
(_RSI is less than _Min_RSI) and (_VOL is greater than _Min_VOL and less than _Max_VOL)
_switch = 0
while _switch > -1
_switch := ( (_RSI >= _Min_RSI and _RSI <= _Max_RSI) ? 1 : -1 )
// While loop should break out automatically after this line if _switch equal to -1
_switch := ( (_VOL >= _Min_VOL and _VOL <= _Max_VOL) ? 1 : -1 )
break
When _RSI is less than _Min_RSI, _switch is correctly set to -1. But, the while loop does NOT break out automatically.......
Instead, it continues to the _VOL line. In essence the overall output is an OR whereas I'm expecting AND.
Above is a sample. The actual code has 50+ checks, for (each of) 10 time-frames. Originally I was using 50 if statements, but thought the while loop would help performance.
Seems the only workaround is to evaluate (and break out after) each line, which kinda defeats the purpose of using a while loop in the first place.
What am I doing wrong? Or does the while loop simply work differently in PS vs other languages?
Thanks
Answer from TradingView Support.
Correct, as we said in the previous post when the 'while' expression
is checked, it goes into the 'while' scope and executes the code
block, if the expression is changed from inside the local block it
will only be re-checked on the next iteration of the script, but it
will not break automatically in the middle of the local scope. If you
want to stop the loop in the middle - use the 'break' keyword after
re-assigning the value of the expression.
Clear.
Upon getting an error in the GAP command line interpreter, the chain of function calls that led to the error get's truncated, so I can see the start of the chain, which is the function I'd need to fix.
gap> MyAwesomeFunction(x,y);
Error, resulting list would be too large (length infinity) called from
ConstantTimeAccessList( enum ) at /path/to/gap/lib/coll.gi:506 called from
AsList( l ) at /path/to/gap/lib/list.gi:612 called from
AsPlist( l ) at /path/to/gap/lib/list.gi:673 called from
EnumeratorSorted( Enumerator( D ) ) at /path/to/gap/lib/domain.gi:231 called from
EnumeratorSorted( C ) at /path/to/gap/lib/coll.gi:862 called from
... at line 4 of *stdin*
you can 'quit;' to quit to outer loop, or
you can 'return;' to continue
brk>
How do I tell GAP to show me the full chain of functions here? What's behind that ...?
As per Alexander Konovalov's comment, the Where function is the key here. Entering Where(42) in the break look will show up to 42 functions in this "called from" chain.
I have a block which looks like this:
fork
begin
$display("before repeat");
repeat (delay_before_rsp2data) #1ps;
$display("after repeat");
$display("some information");
`ovm_do_on_with("do some stuff");
end
join_none
In almost all cases it works fine, but in one case (that I found), after the `ovm_do_on_with line, it jumps back to the repeat line, and continuing from there. I know this is happening, because what is displayed is:
before repeat
after repeat
some information
[all the displays from the `ovm_do_on_with part]
after repeat
some information
[all the displays from the `ovm_do_on_with part]
I've tried putting the whole 'repeat' line inside a before end block, switching it with a for loop, and a few more combinations like that, but always the same behavior.
Any idea what could be causing this?
It seems that you are executing the fork ... join_none more than one time (probably in loop) and I guess the delay_before_rsp2data is random.
If this is the case, then the after repeat you saw may not be from the same thread as the before repeat.
My suggestion, add some kind of id to debug and track which thread it is from. For example
int id=0;
task my_fork;
fork
automatic int k; // !! must be automatic
begin
id++; k = id;
$display("before repeat, id %d", k);
repeat(delay) #1ps;
$display("after repeat, id %d", k);
`uvm_do_on_with();
end
join_none
endtask
Then you should be able to track the threads and correlate them.
Remember to use automatic variable inside fork join_none so that it's thread has unique id.
Some quick example:
http://www.edaplayground.com/x/4vRd
This is a very basic question but please bear with me!
I got this code in a question as part of a quiz I was doing earlier and just didn't know if I might be missing something. I typed it into the editor and it would not run and it does appear to be incomplete. Had it been if (k) it would have made more sense.
But, as I have heard that you can leave out components of a for loop, I was just wondering if there is any time you would see the likes of for (k)?
int k = 0;
for (k) {
printf ("hello");
}
for(int k; ;)
/*this is the correct syntax of a for loop without conditional statement and incrementation/decrementation statement*\
Remember,those semi-colons within the paranthesis is important(without that the program wouldn't compile).
Now,to answer some of the questions you asked me in the earlier answer-
for(int k; ;)
{
printf("infinite loop");
}
When will this loop come to an end?
This loop will never come to an end.It is an infinite loop.It will keep printing infinite loop forever.
Is it possible to bring this loop to an end?
Yes,it is.It can be brought to an end using break statement.
for(int k; ;)
//or for( ; ; )
{
printf("infinite loop");
break;
}
Prints infinte loop only once.It will encounter the break statement and the control will move outside the loop.
Possible application.
It's used when you actually have no idea about when a loop should come to an end.
int i=0; //to take user input
for(int k; ;)
{
//accept the value of k from user.
/*You want the user to enter 1 as the input*/
if(k==1)
{
printf("entered 1,moving out of loop");
break;
}
}
What is the meaning of above loop?
- This loop keeps running until the user enters '1'.This is important in cases where you are giving the user options and the options are limited and so you don't want the user to give an invalid input.It runs until a valid input is entered(you can add more if statements with break statement).
Menu: 1)Pizza
2)Burger
3)Quit buying!
for(int k;k<10;k++)
/* this is a finite loop and this isn't suitable for the above requirement because you are not sure if the user will give the valid input within 10 iterations.*/
When k becomes 10,the control will move out irrespective of whether the user has entered a valid input or not.What if the user inputs 8 when k=9? The control will move out of the loop at k=10.As a result,your program will not work efficiently because i=8 is not an input you expected.You wanted 1,2 or 3 as input.
So,an infinite loop is used when you are not sure about how many iterations are required.You will actually be using a break statement to exit such a loop.
Is this the only option for an infinite loop?Why not while() ?Isn't while() with no condition an infinite loop?
while();// invalid in C.
//objective-C follows C-standards.
while("condition"); //valid
Some valid for loop declarations in C
for(k; ;) // infinite loop
for(; ;) // infinite loop
for(; k<0;)// valid
So,I think that sums up a small explanation.
Remember,semi-colons are important(irrespective of whether a condition is given or not).
And of course,you have other options to keep running or taking user input unless a valid input is given.But above one was just an application I could figure out to show that an infinite loop could be cool!
If you find any error or doubt,please comment.
Well,even I am not too good in C.But yeah since java is somewhat similar,I figured it out.
I have an AutoHotkey script in which the left-mouse-button is mapped to a function. Part of the function includes simulating a left-button click offset from the actual cursor position. Not surprisingly, this ends up becoming a bit of an infinite loop.
Likewise, there is a handler that traps a key-press and performs some math before passing the key-press on through.
Is there a way perform a click without triggering the click-handler? Similarly, is there a way to send a key-press without triggering the key-press-handler?
Trap() {
MouseGetPos, x,y
;Perform some math with x and y
Click %x% %y% left ;oops, this causes Trap to get called again
}
LButton:: Trap
From the AutoHotkey manual:
$ - This is usually only necessary if the script uses the Send command to send the keys that comprise the hotkey itself, which might otherwise cause it to trigger itself.
That does the trick:
$LButton:: Trap
I actually do not see the looping behaviour you describe, so I wonder if there is some other factor at play.
If that really is the problem, then you can use a boolean flag to prevent recursive execution:
isTrapping := false
Trap() {
global isTrapping
if isTrapping = true
return
isTrapping := true
MouseGetPos x, y
; do some maths with x & y
Click %x%, %y%
isTrapping := false
}
LButton::Trap()