New to C++, IF/ELSE duplicate code

krisl100

Beta member
Messages
1
In my last excercise I have an IF/Else statement & a second pass counter, here is the code:

How can I remove the second pass counter (++pass) & the duplicate line of code? I just need a very simple solution. Thanks.

//Students are allowed to fail one exam, not both, this is how we achieve that goal.
if ((midterm < EXAMSPASS && final < EXAMSPASS) || course < PASSMARK)
{
fail++;
cout << endl << "RESULT: " << name << " " << "Failed " << endl;
cout << "-------------------------------------------" << endl;
}
else if (course >= HONOURS)
{
pass++;
cout << endl << "RESULT: " << name << " " << "Passed with Honours "
<< endl;
cout << "-------------------------------------------" << endl;

}

else
{
//Since pass+1 is set above, need to reset the counter this way
//so it counts the passed results correctly
++pass;
cout << endl << "RESULT: " << name << " " << "Passed " << endl;
cout << "-------------------------------------------" << endl;
}
 
Well, first, use code tags next time, it'll make my reading easier. Second, I'm not completely certain what you want removed or remade from your remarks.

Your original code looked like this
Code:
//Students are allowed to fail one exam, not both, this is how we achieve 
//that goal.
if ((midterm < EXAMSPASS && final < EXAMSPASS) || course < PASSMARK)
{
fail++;
cout << endl << "RESULT: " << name << " " << "Failed " << endl;
cout << "-------------------------------------------" << endl;
}
else if (course >= HONOURS)
{
pass++;
cout << endl << "RESULT: " << name << " " << "Passed with Honours "
<< endl;
cout << "-------------------------------------------" << endl;

}

else
{
//Since pass+1 is set above, need to reset the counter this way
//so it counts the passed results correctly
++pass;
cout << endl << "RESULT: " << name << " " << "Passed " << endl;
cout << "-------------------------------------------" << endl;
}

I'm going to assume that you'd like to optimize the if - else constructs and maybe loose a counter. If - else statements can often be out of control, unless you take the time to think hard about the actual order that they fall through in under most likely cases. In most classes, grades will be passing most of the time. Failing marks are the exception. Your logic should reflect that.

I'll highlight code that has been moved in red for down, green for up. Code changes will be in blue. I'd optimize something like this

Code:
[COLOR=Blue]bool fail_flag = FALSE;
// Breaking out this condition and assigning a bool flag allows it to carry as 
// guard for all tests with less overhead.  It is marginally possible to pass 
// on grade points and still fumble two exams!
if(midterm < EXAMSPASS && final < EXAMSPASS){fail_flag = TRUE;}[/COLOR]


//Students are allowed to fail one exam, not both, this is how we achieve 
//that goal.
if ( [COLOR=Blue]fail_flag[/COLOR] || course < PASSMARK)
{
fail++;
cout << endl << "RESULT: " << name << " " << "Failed " << endl;
cout << "-------------------------------------------" << endl;
}
// This test now actually replaces the default in the else.  It will count all
// that passed with the variable called pass, whether they've honours or
// not.  
[COLOR=Green]if (course >=[COLOR=Blue] PASSMARK && !fail_flag[/COLOR] )[/COLOR]
{
pass++;
cout << endl << "RESULT: " << name << " " << "Passed with Honours "
<< endl;
cout << "-------------------------------------------" << endl;
}
[COLOR=Red]if(course >=HONOURS [COLOR=Blue]&& !fail_flag[/COLOR])[/COLOR]
{
honours++;
cout << endl << "RESULT: " << name << " " << "Passed " << endl;
cout << "-------------------------------------------" << endl;
}

[COLOR=Blue]// I leave it to your ingenuity to find a mechnaism within the loop this is 
// contained in to ASSURE that fail_flag always begins a cycle as FALSE.
// It may work that way because of the placement it has already.[/COLOR]

I believe that the above structure solves all your problems. Perhaps not exactly as you meant, but I think it shows how just a positional change or a separate flag can be used to streamline code. The point of the flag is that it is very efficient to test and *NOT* enter code blocks, more at least then entering two and using one to correct for a differential numbering problem. Since the fail_flag is set early, you check it at all stages and there are less ops to read a variable and perform a logical op on it than to perform a couple of extra logical and grouping operations each time. It still guards against double exam pass failure, and a check can easily be performed in each filter segment.

The first if sets fail_flag. The second uses fail_flag and a course vs. PASSMARK comparison to find failed students. The third finds those that passed, at any level. The fourth is something that I threw in extra. You don't actually need it to calculate pass / fail students. But it can do the honour students easily and reliably, without affecting the total number passed. It increments a different variable to keep count of only those that are honour students.

As an exercise to yourself, answer me this: Can the if block used to detect failed students be moved to a position lower in the code? Would it be effective to do so? Are there other changes that might optimize execution?

I'll leave it at that. I hope I've helped you and piqued your interest a bit.
 
Back
Top Bottom