? and :

This is a discussion on ? and : within the C Programming forums, part of the General Programming Boards category; could someone tell me what each part of the following line does? Code: compare= (sort_type) ? reverse : alpha; Thanx...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    24

    ? and :

    could someone tell me what each part of the following line does?

    Code:
    compare= (sort_type) ? reverse : alpha;
    Thanx

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Code:
    if (sort_type)
       compare = reverse;
    else
       compare = alpha;
    ( condition ) ? true:false
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    This says If the first statement is true, do the second part, otherwise, do the 3rd part.

    Code:
    (if this is true) ? (do this) : (if not, do this);
    -edit- You beat me by less than a minute. ;D
    Last edited by stumon; 03-30-2003 at 08:58 PM.

  4. #4
    Xei
    Xei is offline
    Registered User Xei's Avatar
    Join Date
    May 2002
    Posts
    719
    I never knew that C/C++ supported syntax like that. Is there any other types like that? I also have another question, is there a way to do something 'similar' to the following using such syntax?
    Code:
    if(Result != 5)
    {
         a++;
         b++;
    }
    else
    {
         C+=2;
         D-=2;
    }
    Thanks.

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Thumbs up

    Yes, C, C++, Java and C# - all have the conditional operator (?- the only ternary operator!!
    Mr. C: Author and Instructor

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    9
    Perhaps you should avoid use of this operator, e.g.
    Code:
    array_index = (array_size > MAX_array) ? first_array_index : last_array_index;
    because you will get difficult to read and understand the programming logic.

    There is no way to do the similiar thing using ? : operator.
    Code:
    if(Result != 5)
    {
      a++;
      b++;
    }
    else
    {
      C+=2;
      D-=2;
    }

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    27
    Why not use the comma operator , like this:

    Code:
    (result!=5) ? (a++,b++) : (C+=2,D-=2);
    Should work fine.

  8. #8
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >Perhaps you should avoid use of this operator

    You should not avoid it, but use it with care. In the first example you gave the use of the operator is done in a very clear way. In the other example, I'd prefer the if-else-construction for readability.

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Here's a bubble sort function where conditionals add to the unreadability.

    Code:
    void bubbleSort(int n[],int s)
    {
    	int i,j;
    	for(i=0;i<s;i++)
    		for(j=0;j<s-1;j++)
    			n[j]=(n[j+1]<n[j]?n[j]^=(n[j+1]^=(n[j]^=n[j+1])):n[j]);
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Here's a bubble sort function where conditionals add to the unreadability.
    And undefinedness. You can't change a variable more than once between sequence points, so
    Code:
    n[j]=(n[j+1]<n[j]?n[j]^=(n[j+1]^=(n[j]^=n[j+1])):n[j]);
    is definitely undefined since parentheses are not a sequence point.

  11. #11
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Screw conformity. It works for me.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  12. #12
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    Screw conformity. It works for me.
    Remind me never to use any programs that you write.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by XSquared
    Here's a bubble sort function where conditionals add to the unreadability.
    That's a pretty poor execuse of why not to use said operator. That's like saying "Here's an example where pointers are used in a poorly illustrated, confusing example. So you should avoid pointers all together."

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

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