+ 5
Anyone can explain in simple terms the "?:" statement?
#include <stdio.h> int main() { int y; int x = 3; y = (x >= 5) ? 5 : x; /* This is equivalent to: if (x >= 5) y = 5; else y = x; */ return 0; } Why is it like this? I cannot get what it is. Thanks.
18 ответов
+ 9
I used to be confused by these too...
First you have the variable that the result will be assigned into (That is "y =").
Then, you have the condition to be evaluated (That is "(x >=5)")
The next part (between ? and : ) is what will be assigned in the variable in case of a true result (That is "5").
The last part is the value that will be assigned into the variable in case of false result.
So... If we want to rewrite this code it will go like this:
if (x >= 5)
{
y = 5;
}
else
{
y = x;
}
+ 6
John Wells, For the most part - I agree with you 100% ! The way programers are making their code unreadable, is sometimes annoying!
+ 5
Even experts can get confused when the programmer strings 6 or more together as a single assignment. Today's compiler optimizations should generate identical code for both forms so there really isn't a need to use it.
+ 4
Ronen Gil thats a very nice explanation.
do you know what other languages this can be used in?
+ 4
LONGTIE, in one way or the other, the ? operator is avliable in all “C like” oopl - in C, C++ , C#, Java, JavaScript, Swift, php, Python, SAS and even in Ruby, there is some version of the ? operator...
+ 4
Chris, Just as long as it doesn’t make the code harder to read, there is some logic in using the ? operator, in simple (like the one in the quastion, here) cases... But in complex conditions that make the code harder to understand as it is, the ternary ? operator should not be considered.
+ 3
You are welcome. Glad I could help...
+ 3
Francis, if you liked the explanation, you can +1’d it...
TIA
+ 3
? : is ternary operator with syntax
(condition)? (st1) :(st2)
so when the condition evaluates to true, st1 is the value of expression while when it evaluates to false, expression becomes st2
+ 2
Another way to form an if-else statement is by using the ?: operator in a conditional expression. The ?: operator can have only one statement associated with the if and the else.
+ 2
Ternary operators are smarter in the sense they are easy to catch, time saver and enables faster coding...
+ 1
I like the ternary operator. It saves some space, but in my opinion you should never chain them. You can even use it in return statements.
+ 1
In very long codes, of course. It's easier to read if you have sections with different functionalities. While scrolling, an if-statement is way easier to spot.
However, even in complex codes you often write functions with maybe 5 lines, and I like to keep those even more compact by using the ternary operator.
+ 1
Thanks...you have expained it in a better and simpler way.
+ 1
If bracket expression result true then statement before : is execute if false then after statement execute
+ 1
Thanks everyone. Now I got it!
0
thank you very much for a great explanation!
0
?: stands for conditional operator shortcut for if else statement
(condition)?statement to be true:statement to be false