What's Syntax about this line of Ternary Operator - c++

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 } );

Related

if then Ternary Operator in C++ [duplicate]

What's the standard line to add to the ternary operator in order to do nothing if the condition is not met?
Example:
int a = 0;
a > 10 ? a = 5 : /*do nothing*/;
Using a seems to do the trick, but I am wondering if there is a more generally accepted way.
That will do it:
a = a > 10 ? 5 : a;
or simply:
if (a > 10) a = 5;
Another option:
a ? void(a = 0) : void();
What's good about this one is that it works even if you can't construct an instance of decltype(a = 0) to put into the 'do nothing' expression. (Which doesn't matter for primitive types anyway.)
You can also use a logical expression (though maybe confusing) in case you don't want to use an if statement.
a > 10 && a = 5
You can do:
a > 10 ? a=5 : 0;
But, I would prefer:
if (a > 10)
a = 5;
Just for a sake of variety, but not recommending as it is very ambiguous.
void do_smth()
{}
bool a = true; // not necessarily
a && (do_smth(), 0);

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.

Multiple Conditions for Swift 'If' Statement?

Is there a way to write an If statement is Swift such as the following?
if a>b or c/d {
//Do Something
}
Just like everywhere:
if a > b || d % c == 0 {
// do sth
}
I assume your c/d means you'd like d to be a multiple of c.
Swift uses the same operators as all C-based languages, so
if a > b || c < d {
}
where || is the OR operator, && is the AND operator.
The list of all operators can be found in Swift Basic Operators
Not sure what c/d condition is supposed to mean.
for and operation we can use
if x==y && y == z { //perform some operations }
for or operation we can use
if x==y || y == z { //perform some operations }

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.

question about ? and : 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 ?
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];
}