Solved

# How Evaluate grade without using IF...ELSE statement

Posted on 2011-10-14
Medium Priority
440 Views
Hi,
I have to write function that takes percentage asa parameter and returns grade.

example:
Percentage: 50 - 60 Grade: B

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

0
Question by:Ramkisan Jagtap
• 4
• 3
• 3
• +2

LVL 45

Assisted Solution

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

ID: 36967797
use swich case.....
0

LVL 12

Author Comment

ID: 36967800
Thanks nishantcomp2512,
Can we use condition is switvch case... Can you please elaborate it?
0

LVL 12

Expert Comment

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

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

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

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

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

nishant joshi earned 500 total points
ID: 36967859
{
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+";
}
}
0

LVL 14

Expert Comment

ID: 36967867
seems good or not???
0

LVL 12

Author Closing Comment

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

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

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