0

Here is the normal code :

       if(a==b)
            printf("equal");
        else if(a>b)
            printf("bigger");
        else
            printf("smaller");

My teacher want to change if/else to this structure:

printf("%s",a>b?"bigger":"smaller")

of course, this just a simple and standard. and I don't know how to apply this to above code to replace if/else.

Mat
  • 202,337
  • 40
  • 393
  • 406
hqt
  • 29,632
  • 51
  • 171
  • 250
  • 1
    I've heard different names, but the term "conditional operator" seems reasonable enough: http://en.wikipedia.org/wiki/%3F: – jglouie Jun 28 '12 at 16:30
  • 5
    it's called a ternary operator http://en.wikipedia.org/wiki/%3F: (for some odd reason the final colon in the wikipedia link isn't taken into account) – fvu Jun 28 '12 at 16:30
  • -1 for asking a homework here in S.O. – Dexter Huinda Jun 28 '12 at 16:33
  • Because in C, the conditional operator is the only ternary operator. You wouldn't generally say, "hmm, what should I call the `/` operator: I know, I'll call it a binary operator". You'd call it division ;-) – Steve Jessop Jun 28 '12 at 16:33
  • @DexterHuinda this not really kind of homework ;) if I remove `homework` pharse, you will don't see it looks like. ;) – hqt Jun 28 '12 at 16:37
  • lmao @hqt, just make sure to throw questions when you have already tried and burnt a few calories thinking for a solution, gets trapped and thus ran to SO for 911. – Dexter Huinda Jun 28 '12 at 16:43
  • @DexterHuinda: Homework questions ask for clarification are welcomed at SO. Homework questions that ask for a solution or for code are the ones not welcomed. There are many experts on SO who may be able to explain issues better than a student's teacher. – Thomas Matthews Jun 28 '12 at 20:13
  • @ThomasMatthews Yes, I know that, because I noticed a lot of **lazy** people go here to get instant code even if they do not understand how it works. – Dexter Huinda Jun 28 '12 at 20:19

4 Answers4

9

It's not generally a good idea to use nested conditional operators, but it can be done:

printf("%s", a==b ? "equal" : (a > b ? "bigger" : "smaller"));
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • No. @fvu sorry, this just a small question from my teacher and this just knowledge. My real assignment and homework different from this :) It hard, long and doesn't need trick to finish – hqt Jun 28 '12 at 16:34
  • @DexterHuinda the idea here on SO is that you GUIDE the student to the answer, to to hand him an answer he can paste into his assignment, just to avoid the dumbing down of students. Cfr Mimisbrunnr's answer. – fvu Jun 28 '12 at 16:34
  • 4
    It's not as if I completed his entire homework assignment for him. It looks like he got 99% of the way there by himself and wanted help after the teacher told him to rewrite part of his (already correct) code. I don't see why it is a problem to ask this question or to answer it directly. – Mark Byers Jun 28 '12 at 16:37
  • @hqt I just hope you don't turn this into a habit. Learn to think for yourself. Because that's the whole idea why it is brought home as a homework, for you to figure it out for yourself. – Dexter Huinda Jun 28 '12 at 16:40
5

Your teacher is telling you to use the ternary operator.

// Generally it looks like this 
( predicate ) ? If_True : If_False ;

the operator can be stacked with it self, ( using parenthetical statements ) to generate more complex logic.

( predicate_0 ) ? If_True : ( ( predicate_1 ) ? If_True : ... )) ;

Though generally stacking the ternary operator in this fashion makes for code a bit harder to read. and in most cases you are better off using the if ... else block

zellio
  • 31,308
  • 1
  • 42
  • 61
  • 1
    for a geeky approach, this is called coding with style. – Dexter Huinda Jun 28 '12 at 16:47
  • I personally like the ternary operator, and use it quite a bit. Though I've had a number of people complain that it's too hard to read ( of course that might have more to do with it being used in Perl ) – zellio Jun 28 '12 at 17:14
1
printf("%s", a == b? "equal" : (a > b? "bigger" : "smaller"));
dreamerkumar
  • 1,540
  • 1
  • 18
  • 28
1

Some like the ternary operator, some do not.

You teacher is good for showing you it, and letting you apply it. Like anything, it can be abused.

However, there is one really cool use of it that cannot be done any other way (AFAIK). Consider the following:

#define MAX(_a_, _b_) (((_a_) > (_b_)) ? (_a_) : (_b_))

Notice you can use this like this:

int x = MAX(5, 17);

I don't know of any way you could do this with an if...else statement. (You could do it with a function call, but that isn't the point.)

Personally, I avoid ternary in all but the simplest of cases.

And for the record, 1 line code in C does not necessarily execute any faster than 4 line code. Be wary of using ternary just so you can write 1 liners.

https://softwareengineering.stackexchange.com/questions/28314/is-the-ternary-operator-evil Ternary operator: bad or good practice?

Community
  • 1
  • 1
Josh Petitt
  • 9,371
  • 12
  • 56
  • 104
  • Yeah it's a good way to make an `if...else` block have a return value like it does in lisp and other similar languages. – Daniel Jun 28 '12 at 19:38