i dont understand this condition

This is a discussion on i dont understand this condition within the C Programming forums, part of the General Programming Boards category; iam creating a program that the user will enter (upper/lower case) from A-H or (a-h) and someone came up with ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    62

    i dont understand this condition

    iam creating a program that the user will enter (upper/lower case) from A-H or (a-h) and someone came up with this code which i dont understand why is it (or ||), and not (and &&)

    Code:
    (((c >= 'A') && (c <= 'H')) || ((c >= 'a') && (c <= 'h')))
    Last edited by joker_tony; 03-21-2008 at 05:32 PM.

  2. #2
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Well do you know of the ASCII format?

    If not, go to www.asciitable.com

    The char, declared as c is being compared to the ASCII value of the letter.

    So if the user enters a character that is (greater than or equal to the value of 'A' AND less than/equal to 'H') OR ( greater than or equal to 'a' AND less than/equal to 'h')

    it will do what that if statement suggests.

    When doing a check for characters entered by the user, you may want to compare the ASCII values of them, because that's how the computer recognizes alphabets, they are assigned an ASCII value, for e.g. 'A' maybe assigned a value of 65.

    The only way to access that ASCII value is to put the alphabet in single quotes: 'A'
    =========================================
    Everytime you segfault, you murder some part of the world

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    You should have put this in the other thread, not started a new one.
    =========================================
    Everytime you segfault, you murder some part of the world

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    62
    thank for your help now i undertand

  5. #5
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    Code:
    if((a==A)||(a==H)&&(a==a)||(a==h))
    That is what you put earlier. You haven't specified the range there. That will only check if the character is equal to A or equal to H, you need to specify a range, hence >= or <=

    Also your && || operands had to be switched around, say it out in your head, it will make sense that way.
    =========================================
    Everytime you segfault, you murder some part of the world

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Omaha, Nebraska
    Posts
    116
    if you need to convert upper or lower case check out the string.h header for tolower() and toupper()
    probably easier then rewriting the whole thing.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Quote Originally Posted by joker_tony View Post
    Code:
    (((c >= 'A') && (c <= 'H')) || ((c >= 'a') && (c <= 'h')))
    Too many unnecessary brackets. That code is equivalent to this:
    Code:
    (c >= 'A' && c <= 'H' || c >= 'a' && c <= 'h')
    If you prefer having unnecessary brackets, then there's certainly no need to go furthur than this:
    Code:
    ((c >= 'A' && c <= 'H') || (c >= 'a' && c <= 'h'))
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,413
    I would prefer the second because it's more explicit in telling how the expression is evaluated.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. condition variable on read/write locks
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 04-29-2009, 09:32 AM
  2. Condition variables
    By sethjackson in forum Windows Programming
    Replies: 16
    Last Post: 03-19-2008, 11:42 AM
  3. SDL Condition variables.
    By antex in forum Game Programming
    Replies: 3
    Last Post: 11-11-2005, 06:11 AM
  4. Looping condition
    By Chaplin27 in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2005, 02:06 PM
  5. Race condition
    By Roaring_Tiger in forum C Programming
    Replies: 5
    Last Post: 10-24-2004, 09:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21