Thread: What's the best way to write this?

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    What's the best way to write this?

    Hi, I'm wondering what the best way to find out if a variable equals one of two or more values is.

    for example:

    Code:
    if (i == 2 || i == 3) {
    	do_something();
    	do_something_else();
    } else {
    	do_nothing();
    	do_nothing_else();
    }
    or if you use an inline if:

    Code:
    (i == 2 || i == 3) ? do_something : do_nothing;
    the problem i find with this is that i can never equal both 2 and 3 at the same time, and there's no logical XOR operator in C. so is the following way the correct one?:

    Code:
    switch(i) {
    	case 2:
    	case 3:
    		do_something();
    		do_something_else;
    		break;
    	default:
    		do_nothing();
    		do_nothing_else;
    		break;
    }
    or is there a better way to write this?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    All cases are logic OR. Why would you need XOR? If you really need one, write a function for it!
    return ( (a && !b) || (!a && b) );
    Although frankly, that would be easier with C++... Oh well, can't have everything.
    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.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    Well, you could but what i was trying to demonstrate was that an OR where both are true or an AND are useless because it can never evaluate to true. I'm just wondering if the switch statement really is the best way, or if there's a better way to write this. Previously I was using the if(i == 2 || i == 3) method, but now that I think about it it doesn't really seem right for the reason stated above.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The switch won't help either. If both are true, then it will jump to case 2 and fall through, essentially making it a OR solution.
    You're going to have to go with the && solution if you really must have XOR.
    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.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    && and || are short-circuit operators.
    So for A || B
    B is not evaluated if A is true.
    For worst case 2 tests(both A and B are evaluated) are done.
    Last edited by Bayint Naung; 07-29-2010 at 05:02 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-18-2006, 11:25 AM
  2. program to make a floppy write protected
    By shrijeetp in forum C Programming
    Replies: 1
    Last Post: 10-03-2005, 06:00 AM
  3. Reroute where programs write to
    By willc0de4food in forum C Programming
    Replies: 7
    Last Post: 09-21-2005, 04:48 PM
  4. Function to write string 3 times
    By Giggs in forum C++ Programming
    Replies: 15
    Last Post: 12-24-2002, 04:00 PM
  5. write(), read()
    By RedRum in forum C++ Programming
    Replies: 5
    Last Post: 06-09-2002, 11:45 AM