-1

I have been asked to create a mark to grade converter in Windows Form Application.

My code below is from the click of a button. Once a user has input their mark into 'Markbox' and the button is clicked the if statement will run and it will find the correct range of the mark. Then it will display a grade, relevant to the mark in 'Gradelb'.

Below is the code under the button click command. I wanted to condense it down to reduce code line space as well as making it more manageable.

void SubmitBtn_Click(object sender, EventArgs e) {

        int mark = int.Parse(Markbox.Text);

        if (mark >= 45 && mark <= 50) Gradelb.Text = "A*";
        else if (mark >= 40 && mark < 45) Gradelb.Text = "A";
        else if (mark >= 35 && mark < 40)  Gradelb.Text = "B";
        else if (mark >= 30 && mark < 35) Gradelb.Text = "C";
        else if (mark >= 25 && mark < 30) Gradelb.Text = "D";
        else if (mark >= 20 && mark < 25) Gradelb.Text = "E";
        else if (mark >= 0 && mark < 20) Gradelb.Text = "U";
        else MessageBox.Show("Please enter a mark between 0-50");

Apologies for any errors or incorrect terminology, I am a new Apprentice employee.

Bal Jhand
  • 61
  • 5
  • Doesn't deserve so many downvotes! Anyway, your if statement is condensed, needs no adjustment – NeedAnswers Aug 14 '14 at 08:57
  • You say "enter a mark between 0-100," and yet any value over 50 will invoke that alert... – Matthew Haugen Aug 14 '14 at 08:57
  • 2
    @Matthijs - quite no need for the sarcasm. If you don't understand what the OP's issue is then you need to re-read. It's not the best quality question, but you know what he's asking. –  Aug 14 '14 at 08:58
  • As for what I *think* your question is, there's no reason to test whether it's below 45 if you've already checked to see if it's above or equal to 45 and found that it wasn't. Same goes for all your other tests. Most of those only need one check. – Matthew Haugen Aug 14 '14 at 08:59
  • I tried having a look at ternary operation but I could understand the whole ideology. This if statement looks at a users marks (which they input; Markbox.Text) and gives out the correct grade by making sure the input value fell in the range in the statement. – Bal Jhand Aug 14 '14 at 09:01
  • 1
    @DeeMac I dont see any sarcasm in his comment, he is just pointing out what is in fact correct – Mo Patel Aug 14 '14 at 09:01
  • What has he pointed out? –  Aug 14 '14 at 09:02
  • 3
    @DeeMac: OP has not stated anything in his question as to what he wants to achieve. He just placed a piece of code and a title. While this may be sufficient to diagnosing the issue OP has; it is not the way you ask for help. – Matthijs Aug 14 '14 at 09:03
  • @Matthijs - nor is it the most productive way of responding (you didn't outline any of those concerns to OP at all). You should be encouraging to new users, and point them to the relevant 'how to ask a question' resources, which is the whole reason they exist. –  Aug 14 '14 at 09:07
  • I've gave this question +1 btw. OP has shown an effort with what he has already, and has shown an eagerness to want to make it more succinct by asking how to improve it (most developers at a junior level would simply run with the code that works rather than striving to improve it). –  Aug 14 '14 at 09:08
  • Sorry Matthew I was just messing around with the numbers earlier and forgot to change the default. – Bal Jhand Aug 14 '14 at 09:08
  • @DeeMac: Yeah, I guess you are right on that. I did ask OP what the issue was, but should've indeed pointed towards the faq-section. – Matthijs Aug 14 '14 at 09:09
  • 1
    @BalJhand: Please refer to `http://stackoverflow.com/help/how-to-ask` prior to asking any other questions. We could really use some information in your question as to what you want to achieve. You could even change the current question for later reference to users browsing SO for help. They may stumble upon your question and only see a piece of code, without any explaining as to what this code does or should be doing; causing users to simply skip over this page while it could provide them with valuable information. – Matthijs Aug 14 '14 at 09:12

1 Answers1

5
string Gr = new string[] { "A*", "A", "B", "C", "D", "E", "U" };

if(mark >=0 && mark <= 50) 
    Gradelb.Text =  Gr[10 - Math.Max(4, (int)Math.Ceiling(mark/5f))];
else 
    MessageBox.Show("Please enter a mark between 0-100");

A word of caution: After living a decade of "one-liner"'s life, I can advise you one thing: There is no guarantee that this code will be any more efficient than yours.

Explanation

Since the grades and the marks range they are linked with follow a fixed pattern, I created an array of grades so that I could refer to each grade by array index. Now all I need is a expression that could convert a given number to index.

46-50 => 0
40-45 => 1
and so on...

This can be done by dividing the number by 5 (since that is the group size in your example). So for example dividing 41 by 5 will give you 8.2. Doing a Ceiling() (which returns nearest greater or equal integer) on it will give 9. Subtracting this value from 10 will give you the index of second group (which is grade A).

Math.Max() (which returns larger of the two parameters) is simply there to ensure that values which are out of array bounds do not cause an exception. This will be the case when marks are 15 or less.

dotNET
  • 33,414
  • 24
  • 162
  • 251
  • +1. This is all the OP needed. On top of your comment, I'd also advise that sometimes - 'elegance' is not worth the trade off you'd make in readability. –  Aug 14 '14 at 09:08
  • could you give me the name or a direction for the method you used so I can research it? – Bal Jhand Aug 14 '14 at 09:36
  • @BalJhand: What method? – dotNET Aug 14 '14 at 09:37
  • The whole code you provided me, is there a special name I could look up? – Bal Jhand Aug 14 '14 at 09:39
  • 1
    @BalJhand: If you're referring to the minimized number of code lines, this is commonly known as one-liner's code. See some pretty examples of one-line programs in different languages at http://en.wikipedia.org/wiki/One-liner_program. – dotNET Aug 14 '14 at 09:44
  • 1
    @BalJhand: There's no general rule for reducing the number of lines of code. This is more of a thought-process where you need to know the capabilities and working of the compiler and all language constructs. Then you combine these elements in an innovative/creative manner to achieve smaller code. This will almost always reduce readability but may not always generate efficiency. – dotNET Aug 14 '14 at 09:51
  • I see where you are coming from. It all occurred when i was showing my code for feedback to a senior developer in my department. He said I could condense my if statements as they are quite long, readable but long. That's when I got curios and researched online until this morning where i got really confused. Thank you any way, i personally prefer my code as it is easier to understand as well as it being slightly more manageable, nevertheless I can see your code being useful in other situations. – Bal Jhand Aug 14 '14 at 10:10
  • @dotNET i think he thinks the array index is a method and that's what hes asking explanation. – Franck Aug 14 '14 at 10:53