Search⌘ K
AI Features

Solution Review: Letter Grade to GPA

Understand how to create and manage a Perl subroutine that converts letter grades to GPA. Learn to pass parameters and use conditional if-elsif-else statements to return the correct GPA, reinforcing core Perl subroutine concepts.

We'll cover the following...

Let’s look at the solution first before jumping into the explanation:

Perl
sub gpaPoint{
$grade = @_[0];
if($grade eq "A+")
{return 4;}
elsif($grade eq "A")
{ return 4;}
elsif($grade eq "A-")
{return 3.7;}
elsif($grade eq "B+")
{ return 3.3;}
elsif($grade eq "B")
{ return 3;}
elsif($grade eq "B-")
{ return 2.8;}
elsif($grade eq "C+")
{ return 2.5;}
elsif($grade eq "C")
{ return 2;}
elsif($grade eq "C-")
{ return 1.8;}
elsif($grade eq "D")
{ return 1.5;}
elsif($grade eq "F")
{ return 0;}
else
{return -1;}
}
print gpaPoint("B+"); # change value to check for multiple grades

Explanation

We can get the passed parameter by using @_[0] and by using if elsif else statement, we can compare the passed parameter and return the GPA accordingly.