07 NES BASIC PROGRAMMING GUIDE
Decisions, loops and subroutines
IF evaluates a numeric condition. A nonzero value is true. THEN can name a literal line target or start an inline statement. In a running program, IF A=1 THEN 100 jumps to line 100. IF A=1 THEN PRINT "YES" executes the inline PRINT. There is no ELSE. A false IF skips the entire remainder of its line, including colon-separated statements; put work that must always run on a different source line.
GOTO jumps to a literal line number. The target must exist. GOSUB jumps and saves a return location; RETURN resumes after that call, including a colon continuation. A missing target reports UNDEFINED LINE. RETURN without a matching call, or natural end of source while a GOSUB is still pending, reports INVALID RETURN. END/STOP intentionally terminate the program and discard pending control frames.
10 GOSUB 100
20 PRINT "BACK"
30 END
100 PRINT "IN SUBROUTINE"
110 RETURNThis prints IN SUBROUTINE, then BACK. The END prevents normal execution from falling into the subroutine. A subroutine is a convention using line numbers, not a separately declared function with arguments or local variables.
FOR/NEXT repeats using a numeric variable, inclusive limit and optional nonzero STEP. Positive and negative steps work. A loop whose initial value is already past its limit performs zero trips. NEXT names the same loop variable. Do not reuse an active loop variable for another FOR. A missing or mismatched NEXT reports UNMATCHED LOOP. FOR/GOSUB nesting together can use eight frames.
10 A=0
20 FOR I=1 TO 5
30 A=A+I
40 NEXT I
50 PRINT "TOTAL=";A
60 ENDThis prints TOTAL=15. After the loop, I is 6. Changing the step changes which values are added. Arithmetic that overflows while advancing a loop still raises OVERFLOW. To stop a runaway program, hold Start+Select for 60 emulated frames (about a second at normal NTSC speed), or use external-keyboard Ctrl+C.