Unconditional GOTO statement
The unconditional transfer of control
means that the sequence of execution will be broken without performing any
condition and the control will be transferred to some statement other than the
immediate next one.
The "Goto" statement is one
of the very important commands in every language. It only tells the program to
jump to the label, part of a different program and start the execution there.
This statement transfers the control
from one part to other without testing any condition. The GOTO is an
unconditional branching statement.
GOTO
The GOTO statement will make the QBASIC
run lines of commands in different order then they are written. The line label
or line number can be either a number or a word of a combination of two as long
as you use in program.
Syntax: GOTO
[ line label | line number]
line
label:
The valid line label should start with a letter.
line
number :
The valid line number to execute next.
Line label of line number is the label
of the line to execute next. Line label is defined using any number or name
followed by colon ( : ).
Example
1 CLS 1 PRINT
"this is first line1": GOTO down 2 PRINT
"this is second2 line": GOTO 3 down: PRINT
"this is third line3": GOTO 2 3 END |
Output |
It
prints "first line" and it goes to the line label "down"
where it prints "third line", then it goes to the line that is
numbered "2" and prints "second line" and goes to line
number 3 and an END statement
which ends the program. |
Example:
2 CLS top: INPUT "enter two
no"; a, b s = a + b PRINT "sum of two
no"; s INPUT "Do you want to continue(y/n)";
ch$ IF ch$ = "Y" OR ch$
= "y" THEN GOTO top END |
|
In
above example the program will continue till your press “Y” or “y” |
Example: 3 CLS x
= 1 label: PRINT
x x
= x + 1 IF
x <= 5 THEN GOTO
label END
IF END |
Output 1 2 3 4 5 |
In the above example the
GOTO statement braches to the instruction after the "label" label
until the value of variable "x" is equal to 5. |
No comments:
Post a Comment