I need to leave a cycle in a cycle but when I use Break/exit, that leave the whole cycle / procedure.
procedure MyProcedure; var a,b,c:integer; begin for a:=0 to 10 do for b:=0 to 10 do begin c:=StrToInt(inputbox()); if c=3 then Break; end; end;
Hi!Except that this has the same error I did in my first post also, if c is 3, the loop is broken and the a loop will continue which will increase a.
I have show the clear solution above.
2. Goto:but.. it has a "small" problem: it causes an infinite loop because "a" is never incremented. OTH, the solution you suggested using "break" doesn't and seems to solve the OP's problem satisfactorily.
var a,b,c: Integer; label bLoop; begin for a := 0 to 10 do begin bLoop: for b := 0 to 10 do begin c := StrToInt(inputbox()); if c = 3 then goto bLoop; end; end; end;
And now a maybe unpopular oppinion, I would go with the goto variant, because the code is 1. shorter and 2.
And as long as it leads to better readable code, there is nothing wrong about gotoAnd _no_ infinite loops. <chuckle>
Avoid goto. Yes, you can have 'tighter' code. But it ages badly over time. Why many SQ policies forbid it entirely in HOL.Nah, goto is only bad if it is used badly, simple as that. While it's true that 99% of the time goto is misused, here we have a case where it is better than any other use (as already discussed, break and continue do not fit here), where it actually improves the code (from a readability point of view, I don't care about the produced assembly, this is the job of the compiler not mine)
but.. it has a "small" problem: it causes an infinite loop because "a" is never incremented. OTH, the solution you suggested using "break" doesn't and seems to solve the OP's problem satisfactorily.Which is exactly the behaviour requested in the original post... If he wants code that can loop infinetly often that is none of my problem, maybe he has reasons for this, maybe it's a logical error on his side, he requested how to solve this problem so I think we should give him the *correct* solution (with respect to his question) and not the thing we think he wants. Becaus I don't know about you, but I don't trust my mind reading skills that far that I can read minds through an internet forum
Hi!Except that this has the same error I did in my first post also, if c is 3, the loop is broken and the a loop will continue which will increase a.
I have show the clear solution above.
Imagine the following case, inputbox always returns 3. In the goto example (which is basically what the original post did), this would result in an infinite loop, in your example this would result in exactly 11 iterations and then the procedure would finish.
Otherwise, I would choose the break version, because for loops are imho easier to read then while loops.
Also, what does "pure pascal" mean, aren't break and goto parts of the pascal language?
Except that this has the same error I did in my first post also, if c is 3, the loop is broken and the a loop will continue which will increase a.
I don't trust my mind reading skills that far that I can read minds through an internet forumThat's probably a good thing. That said, I don't recall anyone ever posting that they need to create an infinite loop and, his request doesn't sound like he wants one:
I need to leave a cycle in a cycle but when I use Break/exit, that leave the whole cycle / procedure.From what he said, it sounds like he wants to leave the _inner_ loop _only_ and keep executing the outer loop. Granted his "BackTo('@');" very much sounds like a "goto" but, if he really wanted an infinite loop he wouldn't use an outer "for" loop. A simple "while true do" gives him a nice shiny infinite loop.
procedure MyProcedure; var a,b,c:integer; begin for a:=0 to 10 do for b:=0 to 10 do begin c:=StrToInt(inputbox()); if c=3 then Break; end; end;
Remember that the value of b becomes undefined if the break is taken.
Isn't it just the other way around? If the break is taken then b will keep its current value. It's undefined only if the loop ends normally.You are correct. The value is undefined if the loop ends normally.
Oh, oh, oh.
This is expected behaviour - look into the first post of Jake012345.
But it is not caught in an endless loop but jumps into then next loop of the a - like he wanted.
First read. Then think. Then write.
Winni
From what he said, it sounds like he wants to leave the _inner_ loop _only_ and keep executing the outer loop. Granted his "BackTo('@');" very much sounds like a "goto" but, if he really wanted an infinite loop he wouldn't use an outer "for" loop. A simple "while true do" gives him a nice shiny infinite loop.
Yes, you are correct.Remember that the value of b becomes undefined if the break is taken.Isn't it just the other way around? If the break is taken then b will keep its current value. It's undefined only if the loop ends normally.
It is only an infinite loop if c is always 3. If no 3 occurs, it will be 11*11That is true but, the fact that he typed "for a = 1 to 10 do" really gives the impression that he is not interested in a case that results in an infinite loop.
It is only an infinite loop if c is always 3. If no 3 occurs, it will be 11*11
That is true but, the fact that he typed "for a = 1 to 10 do" really gives the impression that he is not interested in a case that results in an infinite loop.Sure, I also have the impression he wants to do from 1 to 10 not from 0 to 10 (11 is an akward number of iterations), and having this goto scenario seems pretty odd to me, but maybe InputBox shows a form with the title: "choose your action 1: ... 2: ... 3: Delay this loop for another 11 iterations" and this is everything well thought out, how knows. Or maybe he really wants to annoy his users by adding a trick value which just keeps them in this loop, I mean who doesnt sometimes want to torture his users for choosing stupid input
He better not default the return value of InputBox to "3". :D
I don't recall anyone ever posting that they need to create an infinite loop
True fact: some airlines have received notice that they must turn off the avionics on some aircraft within a specific period (100 hours or so) to avoid bugs related to counters etc. overflowing. They tend to leave the aircraft hot at the gate between flights (on external APU) including overnight.
I was referring to the Scarebus 350, actually and the offending rollover was about 150 days... but coming from the Avionics world such things don't surprise me regardless of model of a/c. (And Boing and Scarebus buy avionics from many common suppliers re-using tons of code modified for each model and variant...).
When I was a young boy Germany bought a lot of Lockheed F-104 "Starfighter" - because of corruption. They fell doen from heaven - nearly one very week.:o