Is this a bug or am I crazy?

General discussion for topics related to the FreeBASIC project or its community.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Is this a bug or am I crazy?

Post by jj2007 »

counting_pine wrote:I think it should just say that the order of these three activities is undefined:
- evaluating the start value
- evaluating the end value
- initialising the iterator

I would say that the most intuitive ordering is the one given above, but it would potentially require using a temporary variable to store the start value before the iterator is initialised.

Because FBC doesn’t perform them in the most intuitive order, I would say that well-written code should not make any assumptions about the ordering, and so it should avoid referring to the iterator when setting the start/end values.
This is indeed the most intuitive order, and the order that Eddie expected to see. Take this simple example:

Code: Select all

' print the ten values before and after the middle element:
Dim as double MyArray(0 To 100)
Dim as integer i
Randomize , 1
for i=0 to 100
  MyArray(i)=Rnd
next

i=50
for i=i-10 to i+10
  print i, MyArray(i)
next

Sleep
Using the iterator is not that exotic. The problem is the FB implementation, and I bet the authors simply did not reflect about this case; otherwise they would have used a temporary variable, as you suggest. There is no performance penalty since the initialisation is done outside the loop.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Is this a bug or am I crazy?

Post by fxm »

Quick Basic works as above (I just tested it).
Thus FreeBASIC, even with the option '-lang qb', is not compatible with Quick Basic.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Is this a bug or am I crazy?

Post by dodicat »

C runs into a loop.
i=0;
for (i=i+2;i<=i+7;i++)
...
...

qb64
dim i as integer
i=0
for i=i+2 to i+7
print i
next i

gives
2
3
4
5
6
7
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: Is this a bug or am I crazy?

Post by jevans4949 »

The fact that C runs into a loop is not illogical; the expressions in a for statement can be anything - not necesarily a loop control. Expression 2 is just evaluated at the bottom of the loop.

Other languages ususally direct you that all the expressions (other than incrementing the loop counter and testing its value) are evaluated at the time the loop is initialised. Some would prevent you from messing about with the loop counter at all while the loop is running - or at least warn you.

In the past I have sometimes set the counter to an arbitrary high value to force exit from a loop, but it's probably not good practice - unless the language provides no other way of prematurely exiting the loop.

In the case under discussion,most people would probably set up a while loop - I would.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Is this a bug or am I crazy?

Post by jj2007 »

jevans4949 wrote:In the case under discussion,most people would probably set up a while loop - I would.
Like this?

Code: Select all

Dim as integer i

for i=i-5 to i+5   ' the case under discussion
  print i;" "
next

i=0
while i<i+5
  print i;" "
  i=i+1
Wend

Sleep
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Is this a bug or am I crazy?

Post by MrSwiss »

Certainly NOT that way (re-introducing ambiguity), instead:

Code: Select all

Dim as integer i

for i=i-5 to i+5   ' the case under discussion
  print i;" "  ' deliveres: -5 To O, aka: NOT as expected
next

i = -5  ' the case under discussion with While-Loop
While i < 6  ' or: i <= 5
  print i;" "  ' deliveres: -5 To +5, aka: as expected
  i += 1
Wend

Sleep
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: Is this a bug or am I crazy?

Post by jevans4949 »

For the while statement,I suppose hopefully the progammer would eventually clock that i will always be less that i+5 !

I suppose with the for statement, maybe we ought to document that the iterator will be set to the startvalue before the endvalue is computed, and any use of the iteraor in the computation of endvalue will reflect this.

I suppose there is a sensible use for this in, e.g.

Code: Select all

for i = 2 to  i^5 step 1
print i
next i
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Is this a bug or am I crazy?

Post by jj2007 »

Read the first post... it certainly cannot be done with a while loop. The implementation of the for ... next loop is slightly broken, that's all.
Eddie wrote:Dim As Integer x
x = 0
For x = (x+2) To (x+7)
?x
Next
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Is this a bug or am I crazy?

Post by MrSwiss »

There is absolutely nothing *broken*, in For ... Next (except, your funny logic)!

You certainly are, as stubborn, as a mule (Btw: sorry, to the mule):

Code: Select all

Dim As Integer x = 0
' taking into account that: x = 2 (on second, aka: endvalue, evaluation)
For x = (x + 2) To (x + 5)
  ? x
Next
alternative *with local iterator*:

Code: Select all

Dim As Integer x = 0

For i As UInteger = (x + 2) To (x + 7)  ' Step = default setting
  Print "iterator: "; Str(i)
Next
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Is this a bug or am I crazy?

Post by jj2007 »

MrSwiss wrote:There is absolutely nothing *broken*, in For ... Next (except, your funny logic)!

You certainly are, as stubborn, as a mule (Btw: sorry, to the mule)
Do you always insult people when they don't accept Swiss logic?
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Is this a bug or am I crazy?

Post by BasicCoder2 »

jj2007 wrote:The implementation of the for ... next loop is slightly broken, that's all.
It certainly doesn't behave the way I would have assumed it would behave if I was pretending to be a computer reading and executing the instructions.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Is this a bug or am I crazy?

Post by MrSwiss »

jj2007 wrote:Do you always insult people when they don't accept Swiss logic?
No, of course not, only people, who dish out insults themselfs.

Like you are known to do, on every occasion, you seem to find appropriate.
(You just got the boomerang, you've thrown yourself, against your own head!)
Btw: you've had it coming, for a long time ...
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Is this a bug or am I crazy?

Post by jj2007 »

MrSwiss wrote:No, of course not, only people, who dish out insults themselfs.
Show me one example where I have posted an insult. To help you distinguish insults from legitimate criticism:
criticism: "my logic is not your logic"
insult: "you are as stubborn as a mule"
adele
Posts: 47
Joined: Jun 13, 2015 19:33

Re: Is this a bug or am I crazy?: Agree to Eddie

Post by adele »

@Eddie,

I am with you.
' On May 24, 2018 2:17, You/Eddie expected s.th. different than
' FB delivered. I agree to You/Eddie. Human speech's logic does too.

Code: Select all

'weird_FOR.bas
x = 10
Print "x=";X
Print "for X=x-2;count down to x-17;stepping is -2"
Print "should translate To: " 
Print "for X=";x-2;" down to ";x-17;" stepping is "; -2
Print "Expect output of ";x-2;" to ";x-17   

   For x = (x-2) To (x-17) Step -2
   ?x;" "; 
   Next

?"---":? "last value for x="; x
Sleep
' Speaking for me, I prefer human logic. Not that of K&R or
' some C/C++ compiler backend.

Adi
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: Is this a bug or am I crazy?

Post by BasicCoder2 »

Using a variable external to the for/next loop as the for/next looping variable when setting up the for/next loop seems to me not best practice?

Code: Select all

dim as integer x
x = 3
for x as integer = x to 5
    print x;",";
next x
print

print x
x = 3
for x = x to 5
    print x;",";
next x
print
print x

sleep
Post Reply