A programming statement
that forwards a user to a different section of the program. Below is
a basic example of how a goto may be used in Perl.
use strict;
start:
print "Please type your password: ";
my $password = <STDIN>;
if ($password =~ /secret/i) {
print "Success";
}
else {
goto start;
}
|
In the above example, when the program is run, it would continue
to prompt the user for a password
until he or she enters "secret". This is accomplished by
first creating a label called
"start:"; at the beginning of the document, if the user
does not enter secret, then the user is referred back to the start
label through the goto statement and this will continue to loop
until properly entered.
Although a goto statement is an easy method of moving through a
program, it is commonly considered bad practice to use goto
statements excessively in your programs because it creates difficult
to read code, commonly known as spaghetti code. However, in some
cases, a goto may be the only option or the best solution. We feel
that it is best left to the programmer
to decide when and when not to use the goto statement and stay away
from the endless debate of using or not using goto statements in
your code.
Also see: Loop, Programming
definitions
|
|
| Resolved | Were you able to locate the answer to your questions? |
|
|