dbTalk Databases Forums  

What is the answer?

comp.databases.pick comp.databases.pick


Discuss What is the answer? in the comp.databases.pick forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
mg.ryder@gmail.com
 
Posts: n/a

Default What is the answer? - 02-11-2007 , 05:54 AM






Silly discussion last week about the following bit of code:

A = 5
FOR A = 10 TO 1
PRINT "HELLO WORLD"
NEXT A
PRINT A

Nonsense piece of code, I kno - but what would the value of A be when
it is PRINTed?
jbase 4.0 = 9
jBase 4.1 = 5
OpenQM = 10
D3 = 10

I would have expected 10, others thought, maybe, 11 - but 5 or 9 -
huh?
I would be interested in the response from other MV flavours.

Mike


Reply With Quote
  #2  
Old   
GVP
 
Posts: n/a

Default Re: What is the answer? - 02-11-2007 , 08:08 AM







where is STEP -1 ?

5 because FOR not executed at all
10 because FOR A=10 executed
9 because A=10 and then A--


Reply With Quote
  #3  
Old   
frosty
 
Posts: n/a

Default Re: What is the answer? - 02-11-2007 , 09:51 AM



GVP wrote:
Quote:
where is STEP -1 ?

5 because FOR not executed at all
10 because FOR A=10 executed
9 because A=10 and then A--
I think the absence of "STEP -1" is the
crux of the matter.

uniData 6.0 gives 10, BTW, which is the
result I would most expect. I would not
expect 11.

Depends on how the compiler and runtime
handle the for/next; the 5 result makes
sense, actually. The 9, though, is odd;
perhaps the compiler _assumed_ STEP -1?

--
frosty




Reply With Quote
  #4  
Old   
John Henderson
 
Posts: n/a

Default Re: What is the answer? - 02-11-2007 , 01:58 PM



mg.ryder (AT) gmail (DOT) com wrote:

Quote:
Silly discussion last week about the following bit of code:

A = 5
FOR A = 10 TO 1
PRINT "HELLO WORLD"
NEXT A
PRINT A

Nonsense piece of code, I kno - but what would the value of A
be when it is PRINTed?
jbase 4.0 = 9
jBase 4.1 = 5
OpenQM = 10
D3 = 10

I would have expected 10, others thought, maybe, 11 - but 5 or
9 - huh?
I would be interested in the response from other MV flavours.
uniVerse 10 = 5, consistent with the very telling result from
running the 4-line program:

FOR A = 10 TO 1
PRINT "HELLO WORLD"
NEXT A
PRINT A

namely:

Program "T3": Line 4, Variable "A" previously undefined. Empty
string used.

John


Reply With Quote
  #5  
Old   
Mark Brown
 
Posts: n/a

Default Re: What is the answer? - 02-11-2007 , 02:21 PM



It should only ever be 10.

The FOR statement will set the variable to 10 and start off by testing the
terminus. It matched the terminal criteria and so drops out of the loop
with a value of 10.

Some compilers boil the FOR/NEXT into something like:

set A = 10
dec A ' now it's 9, so the following will work
lbl1: inc A ' every iteration inc's A
if A > 1 then goto lbl2 ' test
stack "HELLO WORLD" ' fall thru if it's still working
print stack
goto lbl1 ' return to top of loop
lbl2: continue

Looks like jBase is anticipating loop termination and so skips directly.

Mark

<mg.ryder (AT) gmail (DOT) com> wrote

Quote:
Silly discussion last week about the following bit of code:

A = 5
FOR A = 10 TO 1
PRINT "HELLO WORLD"
NEXT A
PRINT A

Nonsense piece of code, I kno - but what would the value of A be when
it is PRINTed?
jbase 4.0 = 9
jBase 4.1 = 5
OpenQM = 10
D3 = 10

I would have expected 10, others thought, maybe, 11 - but 5 or 9 -
huh?
I would be interested in the response from other MV flavours.

Mike




Reply With Quote
  #6  
Old   
Tony Gravagno
 
Posts: n/a

Default Re: What is the answer? - 02-11-2007 , 04:06 PM



John Henderson wrote:
Quote:
uniVerse 10 = 5, consistent with the very telling result from
running the 4-line program:

FOR A = 10 TO 1
PRINT "HELLO WORLD"
NEXT A
PRINT A

namely:

Program "T3": Line 4, Variable "A" previously undefined. Empty
string used.
This seems to be the result of engineer attemptS to maintain some form
of local scope versus global. While in the loop the variable space
referenced is the local instance while outside the loop that local
instance no longer exists so the global instance is used.

I'm curious to see what happens with this code on various platforms:

VAR=5
GOSUB TRY1
CRT "POINT C=":VAR
STOP
TRY1:
FOR VAR=10 TO 1 STEP -1 ; * intentional decrement
CRT "POINT A"
IF VAR<9 THEN RETURN
NEXT VAR
CRT VAR ; * we should never get here
CRT "POINT B"
RETURN
END

I would expect POINT C=8. But it sounds like some environments might
differ.

Current version of Caché shows "POINT B". (reported)

T


Reply With Quote
  #7  
Old   
John Henderson
 
Posts: n/a

Default Re: What is the answer? - 02-11-2007 , 05:31 PM



Tony Gravagno wrote:

Quote:
John Henderson wrote:
uniVerse 10 = 5, consistent with the very telling result from
running the 4-line program:

FOR A = 10 TO 1
PRINT "HELLO WORLD"
NEXT A
PRINT A

namely:

Program "T3": Line 4, Variable "A" previously undefined.
Empty string used.

This seems to be the result of engineer attemptS to maintain
some form of local scope versus global. While in the loop the
variable space referenced is the local instance while outside
the loop that local instance no longer exists so the global
instance is used.
That explanation didn't occur to me, so I didn't tell the whole
story.

FOR A = 10 TO 10
PRINT "HELLO WORLD"
NEXT A
PRINT A

outputs:

HELLO WORLD
10

even when I add

A = 5

as a first line above the others.

John


Reply With Quote
  #8  
Old   
Ross Ferris
 
Posts: n/a

Default Re: What is the answer? - 02-11-2007 , 05:42 PM



On Feb 12, 7:21 am, "Mark Brown" <mbr... (AT) drexelmgt (DOT) com> wrote:
Quote:
Looks like jBase is anticipating loop termination and so skips directly.

I wonder if jBase compiler is actually "smart" enough to not even
generate the code that is implicitly skipped - the 4.0 result of "9"
just seems "too stupid" - smart enough to guess the decrement, dumb
enough to not check value first time through loop .... I wonder if the
results would be any different with a construct like

INPUT LOW
INPUT HIGH
A = 5
FOR A = LOW TO HIGH ;* supplied 10 & 1 from keyboard
....


Whilst I appreciate optimizations, I also like consistency, and like
you Mark would always expect a "10"



Reply With Quote
  #9  
Old   
Excalibur
 
Posts: n/a

Default Re: What is the answer? - 02-12-2007 , 12:53 AM



Hi
You are setting A equal to 10 with the FOR statement. What goes before is
irrelevant, FOR assigns the value following to the variable and increments
or decrements with the STEP command which defaults to +1. The D3 result is
correct
Peter McMurray
"John Henderson" <jhenRemoveThis (AT) talk21 (DOT) com> wrote

Quote:
Tony Gravagno wrote:

John Henderson wrote:
uniVerse 10 = 5, consistent with the very telling result from
running the 4-line program:

FOR A = 10 TO 1
PRINT "HELLO WORLD"
NEXT A
PRINT A

namely:

Program "T3": Line 4, Variable "A" previously undefined.
Empty string used.

This seems to be the result of engineer attemptS to maintain
some form of local scope versus global. While in the loop the
variable space referenced is the local instance while outside
the loop that local instance no longer exists so the global
instance is used.

That explanation didn't occur to me, so I didn't tell the whole
story.

FOR A = 10 TO 10
PRINT "HELLO WORLD"
NEXT A
PRINT A

outputs:

HELLO WORLD
10

even when I add

A = 5

as a first line above the others.

John



Reply With Quote
  #10  
Old   
John Henderson
 
Posts: n/a

Default Re: What is the answer? - 02-12-2007 , 01:17 AM



Excalibur wrote:

Quote:
You are setting A equal to 10 with the FOR statement. What
goes before is irrelevant, FOR assigns the value following to
the variable and increments or decrements with the STEP
command which defaults to +1.
Yes, that's understood. The point is that unlike

FOR A = 10 TO 10

the statement

FOR A = 10 TO 1

under uniVerse does _not_ set the variable A at all. It's left
at its previous value if it had one, or left unassigned if not.
It's as if the FOR loop never even existed.

Quote:
The D3 result is correct
I accept that it's not incorrect.

John


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.