Search code examples
perlif-statementcontrol-flow

Perl If and Elsif loop help


Alright, I'm still a newbie in Perl, so the answer to this question may seem fairly obvious, but I've done some work in Python, and I've encountered a problem with learning the if, elsif, and else loops; specifically, that they don't work properly. Here's my code:

my $x = 0;
print "X has been set to ". $x .".\n";

while ($x<11)
{
  $x++;
  print "The value of x is now ". $x .".\n";
  if ($x>4, $x<7){
      print "Something\n";
      system ("Pause");
  }

  elsif ($x>7, $x>11){ #<--Here
  print "Something else\n";
  system ("Pause");
  }

  elsif ($x==11){
      print "Last line\n";
  }
} #<-- and Here
system "Pause";

Maybe my problem is obvious by now, but if not, the problem is that it doesn't seem to be evaluating any of the expressions; it just prints the first loop it finds anyway, which in this case is the if loop. If i remove it, or comment it out, it goes straight to the first elsif loop; that is, no matter the value of x, it prints the first loop it finds without any sort of evaluation. When I added

use strict;
use warnings;

I got the warning "Useless use of numeric gt (>) in void context at line 16" and the same thing for line 24. I marked those out in the original code with the arrows and here. Am I doing something/not doing something I should be?


Solution

  • use strict;
    use warnings;
    
    while ($x<11)
    {
      $x++;
      print "The value of x is now ". $x .".\n";
      if ($x>4 and $x<7){
          print "Something\n";
          system ("Pause");
    
      }elsif ($x>7 and $x>11){ #<--Here
        print "Something else\n";
        system ("Pause");
    
      }elsif ($x==11){
          print "Last line\n";
      }
    } #<-- and Here
    system "Pause";