question about ? and : in c++ - c++

Why this statement :
int a = 7, b = 8, c = 0;
c = b>a?a>b?a++:b++:a++?b++:a--;
cout << c;
is not equal to :
int a = 7, b = 8, c = 0;
c = (b>a?(a>b?a++:b++):a++)?b++:a--;
cout << c;
and is equal to :
int a = 7, b = 8, c = 0;
c = b>a?(a>b?a++:b++):(a++?b++:a--);
cout << c;
Please give me some reason. Why ?

Because ? : is right-to-left associative. It's defined like that in the language.

I believe #sth has provided the correct answer, however, I think #Skilldrick got it right on the comments - why the hell would you ever write something like that.
As well as the precedence issue, you really need to be careful when incrementing the same variables in a single statement. There may or may not be sequence points in the statement, and therefore the order of evaluation of the increments might not be guaranteed. You could end up with different results with different compilers or even different optimization settings on the same compiler.

The operators &&, ||, and ?: perform flow control within expressions. ?: behaves like an if-else statement.
c = b>a?a>b?a++:b++:a++?b++:a--;
if ( b>a )
if ( a>b )
a ++;
else
b ++;
else if ( a ++ )
b ++;
else
a --;
b>a? (
a>b ?
a ++
:
b ++
) : ( a ++ ?
b ++
:
a --
)
The associativity is necessary to have behavior like if … else if … else.
Sometimes I use an expression similar to yours for lexicographic sequence comparision:
operator< ()( arr &l, arr &r ) {
return l[0] < r[0]? true
: r[0] < l[0]? false
: l[1] < r[1]? true
: r[1] < l[1]? false
: l[2] < r[2];
}

Related

What's Syntax about this line of Ternary Operator

I trying many time to understand how to work or what's syntax code it ?
greatest=(a>b&&a>c)?a:(b>c)?b : c;
I know the main syntax of Ternary Operator
but in greatest i don't know how come it
the Required I want to anyone explain me that and give me syntax of them .
thanks.
This operator is essentially similar to these if-else statements
if ( a > b && a > c )
{
greatest = a;
}
else if ( b > c )
{
greatest = b;
}
else
{
greatest = c;
}
To make the expression with two nested conditional operators more clear use parentheses
greatest = ( a > b && a > c ? a: ( b > c ? b : c ) );
You could use the standard algorithm std::max instead of the expression with the two nested conditional operators. For example
greatest = std::max( { a, b, c } );

I need to know the explanation of this sentence in c++ " b = (c > 5) ? a/c/2 : 0; "

I need to know the explanation of this sentence in C++
I am editing a library for BL0937 where it uses energy monitoring paramteres , I have just used a b c for simplicity
b = (c > 5) ? a/c/2 : 0;
That can be rewritten as
if(c>5)
{
b = (a/c)/2;
}
else
{
b = 0;
}
x ? y : z is a ternary operator that means "if x, then y, otherwise z."
a/c/2 is just a simple chain of division. As the division operator (/) evaluates from left to right, it is equivalent to (a/c)/2.
So your expression first evaluates c>5 and if that is true, it will evaluate to a/c/2, and otherwise 0. The evaluated value (either a/c/2 or 0) will be assigned to the variable b.
I'll mention more rules of operator precedence for c++, in case that you might be confused of the precedence of / and x ? y : z.
Here is an official page for operator precedence: https://en.cppreference.com/w/cpp/language/operator_precedence
From this, you know operator / has much priority to x ? y : z and =, and thus it is firstly calculated from left to right, i.e. a/c/2 equals (a/c)/2. And then x ? y : z has the same precedence as =, calculated from right to left. Now it means:
b = ( (c>5) ? ((a/c)/2) : 0 );
Hope it helps.

Evaluation of logical condition in ternary operator

I do not know whether parens are required before the ternary operator.
Example:
int a,b,d,e,f; // Some numbers
int l = ( a > b || d < e ) ? a : d;
is surely fine, if a>b or d<e then l = a else l = d.
I am not sure if instead
int l = a > b || d < e ? a : d;
is equally valid, and what about longer statements?
int l = ( a > b || d ) && e > f ? a : d;
I guess that my question is: the ternary operator always treat whatever comes before him as a logical expression and evaluates it, and therefore there is no need for parens before it?
Your first example is correct.
And the second statement is equally valid to the first one.
Your second example reads as follows:
(...) have highest precedence and > and < have higher precedence than && which has higher precedence than || which has higher precedence than ?:.
So
if the result of a > b || d (d is converted to 1 if different than 0) is true
and
if the result of e > f is true
then
l=a
otherwise l=b.

C++ Operator (unsure of)

Is there a c++ operator that i could use for a for loop where it would add or subtract to variables based on whether one of the variables is less than or greater 0.
For instance
int a;
int b;
for(int i=0;i<some_number; i++)
result = a +< b
result = a-> b
No.
You can combine with the ?: operator.
int a;
int b;
for(int i=0;i<some_number; i++)
result = (a < b)? result+b:result-b;
That is if I understood your example correctly.
-> is an existing dereference operator.
Operator ?: is an equivalent to the if...else construct. If the statement before ? evaluates to true, the statement right after the ? gets executed, otherwise the statement after the : gets executed.
Do you want something like this?
result += a > 0 ? b : -b;
Note that this will subtract b if a == 0, which isn't quite what you asked for.
Not directly, but the ternary operator is close.
for(int i=0;i<some_number; i++)
result = (a > 0)?(a):(b);
This line will be equivalent to result = a when a is greater than 0, and result = b elsewise.
It could also be written as result = a?a:b;, but the longer form is more readable.
Not sure if this would be any help?
result = a + (b*(a < b));
result = a - (b*(a > b));
Basically, (a < b) is converted into a boolean, which is basically either 1 (true) or 0 (false). b multiplied by 0 is of course zero, so nothing is added, and b multiplied by 1 is exactly b's value.

Conditional operator "?:" in C++

Why this statement :
int a = 7, b = 8, c = 0;
c = b > a? a > b? a++: b++: a++ ? b++:a--;
cout << c;
is not equal to :
int a = 7, b = 8, c = 0;
c = (b > a? (a > b? a++: b++): a++)? b++: a--;
cout << c;
and is equal to :
int a = 7, b = 8, c = 0;
c = b > a? (a > b? a++: b++): (a++? b++: a--);
cout << c;
Please give me some reason. Why ?
Operator precedence and associativity
Table of operator precedence and associativity for C++
Just put it on multiple lines to see the differences :
c = b>a // true
? a>b // false
? a++
: b++ // b is incremted = 9; c = 8 (post increment)
: a++
? b++
: a--;
is not equal to :
c = ( b>a // true
? ( a>b // false
? a++
: b++ ) // b is incremted = 9
: a++ ) // a = 7 (= 8 after post increment), thus true
? b++ // ... b is incremented = 10, c = 9 (post increment)
: a--;
and is equal to :
c = b>a // true
? ( a>b // false
? a++
: b++ ) // b is incremnted = 9, c = 8 (post increment)
: ( a++
? b++
: a-- );
Also, please note that these (horrible) expressions are deterministic only because the ?: operator is used. This operator is one of the very few operators in the C language where the order of evaluation is actually specified. Had you written some other abomination like i++ + ++i; then the compiler could have evaluated the left operand or the right operand first, which it picks is not defined in the C language.
As a rule of thumb, never use the ++ operator as part of an expression with other operators. Only use it on a line of its own (or as loop iterator). Because, against mainstream belief, there is actually never a reason to use it together with other operators.