简体   繁体   中英

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?

if (expr1, expr2) will ignore expr1 . Did you perhaps mean and or && ? (Also, if so, your elsif should be testing x<11 .)

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM