Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

?: ternary conditional operator behaviour when leaving one expression empty

I was writing a console application that would try to "guess" a number by trial and error, it worked fine and all but it left me wondering about a certain part that I wrote absentmindedly,

The code is:

#include <stdio.h> #include <stdlib.h>  int main() {     int x,i,a,cc;     for(;;){     scanf("%d",&x);     a=50;     i=100/a; for(cc=0;;cc++) {     if(x<a)     {         printf("%d was too big\n",a);         a=a-((100/(i<<=1))?:1);      }     else if (x>a)     {         printf("%d was too small\n",a);         a=a+((100/(i<<=1))?:1);      }     else     {         printf("%d was the right number\n-----------------%d---------------------\n",a,cc);         break;     } } } return 0; } 

More specifically the part that confused me is

a=a+((100/(i<<=1))?:1);  //Code, code a=a-((100/(i<<=1))?:1); 

I used ((100/(i<<=1))?:1) to make sure that if 100/(i<<=1) returned 0 (or false) the whole expression would evaluate to 1 ((100/(i<<=1))?:***1***), and I left the part of the conditional that would work if it was true empty ((100/(i<<=1))? _this space_ :1), it seems to work correctly but is there any risk in leaving that part of the conditional empty?

like image 565
TooBored Avatar asked Jul 23 '10 14:07

TooBored


People also ask

Which is the conditional operator * :? If ?: If else?

Conditional or Ternary Operator (?:) in C/C++The conditional operator is kind of similar to the if-else statement as it does follow the same algorithm as of if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.

Which is called ternary operator ?: && ===?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

What does the ternary operator condition expression1 expression2 do?

The conditional operator, ?: is sometimes called the ternary operator, an operator that takes three arguments. If the test evaluates to true, expression1 is evaluated and returned. If the condition evaluates to false, expression2 is evaluated and returned.

Which is the correct example of a ternary operator?

Example: C Ternary Operator Here, age >= 18 - test condition that checks if input value is greater or equal to 18. printf("You can vote") - expression1 that is executed if condition is true. printf("You cannot vote") - expression2 that is executed if condition is false.


1 Answers

This is a GNU C extension (see ?: wikipedia entry), so for portability you should explicitly state the second operand.

In the 'true' case, it is returning the result of the conditional.

The following statements are almost equivalent:

a = x ?: y; a = x ? x : y; 

The only difference is in the first statement, x is always evaluated once, whereas in the second, x will be evaluated twice if it is true. So the only difference is when evaluating x has side effects.

Either way, I'd consider this a subtle use of the syntax... and if you have any empathy for those maintaining your code, you should explicitly state the operand. :)

On the other hand, it's a nice little trick for a common use case.

like image 88
Stephen Avatar answered Sep 26 '22 03:09

Stephen