?
Solved

How Evaluate grade without using IF...ELSE statement

Posted on 2011-10-14
12
Medium Priority
?
440 Views
Last Modified: 2012-05-12
Hi,
I have to write function that takes percentage asa parameter and returns grade.

example:
Percentage: 40-50 Grade:C
Percentage: 50 - 60 Grade: B
Percentage: 60-70 Grade:A
Percentage: 70 above grade: A+

How can this be implemented without using If...Else  statement.


Thanks In Advance.
0
Comment
Question by:Ramkisan Jagtap
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 36967790
Have an array of 10 strings containing the grades - F, F, F, F, C, B, A, A+, A+, A+
Divide the percentage by 10 and return the corresponding string (by index) from the array.

So 47% gives 4 when divided by 10 which returns a C grade.


ps.  Much simpler with an if..else construct
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36967797
use swich case.....
0
 
LVL 12

Author Comment

by:Ramkisan Jagtap
ID: 36967800
Thanks nishantcomp2512,
Can we use condition is switvch case... Can you please elaborate it?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:rgn2121
ID: 36967809
I am pretty sure you won't be able to use the switch in C#, VB.Net yes...
0
 
LVL 12

Author Comment

by:Ramkisan Jagtap
ID: 36967827
@rgn2121: I think you are right!!! I have searched a lot on google but found nothing. Is there any other way?
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 36967837
My comment - that is one way.


ps. where p is the percent

if(p>70) return "A+";
if(p>60) return "A";
...

no else statements involved !!
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 36967842
oh by My comment - that is one way. I meant my first comment - the very first comment to your question
0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36967844
Refer:

http://csharp.net-informations.com/statements/csharp-switch-case.htm

Sample code is as below:

int percent;
            switch (percent)
            {
                case (percent > 40 &&  (percent < 50 ):
                  ...

           }
0
 
LVL 14

Accepted Solution

by:
nishant joshi earned 500 total points
ID: 36967859
public string getGrade(int marks)
        {
            string marksbit = "" + (marks > 40 && marks < 50 ? 1 : 0);
            marksbit = marksbit + (marks > 50 && marks < 60 ? 1 : 0);
            marksbit = marksbit + (marks > 60 && marks < 70 ? 1 : 0);
            marksbit = marksbit + (marks > 70 ? 1 : 0);
            switch (marksbit)
            {
                case ("1000"): return "c";
                case ("0100"): return "b";
                case ("0010"): return "a";
                case ("0001"): return "a+";
                default: return "nograde";
            }
        }
0
 
LVL 14

Expert Comment

by:nishant joshi
ID: 36967867
seems good or not???
0
 
LVL 12

Author Closing Comment

by:Ramkisan Jagtap
ID: 36968026
You solutions are good but I just wanted to know is there any conditional statements that can be used for evaluating this. Anyways Thanks!!!
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 36968832
cough cough.
Comment #36967859 which you accepted as the answer contains 4 if ... else statements -  and you did ask how do I do this without using if ... else.


(condition ? true_result : false_result) is an if then else statement just written with an allowed shorthand method.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month17 days, 13 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question