Thread: questionable logic

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    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,

    i.e.
    Code:
    IF percentage > 89 AND percentage <= 100 THEN
    etc...
    I was just wondering, thanks for the help -Chap

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    I dont see whats wrong with it..

    Well everything above 100% is still an A right? lol

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    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?

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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 >=.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    but I personally think it's more clear
    Not to mention the possibility of an 89.1 if you're using floating-point variables.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    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

  8. #8
    Anal comment spacer DominicTrix's Avatar
    Join Date
    Apr 2002
    Posts
    120
    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.
    "The most important thing about acting is honesty. If you can fake that you've got it made" - George Burns

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Digital Logic
    By strokebow in forum Tech Board
    Replies: 3
    Last Post: 12-09-2006, 01:05 PM
  2. Logic
    By LordBronz in forum C++ Programming
    Replies: 6
    Last Post: 05-23-2006, 05:41 PM
  3. Actors, cues, event based logic.
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 04-27-2006, 10:58 PM
  4. Circular Logic
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-15-2001, 08:10 PM