could someone tell me what each part of the following line does?
ThanxCode:compare= (sort_type) ? reverse : alpha;
could someone tell me what each part of the following line does?
ThanxCode:compare= (sort_type) ? reverse : alpha;
( condition ) ? true:falseCode:if (sort_type) compare = reverse; else compare = alpha;
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
This says If the first statement is true, do the second part, otherwise, do the 3rd part.
-edit- You beat me by less than a minute. ;DCode:(if this is true) ? (do this) : (if not, do this);
Last edited by stumon; 03-30-2003 at 09:58 PM.
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?
Thanks.Code:if(Result != 5) { a++; b++; } else { C+=2; D-=2; }
Yes, C, C++, Java and C# - all have the conditional operator (?- the only ternary operator!!
Mr. C: Author and Instructor
Perhaps you should avoid use of this operator, e.g.
because you will get difficult to read and understand the programming logic.Code:array_index = (array_size > MAX_array) ? first_array_index : last_array_index;
There is no way to do the similiar thing using ? : operator.
Code:if(Result != 5) { a++; b++; } else { C+=2; D-=2; }
Why not use the comma operator , like this:
Should work fine.Code:(result!=5) ? (a++,b++) : (C+=2,D-=2);
>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.
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
And undefinedness. You can't change a variable more than once between sequence points, soHere's a bubble sort function where conditionals add to the unreadability.
is definitely undefined since parentheses are not a sequence point.Code:n[j]=(n[j+1]<n[j]?n[j]^=(n[j+1]^=(n[j]^=n[j+1])):n[j]);
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
Remind me never to use any programs that you write.Screw conformity. It works for me.
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."Originally posted by XSquared
Here's a bubble sort function where conditionals add to the unreadability.
Quzah.
Hope is the first step on the road to disappointment.