FAQ  •  Register  •  Login

Compile error - INKEY$ can't be called in a FOR loop

<<

mathez

Posts: 1

Joined: Thu Nov 19, 2015 5:51 am

Post Thu Nov 19, 2015 5:59 am

Compile error - INKEY$ can't be called in a FOR loop

Hello,

I am using version 1.3.0-s1121, though I've also tried 1.4.0s1967, and have the following simple program:

  Code:
10 FOR l = 1 to 30
IF INKEY$ <> "" THEN GOTO 100
NEXT l
GOTO 10
100 PRINT "*"

The compiler gives these errors:
test.bas:6: Syntax Error. Unexpected token 'NEXT' <NEXT>
test.bas:7: Undeclared label "10"

I am a C/C++ programmer, so I might be missing something but looks like if I have INKEY$ in a FOR loop, it doesn't compile. I can remove the GOTO 10 and do a 10: to fix the second error, though that's not very clean syntax with the line number having to be converted to a label.

But, I haven't yet found a workaround for the INKEY$ problem in a FOR loop.

Thanks so much for the help.
<<

boriel

Site Admin

Posts: 1463

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Sun Nov 22, 2015 12:31 pm

Re: Compile error - INKEY$ can't be called in a FOR loop

The problem here is that the IF THEN is now a composed sentence. You must use END IF, to finish it (think of THEN and END IF as { and } in C++).
So your code should be:
  Code:
10 FOR l = 1 to 30
IF INKEY$ <> "" THEN GOTO 100: END IF
NEXT l
GOTO 10
100 PRINT "*"

Actually, THEN can be omitted, and you can insert many sentences, each in a new line:
  Code:
10 FOR l = 1 to 30
IF INKEY$ <> "" THEN
   REM You can put many sentences here
   GOTO 100
END IF
NEXT l
GOTO 10
100 PRINT "*"

Also, you can use Labels (e.g. MyLabel: ) ending them in with a colon (:), instead of line numbers (line numbers are just considered labels here).

Return to Help & Support

Who is online

Users browsing this forum: No registered users and 0 guests

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin for Free Forums/DivisionCore.

phpBB SEO