Page 1 of 1

Break to...

Posted: Tue Jun 25, 2013 8:59 am
by Michael Vogel
I don't like the Break command, but sometimes it's the simpliest way to do coding - anyhow I don't feel well because who knows, if "Break 17" will do exactly what I want? And what, if I will add/remove a loop in the source, will I remember to modify the Break level as well?

What about a possibility to use a break mark and the compiler is doing the rest by calculating the appropriate break level?

Code: Select all

	If ReadFile(#FileRead,\ImageList)
		While Eof(#FileRead)=#Null
			Name=ReadString(#FileRead)
			While PeekA(@Name)='*'
				Path=Mid(Name,2)
				If Eof(#FileRead)
					Break to next Mark;   instead of Break 2
				Else
					Name=ReadString(#FileRead)
				EndIf
			Wend

			Debug Path+"   "+Name
		Wend
		Break Mark

		CloseFile(#FileRead)
	EndIf

Re: Break to...

Posted: Tue Jun 25, 2013 9:28 am
by PB
Maybe breaking to a label could be supported?

Re: Break to...

Posted: Tue Jun 25, 2013 10:45 am
by luis
Here I am.

Break without a number it's a GOTO renamed, limited in its scope, which can jump only forward.
It's born to alleviate anxiety problems in programmers which doesn't like the stigmata of GOTOs in their programs. So it's still a GOTO but a tamed one and with a mask on its face. Can have its place, I don't debate it.

Break *with a number* it's practically a GOTO, you feel trapped inside the nice, stylish structured code you built and want to skip some levels, but you can't accept to use GOTO because it's dirty, my GOD. So why not use a Break 72 ? Looks perfectly legitimate and it's not a GOTO ! Who could think otherwise!

Break with a label now... seriously ? Even the mask is dropped.


And in some way already requested: -> http://www.purebasic.fr/english/viewtop ... =3&t=49218

Nice reading too :)

Re: Break to...

Posted: Tue Jun 25, 2013 11:29 am
by Michael Vogel
luis wrote:Break without a number it's a GOTO renamed, limited in its scope, which can jump only forward.
Didn't know that, I thought, Break would pop some jump addresses from a stack or does something else in an elegant way, while goto could leave a lot of waste on the heap.
luis wrote:So why not use a Break 72
Shouldn't be Break 42 the perfect solution?

Re: Break to...

Posted: Tue Jun 25, 2013 12:56 pm
by luis
Michael Vogel wrote: Didn't know that, I thought, Break would pop some jump addresses from a stack or does something else in an elegant way, while goto could leave a lot of waste on the heap.
I was talking from the high level point of view, from a language perspective, maybe I should have make myself more clear.
Anyway, from an implementation point of view, from what I saw Break is translated as a direct jump, so.. there you have it :)

The fact Break is a "redefined forward-only GOTO with limited scope" it's a good thing in the sense it makes impossible a misuse of GOTO in its place.

You can jump but only where it's safe: for example not from inside a select/endselect which is using the stack, simply because Break is not accepted there.
You cannot jump in the middle of another piece of code where you don't have a right to be: simply because you can't express that destination using a Break followed by a number.
You can jump only in a "stylish" way: you can't jump backward, usually something easily avoidable in a structured high level language.

Adding a destination label for a Break will strip all this from it, unless the compiler is loaded with checks about its use. Not needed now.

Break N with N > 1 is already pushing the envelope of what Break should do (it's also really ugly and hard to follow and maintain compared to a clean use of GOTO in my opinion).

For everything else we have GOTOs, to be used in a proper way like anything else.
Now that we have local labels to be paired with GOTOs I'm quite happy with it.
Michael Vogel wrote: Shouldn't be Break 42 the perfect solution?
You can try it and report back :)