Ultimate Amiga

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: (Bug Discussion) For...To...Next...Step always performs one iteration  (Read 5551 times)

0 Members and 1 Guest are viewing this topic.

Lonewolf10

  • AMOS Extensions Developer
  • AMOS Dev
  • A2000
  • *****
  • Karma: 3
  • Offline Offline
  • Gender: Male
  • Posts: 618
    • http://www.aliensrcooluk.com

  • For ... To ... Next ... Step always performs one iteration of the loop no matter what is specified for the starting, ending and step values.  E.g. For X=X1 To X2 : <do something> : Next X will perform one loop even if X1 is greater than X2.

I'm not sure that that is a bug. For... Next loops should always run once, because no testing of the values is done until the Next instruction is executed. When the Next instruction is executed the value 1 is added (unless specified otherwise by use of the Step instruction) to the value of the specified variable. If it is greater (or less than, if counting downwards), or equal to, the specified target value then AMOS carries on past the Next instruction. If the value hasn't reached the specified target, then AMOS jumps back to the instruction after the corresponding For instruction.
I hope you understand what I am trying to say...


Regarding bugs, here are two not yet mentioned (unless I missed it):

  • If a line is longer than the screen, and the screen has scrolled to the right, when you use the delete line shortcut (Control+Backspace) the line is deleted, but the screen isn't scrolled back to the far left (C-0 position). AMOS Pro still works fine afterwords, but it can be off-putting.
  • If you have multiple programs open in multiple windows, AMOS Pro crashes sometimes. I haven't used this feature in years, so it maybe a memory related issue.
« Last Edit: January 14, 2013, 09:41:39 PM by Lonewolf10 »
Logged

bruceuncle

  • AMOS Dev
  • A500
  • *****
  • Karma: 6
  • Offline Offline
  • Gender: Male
  • Posts: 425
  • WINUAE Amiga User
Re: (Bug Discussion) For...To...Next...Step always performs one iteration
« Reply #1 on: January 15, 2013, 09:42:58 AM »

  • For ... To ... Next ... Step always performs one iteration of the loop no matter what is specified for the starting, ending and step values.  E.g. For X=X1 To X2 : <do something> : Next X will perform one loop even if X1 is greater than X2.

I'm not sure that that is a bug. For... Next loops should always run once, because no testing of the values is done until the Next instruction is executed.

No, sorry, it is a bug.  The way you describe it is the way AMOS currently handles it - as though the test doesn't occur until the Next is reached.  But it's actually supposed to be done where the For part is. 

A For ... Next loop is always regarded as a shorthand for a specific type of While ... Wend loop.  So the test to terminate the loop is at the beginning of the loop, not at the end.  If the termination test returns False upon entry, the loop is never executed. 

It's the opposite structure to a Repeat ... Until loop where the termination test is always at the end of the loop.  So a Repeat ... Until will always execute at least one iteration of the loop.

For Counter = StartValue To EndValue Step StepValue
   Statements
Next Counter


is equivalent to:

Counter = StartValue
While Counter * Sgn(StepValue) <= EndValue * Sgn(StepValue)
   Statements
   Counter = Counter + StepValue
Wend


If you try For I = 2 To 1 : Print I : Next I in any other version of Basic, you'll see what I mean.

(Nerdy techo note.  If you've never programmed in C, quit reading now before brain damage sets in.  Well, that's how C syntax always grabs me.    ;D )

It derives from the C language standard:

for ((expr1; expr2; expr3)
   statement


is equivalent to:

expr1;
while (expr2) {
   statement
   expr3;
}


See Kernighan and Richie - The C Programming Language ('The White Book'  ;)).
Logged
Repeat after me ...  "The AMOS Pro architecture is complex but it is not complicated."

SamuraiCrow

  • compile-time wierdo
  • Forum Mod
  • A1200
  • *****
  • Karma: 5
  • Offline Offline
  • Gender: Male
  • Posts: 946
  • Compile-time wierdo
Re: (Bug Discussion) For...To...Next...Step always performs one iteration
« Reply #2 on: January 16, 2013, 01:58:05 AM »

Commodore 64 BASIC always did FOR..NEXT using the REPEAT...UNTIL way also.  Probably because it was shorter and faster to do so, not that it's terribly relevant to this discussion.  Microsoft BASICs on the PC always did the WHILE loop shorthand though.
Logged

Hungry Horace

  • Amorphous Blue-Blob Man
  • Site Admin
  • A4000T
  • ******
  • Karma: 307
  • Offline Offline
  • Gender: Male
  • Posts: 3,364
  • Don't forget... Ameboid's need love too!
    • AUW
Re: (Bug Discussion) For...To...Next...Step always performs one iteration
« Reply #3 on: January 16, 2013, 01:29:14 PM »

changing FOR / NEXT structure would break a lot of old AMOS code though.

Surely the solution is to have an option as to how it operates ("classic" or "correct") , and specify in docs that WHILE / WEND or REPEAT/ UNTIL commands should be used by a user to specify a "test before or after code"  loop in their program, if they want to ensure other users get the same operation.

Logged
Quote from: KillerGorilla
because winuae is made of code and your amiga is made of stuff

bruceuncle

  • AMOS Dev
  • A500
  • *****
  • Karma: 6
  • Offline Offline
  • Gender: Male
  • Posts: 425
  • WINUAE Amiga User
Re: (Bug Discussion) For...To...Next...Step always performs one iteration
« Reply #4 on: January 18, 2013, 05:33:08 AM »

Commodore 64 BASIC always did FOR..NEXT using the REPEAT...UNTIL way also.
I never owned a C64 so I didn't realise that.  My machine in that period was an Hitachi Peach with a Basic that had the WHILE ... WEND structure.  So this is the first time I've come across this variation.

changing FOR / NEXT structure would break a lot of old AMOS code though.
Completely agree.  As long as the programmer knows how it behaves, we shouldn't touch it.

In the ref docs database, I've used two flags for 'bugs':
One for an outright bug (i.e. doesn't work or crashes)
One for where it works but doesn't follow accepted Basic language conventions (i.e. like this FOR ... NEXT one). 

The latter ones are more reminders to me to emphasise the differences for the docs rather than something to be fixed.

@ Hungry Horace.
Thanks for the file requester bug.  I would never have found that one!

I had a weird one the other day in the editor.  I hid a window then later tried to get it back with Next Window instead of choosing to Edit it from the menu, and the entire editor text window then started on the top scan line!  Completely overwriting the icons on the toolbars.  Haven't been able to reproduce it again though, so it may have been something I'd upset elsewhere that caused it.
Logged
Repeat after me ...  "The AMOS Pro architecture is complex but it is not complicated."
Pages: [1]   Go Up
 

TinyPortal 2.2.2 © 2005-2022