# questionable logic

• 06-13-2005
Chaplin27
questionable logic
Hey everyone!
I read this pseudocode in a design book, and I found it bothersome, tell me if this is a good way to program, or if it's better to state the exact conditions of if statement execution:

The table we're working with is:
A = 90-100
B = 80-89
C = 70-79

Code:

```IF percentage > 89 THEN         grade = A ELSE         IF  percentage > 79 THEN                 grade = B         ELSE                 IF percentage > 69 THEN                           grade = C                 ENDIF         ENDIF ENDIF```
This is probably just style, but wouldnt it be a better idea to just explicitly state in your code what the exact parameters are? And BTW, I understand this would be written with ELSE IF's...the whitespace used was just taken form the book, :p

i.e.
Code:

```IF percentage > 89 AND percentage <= 100 THEN etc...```
I was just wondering, thanks for the help :) -Chap
• 06-13-2005
mrafcho001
I dont see whats wrong with it..

Well everything above 100% is still an A right? lol
• 06-13-2005
sean
Using your example would involve a lot of unnecessary comparisons, the results of which can be inferred based on previous results. For example after the first ELSE, it is safe to assume that the grade is NOT greater than 89, so why test to see if it's less than or equal to?
• 06-13-2005
Hunter2
I agree with sean. What he's implying is that readability is not significantly improved by explicitly stating the ranges, while performance takes quite a hit.

One change I would suggest, though, is to make it >= 90 and >= 80 and >= 70 rather than > 89 etc... the effect should be the same, but I personally think it's more clear using the >=.
• 06-13-2005
sean
Quote:

but I personally think it's more clear
Not to mention the possibility of an 89.1 if you're using floating-point variables.
• 06-14-2005
Hunter2
Err, right. I assumed it would be working exclusively with integers though, given the pseudocode and the grade table. If working with floating point numbers, then using the grade table provided, explicit comparisons (x <= 100 && x >= 90, x <= 89 && x >= 80, etc.) would actually be required. That's just being pedantic though ;)
• 06-14-2005
Chaplin27
hahaha, very good points have been brought up and I'm very glad I asked that question. Thanks (yet again) for all the help. Cheers, Chap
• 06-14-2005
DominicTrix
As pseudo code I cant see anything wrong with it. But were you thinking in terms of the actually coding and did you mean something like this (more readable and easier to edit):

Code:

```const int gradeA = 80; const int gradeB = 70; const int gradeC = 60; //... if(grade >= gradeA)     // ... etc.```
I would prefer this method everytime.