
Zhris
User
Jun 15, 2010, 1:45 PM
Post #2 of 4
(282 views)
|
|
Re: [adiolord] if else question
[In reply to]
|
Can't Post
|
|
Hello, If you want to go the if / elsif / else route, then the following should be fine:
my $finalmark = 60; if ($finalmark < 49) { print "Your grade is F\n"; } elsif (($finalmark >= 49) && ($finalmark < 59)) { print "Your grade is E\n"; } #etc else { print "Your grade is A\n"; } Personally I would use a hash table to provide information about each grade and the score boundaries (this method allows you to change how a grade is allocated by changing the values in the hash table):
my $finalmark = 60; my %grades = ( 'E' => [0, 20], 'D' => [21, 40], 'C' => [41, 60], 'B' => [61, 80], 'A' => [81, 100] ); foreach (keys %grades) { if (($finalmark >= ${$grades{$_}}[0]) && ($finalmark <= ${$grades{$_}}[1])) { print "Your grade is $_\n"; } } Chris
(This post was edited by Zhris on Jun 19, 2010, 1:19 PM)
|