![]() ![]() So a solution like this will at least break it out and let the rest of your world continue to revolve. I tend to agree with this, but in the world of OPC, tags can go bad quality after you enter a while loop, causing it to hang up in the loop. Some would argue that this is bad coding as your main comparison should not even take place unless the data being compared is good quality and will always work. Everything is checked in the while statement, and it falls out of the loop when either comparison is false. This doesn’t require the break command, which some programmers consider bad coding if you use it. While ((whatever I check for is in limits) AND (the current datetime - loopStartTime < maxLoopTime)) while (1) try for x 1 : 20 for y 1 : 30 assert((condition), 'break') end end catch err if strcmp(err.message, 'break'), rethrow(err), end end end The nice thing about this approach is that it works with an arbitrary number of nested loops, and only evaluates condition once without having to store the result in a variable. Set datetime type variable loopStartTime = the current datetime Set timespan type variable maxLoopTime = 5 That doesn’t sound very explanatory, so I’ll do it with pseudo code… I used an AND of the normal loop check and the time lapse check so either one would bust out. If the difference exceeds the value of the variable set with the maximum time the loop is allowed to run, the while would bust out. 1 In Matlab, if an error occurs, how can I skip that certain for loop index and continue to the next index Note that I do not know which for loop indices will cause the error to occur. The while statement would check whatever it normally checks for in the loop, and also subtract the date and time stored in the time entered variable from the current date and time. When the remainder is zero in the first while statement, Octave immediately breaks out of the loop. ![]() Assuming maxsteps is 8 (or any integer) when I execute the function I get the following error: Subscript indices must either be real positive integers or logicals. The program will print out ' End of inner loop, ' the index of the outer loop will be increased by. The F (steps) is a N (maxsteps) dimension vector which on each loop 'gets' a new column by performing a sum between two values which are different on every iteration: F (steps) a + b. What I did there was set a variable to the number of milliseconds, seconds, or whatever that I will allow the loop to run, then grab the date and time into another variable when I enter the loop. This will cause the program to exit the innermost loop. I have had a situation similar to this in the past while writing C# code. ![]()
0 Comments
Leave a Reply. |