Another way, which looks a little nicer using Switch statements
use warnings; use strict; use feature "switch"; my $letter; given ($ARGV[0]) { when ([0..39]) { $letter = 'F' ; } when ($_ <= 49) { $letter = 'E' ; } when ($_ <= 59) { $letter = 'D' ; } when ($_ <= 64) { $letter = 'C' ; } when ($_ <= 69) { $letter = 'C+'; } when ($_ <= 74) { $letter = 'B' ; } when ($_ <= 79) { $letter = 'B+'; } when ($_ <= 100) { $letter = 'A' ; } } if ($letter) { print "The student has gotten a $letter grade for the score of $AR +GV[0].\n"; } else { print "Please enter a value between 0 and 100\n"; }

UDPATE: Here is the original question before the owner deleted it:

I started perl a few days ago so are there any ways to make this chunk of code more efficient? The user is supposed to input a score and it'll display something like "The student has gotten a A grade for the score of 100."

#!/usr/bin/perl -w if($ARGV[0] ~~ [0..39]){ print "The student has gotten a F grade for the score of $ARGV[0]. +"; }elsif($ARGV[0] <= 49) { print "The student has gotten a E grade for the score of $ARGV[0]. +"; }elsif($ARGV[0] <= 59) { print "The student has gotten a D grade for the score of $ARGV[0]. +"; }elsif($ARGV[0] <= 64) { print "The student has gotten a C grade for the score of $ARGV[0]. +"; }elsif($ARGV[0] <= 69) { print "The student has gotten a C+ grade for the score of $ARGV[0] +."; }elsif($ARGV[0] <= 74) { print "The student has gotten a B grade for the score of $ARGV[0]. +"; }elsif($ARGV[0] <= 79) { print "The student has gotten a B+ grade for the score of $ARGV[0] +."; }elsif($ARGV[0] <= 100) { print "The student has gotten a A grade for the score of $ARGV[0]. +"; }else { print "Please enter a value between 0 and 100"; }
Thanks in advance. Victor

In reply to Re: Conditional statement by toolic
in thread conditional statement by victorlai

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.