I know this is a simple question but i am having a problem with an IF statement:

START: #START GOTO command
**CODE REMOVED TO SAVE SPACE**


print "Continue? (lower case only) \n";
$choice = <>;

if($choice eq "y" || $choice eq "yes")
{
    goto CONTINUE;
}
else
{
        goto START;
}


CONTINUE: #CONTINUE GOTO OPTION

print "how high of a number? ";
$number = <>;
$count = 0;


while ($count <= $number) {
....***CODE REMOVED TO SAVE SPACE***

The if($choice eq "y" || $choice eq "yes") keeps skipping to the else and going back to the top. everything else works except for that.

Not infront of my computer to test, but I think you need to chomp($choice) to get rid of the '\n'. The '\n' is on the end of $choice which is why it is failing.
http://perldoc.perl.org/functions/chomp.html

Thanks I ended up changing it to

chomp($choice=<STDIN>)

worked like a charm. And the '\n' ended up not effecting it.

i improve that like this:

START: #START GOTO command
**CODE REMOVED TO SAVE SPACE**


print "Continue? (lower case only) \n";
$choice = <STDIN>;

if($choice ne "y" || $choice ne "yes")
{
    goto START;
}


print "how high of a number? ";
$number = <>;
$count = 0;


while ($count <= $number) {
....***CODE REMOVED TO SAVE SPACE***

Nole_diver, why use a 'goto' when you can use a block and a 'redo' or a 'do' loop as shown below:

case 1:

START:    #START GOTO command
{  # start of block

**CODE REMOVED TO SAVE SPACE**
print "Continue? (lower case only) \n";
chomp( my $choice = <STDIN> );
  if ( $choice !~ /\b(y|yes)\b/ ) {
    redo START;
  }
}# end of block

print "how high of a number? ";
chomp( my $number = <STDIN> );
my $count = 0;
while ( $count <= $number ) {

....***CODE REMOVED TO SAVE SPACE***

case 2:

my $choice;
do {

 **CODE REMOVED TO SAVE SPACE**
print "Continue? (lower case only) \n";
chomp( $choice = <STDIN> );
} while ( $choice !~ /\b(y|yes)\b/ );

print "how high of a number? ";
chomp( my $number = <STDIN> );
my $count = 0;
while ( $count <= $number ) {

....***CODE REMOVED TO SAVE SPACE***

'goto' bites in modern programming...

Edited 4 Years Ago by 2teez

This article has been dead for over six months. Start a new discussion instead.