3 - 23 3 - 23
MELSEC-Q
3 LET'S CREATE AND EXECUTE A PROGRAM
3.6 Jumps and Loops
BASIC programs are typically executed in increasing order of line numbers. However,
there are instances when it is better that the order of execution is changed. AD51H-
BASIC has the following instructions to change the order in which execution is carried
out in a program.
GOTO______________
Jump unconditionally
ON GOTO___________
Jump depending on a value
FOR-NEXT__________
Loop for the number of times specified
WHILE-WEND________
Loop while a certain condition is met
3.6.1 Jump unconditionally
Use the GOTO instruction to jump to the specified location unconditionally.
10 A=1
20 PRINT A
30 A=A+1
40 GOTO 20
RUN
1
2
3
…
The execution is moved to the line with
the line number specified by the GOTO
instruction.
P ress th e B re ak ke y o r the C trl + C ke ys
to forc e a n exe cutio n to sto p . A fter a
"B rea k in " m e ssag e is d ispla yed ,
it w ill re tu rn to O K .
Will it ever stop? ……
3.6.2 Jump depending on a value
The ON-GOTO instruction expands the functionality of the GOTO instruction; it is
possible to jump to a different line depending on the value of a variable. This allows
multiple destinations to be specified for the jump.
The jump destination can
be changed depending on
the value of the variable
after ON.
There is no restriction
on the number of line
numbers that can be
written after the
GOTO instruction.
10 INPUT"A=" ;A
20 ON A GOTO 40,50,60
30 END
40 PRINT "ONE" GOTO 10
50 PRINT "TWO" GOTO 10
60 PRINT "THREE" GOTO 10
RUN
A=?2
TWO
A=?3
THREE
A=?4
OK
Enter
Enter
Jump destination line number when A is 1.
When A=4, there is no jump destination,
so the following line is executed.
The next line is END, so the program ends.
Jump destination line number when A is 2.
Jump destination line number when A is 3.