Should this construct be allowed in any language?

Everything else that doesn't fall into one of the other PB categories.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Should this construct be allowed in any language?

Post by srod »

I know there are all kinds of semantic problems with the following and such a construct is easily enacted by using more robust coding methods, but I just wondered what others made of the following?

Code: Select all

a=1

If a = 1
  For j = 1 To 10
Else
  For j = 1 To 5
EndIf
    Debug j
  Next
Part of me thinks that this should be allowed (not just in Purebasic) even though, by anyone's standards, it would be extremely poor coding practice, and yet another part of me turns it's nose up in disgust!

Any thoughts?

(Again, no offence intended, but no one needs to point out the reasons why the above does not compile! :) )
I may look like a mule, but I'm not a complete ass.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

So where do you want the jump at the Next keyword to jump?
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Trond wrote:So where do you want the jump at the Next keyword to jump?
I'm not saying that I think this is necessarily a good idea; I just found myself coding a loop with variable start and end indexes etc. and experimented with this just to see if it would compile, just out of interest! Thankfully it didn't compile, but it got me thinking! :)

Anyhow, the 'next' would necessarily have to jump to the statement immediately following the EndIf.

I know it's kind of a barmy notion! :)
I may look like a mule, but I'm not a complete ass.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

srod wrote:Anyhow, the 'next' would necessarily have to jump to the statement immediately following the EndIf.
That would make an infinite loop.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I don't see how?
I may look like a mule, but I'm not a complete ass.
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post by Derek »

Trond wrote:
srod wrote:Anyhow, the 'next' would necessarily have to jump to the statement immediately following the EndIf.
That would make an infinite loop.
It would just be a loop of 5 or 10, not infinite.

@srod, looks crazy though, not a good idea.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I agree, it looks barmy. I think PB did well to spot it and bomb out with an error message.
I may look like a mule, but I'm not a complete ass.
minirop
User
User
Posts: 24
Joined: Tue Nov 28, 2006 12:21 am

Re: Should this construct be allowed in any language?

Post by minirop »

srod wrote:Part of me thinks that this should be allowed (not just in Purebasic) even though, by anyone's standards, it would be extremely poor coding practice, and yet another part of me turns it's nose up in disgust!
no 'cause it's impossible to do in ASM.

but you can do :

Code: Select all

a=1
b = 1
If a = 1
    b = 10
Else
    b = 5
EndIf
  For j = 1 To b
    Debug j
  Next
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Anyone who concieves of such a construct should be flogged 'round the fleet, keelhauled and fed piecemeal to the sharks. Then he should really be punished (forced to code in .net for a year)
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Derek wrote:
Trond wrote:
srod wrote:Anyhow, the 'next' would necessarily have to jump to the statement immediately following the EndIf.
That would make an infinite loop.
It would just be a loop of 5 or 10, not infinite.
Not if it jumps to the statement just after the EndIf. Then the comparision at the start of the loop is never reached.

Srod: Try to code what you want with if and goto only (that's the kind of constructs the compiler has available to compile the loop into) and you'll see that there will be a problem with the Next.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

@minirop: as stated, coding around the issue is not a problem, I was just thinking aloud! :) Actually, the fact that we can code around the problem proves that such constructs can be added to a language; even assembly, although a corresponding LOOP statement would need to be embedded within an extra conditional. It's just a question of whether such things should even be considered?

@netmaestro: yee-owch! That's awful, only a sadist would threaten someone with .net! :)
I may look like a mule, but I'm not a complete ass.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

@Trond: a language implemenation would of course need to alter the way this kind of For... Next operates at the assembly code level.

Something along the lines of:

Code: Select all

a=2

If a = 1 
  j=1
  ECX = 10
Else 
  j=1
  ECX = 5
EndIf 
loop:
  Debug j 
  j+1
  ECX-1
  If ECX : Goto loop : EndIf
Of course this is probably tunring into something which shouldn't even be labelled as a For... Next !
I may look like a mule, but I'm not a complete ass.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

netmaestro wrote:Anyone who concieves of such a construct should be flogged 'round the fleet, keelhauled and fed piecemeal to the sharks. Then he should really be punished (forced to code in .net for a year)
Ho Yoho! Andah Bottle o'Rum... :D
oh... and have a nice day.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

srod wrote:@Trond: a language implemenation would of course need to alter the way this kind of For... Next operates at the assembly code level.

Something along the lines of:

Code: Select all

a=2

If a = 1 
  j=1
  ECX = 10
Else 
  j=1
  ECX = 5
EndIf 
loop:
  Debug j 
  j+1
  ECX-1
  If ECX : Goto loop : EndIf
Of course this is probably tunring into something which shouldn't even be labelled as a For... Next !
Now what if you wanted to do this?

Code: Select all

If a = 1 
  For j = 1 To 10 
  Apple()
Else 
  For j = 1 To 5 
  Popple()
EndIf 
    Debug j 
  Next
Apple() or Popple() would probably erase ECX.

And what if the compiler encountered this code?

Code: Select all

If a
  For a = b To c
Else
  For c = n To a
    Debug c+b
  Next
EndIf
  Debug a+c
Next
It wouldn't know if the first next should belong to both Fors or only the innermost one until later.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

:)
Apple() or Popple() would probably erase ECX.
The compiler would need to ensure that ECX was preserved through a procedure call.

Now your second snippet is not an 'srod' loop as you've altered the loop counter from a to c. I think the compiler would implode with this piece of code!

:)
I may look like a mule, but I'm not a complete ass.
Post Reply