|
But does it provide two ways out? I.e. alternate code blocks executed one but not the other, depending on how you exited the loop?
|
|
|
|
|
It depended on the specific language’s implementation. The general rule was it exits from the innermost loop. I seem to remember VB (pre .NET) was able to break from nested loops…
do while true
for i = 1 to 10
if i > 5 then
exit do
end if
next
loop
I also got something rattling around in my skull about exiting from named loops, but I don’t believe that was a BASIC language. Google/Bing isn’t being helpful at the moment.
- great coders make code look easy
- When humans are doing things computers could be doing instead, the computers get together late at night and laugh at us. - ¿Neal Ford?
|
|
|
|
|
But if you had
for (...) {
for (...) {
I want to exitfor from the outer loop from a condition in the inner loop
}
}
How would you do it? goto was invented for a reason and this is it!!! 
|
|
|
|
|
CHILL has a very nice solution to this: Every block, whether a procedure, loop, switch or even a linear sequence of statements enclosed with BEGIN and END, could be prefixed with a label. In CHILL, a label does not identify a point in the program, but a block, and consequently label scopes could be nested. So to leave the outer loop, you would write
OuterLoop:
DO FOR (...)
InnerLoop:
DO FOR (...)
...
IF <done processing this element> THEN EXIT InnerLoop; FI;
...
IF <some fatal condition> THEN EXIT OuterLoop; FI
...
OD
OD
(Here I illustrate both leaving the inner loop and the outer loop.) However, CHILL doesn't provide what I asked for in my original post: If the post-loop processing depends on whether you completed the loop or left prematurely by EXIT, you must set some variable to a magic value and test it after the loop, and the post-loop processing would syntactically (e.g. with respect to variable scope) be outside the loop.
While we are at CHILL: Another nifty syntactic sugar cube is the keyword EVER:
DO FOR EVER
...
OD
The semantics of EVER is quite obvious. I like this so much that whenever I need to program an inner loop in C, I set up a
#define ever (;;)
to be able to code it as "for ever {...}" in C.
Sure, any well seasoned C programmer would prefer "while (1) {...}", but even though I have been writing more lines of C code than in any other language the last thirty years, I still read it as "while one what???"
|
|
|
|
|
BASIC provides several exit statements such as
EXIT LOOP
EXIT FOR
You can also set the condition in the call to the loop
WHILE Not [answer you want is found] ... WEND will exit when the condition is met (of course you have to add another exit test to avoid infinite loops There are a bunch of similar constructs.
To use this with some OOP type language just write a little function in BASIC (Visual Studio, PowerBasic), compile to a DLL and call it from the object you need to use.
Visual Studio would probably let you do it all in the same project but then you wouldn't have the DLL to reuse.
|
|
|
|
|
Delphi's version of Pascal has Break (quit the loop) and Continue (begin the next iteration).
|
|
|
|
|
That is like break and continue in C, isn't it?
Does it provide two alternatative ways out of a loop: One alternative is executed if the break was performed,the other if break was not performed? If it does, some more syntax must be defined.
|
|
|
|
|
I don't know C well enough to comment.
And yes, if you need to know whether you exited the loop because of the break or the for/while/repeat condition, you have to code it separately.
Usually there's some "natural" way to easily determine that, but obviously not always.
|
|
|
|
|
Hi All,
The first interview (for some time). Not really sure about it, they haven't really told me or the agent what they will ask (please no Labview)...
GLENN (Also my first post from my S3)
|
|
|
|
|
I am wishing you the best of luck and please tell us how it went when its done
Microsoft ... the only place where VARIANT_TRUE != true
|
|
|
|
|
|
Good luck, my friend!
I'm sure you won't need it...you'll be fine.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Thanks, The s3 Is a little odd to post with!
|
|
|
|
|
All the best!
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Thanks the sunshine surroundings are playing havoc to my signal. ...
|
|
|
|
|
Today all the world at your command! You want the job? It's yours !!!
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
* snigger * He said "sunshine"
PooperPig - Coming Soon
|
|
|
|
|
All the Very Best!!!You will do well!!!
|
|
|
|
|
|
knock em dead
You cant outrun the world, but there is no harm in getting a head start
Real stupidity beats artificial intelligence every time.
|
|
|
|
|
Good luck and may the calm be with you.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Good luck.
Alberto Brandolini: The amount of energy necessary to refute bullshit is an order of magnitude bigger than to produce it.
|
|
|
|
|
I hope you getting good result. 
|
|
|
|
|
Good luck to you Glenn, even if the job is something you're not interested in, it's an opportunity to practice interviewing skills.
It was broke, so I fixed it.
|
|
|
|
|
That's what I am thinking....
|
|
|
|