PDA

View Full Version : FOR..NEXT Loop Bug?



MSapper
- 11th April 2011, 19:24
I've had a problem with the FOR..NEXT loop in a couple of projects in the past and it just happened again. I thought this time I would see if it's something I'm doing or a problem with the compiler. Basically I'm using a FOR..NEXT loop to initialize some data in a serially connected Amulet LCD touch screen.

The FOR..NEXT loop which does not work is as follows:

FOR i = 32 to i = 37
addVar = i
GOSUB SetByte
Next i

SetByte:
SEROUT2 tr,84,[$D5,hex2 addVar,hex2 TByte]
SERIN2 rx,84,500,SetByte,[In[0]]
IF In[0] <> $F0 then SetByte
Return

The above code will not work. However the code below will properly set the Amulet variables.

addVar = 32
GOSUB SetByte
addVar = 33
GOSUB SetByte
addVar = 34
GOSUB SetByte
AddVar = 35
GOSUB SetByte
addVar = 36
GOSUB SetByte
addVar = 37
GOSUB SetByte

Any insight would be appreciated. For now I'm avoiding using FOR..NEXT loops.

Thank You
Mike

Darrel Taylor
- 11th April 2011, 20:04
Hi Mike,

The syntax is ...

FOR i = 32 to 37

MSapper
- 11th April 2011, 20:19
Thank you Darrel, dumping the i= does correct the problem. I don't do much programing in basic, primarily in c and java. I guess I need to pay a bit more attention. Just one question shouldn't the compiler throw a syntax error? With the improper syntax it compiles without error.

Darrel Taylor
- 12th April 2011, 01:04
Just one question shouldn't the compiler throw a syntax error? With the improper syntax it compiles without error.

Well no ..., What you had wasn't "Invalid Syntax". In fact it was quite legal.
It just didn't make sense for what you wanted to do.

Logical operators return numbers that can be interpreted as True or False, but those numbers can be used for other purposes as well.
It's up to the programmer to determine when they are appropriate.

If it threw an error, some ingenious person would find a valid use for a logical operator in a FOR loop, and they'd be really ticked off when it didn't work. :)