+ 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.

18th Oct 2018, 2:38 PM
Jumanazar
Jumanazar - avatar
18 Answers
+ 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; }
18th Oct 2018, 3:01 PM
Ronen Gil ืจื•ื ืŸ ื’ื™ืœ ุฑูˆู†ูŠู† ุฌูŠู„ ๐Ÿณ๏ธโ€๐ŸŒˆโ™พ๐Ÿ‡น๐Ÿ‡ฉ๐Ÿ‡ฎ๐Ÿ‡ฑ
Ronen Gil ืจื•ื ืŸ ื’ื™ืœ ุฑูˆู†ูŠู† ุฌูŠู„ ๐Ÿณ๏ธโ€๐ŸŒˆโ™พ๐Ÿ‡น๐Ÿ‡ฉ๐Ÿ‡ฎ๐Ÿ‡ฑ - avatar
+ 6
John Wells, For the most part - I agree with you 100% ! The way programers are making their code unreadable, is sometimes annoying!
18th Oct 2018, 4:55 PM
Ronen Gil ืจื•ื ืŸ ื’ื™ืœ ุฑูˆู†ูŠู† ุฌูŠู„ ๐Ÿณ๏ธโ€๐ŸŒˆโ™พ๐Ÿ‡น๐Ÿ‡ฉ๐Ÿ‡ฎ๐Ÿ‡ฑ
Ronen Gil ืจื•ื ืŸ ื’ื™ืœ ุฑูˆู†ูŠู† ุฌูŠู„ ๐Ÿณ๏ธโ€๐ŸŒˆโ™พ๐Ÿ‡น๐Ÿ‡ฉ๐Ÿ‡ฎ๐Ÿ‡ฑ - avatar
+ 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.
18th Oct 2018, 4:49 PM
John Wells
John Wells - avatar
+ 4
Ronen Gil thats a very nice explanation. do you know what other languages this can be used in?
18th Oct 2018, 4:40 PM
LONGTIE๐Ÿ‘”
LONGTIE๐Ÿ‘” - avatar
+ 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...
18th Oct 2018, 4:48 PM
Ronen Gil ืจื•ื ืŸ ื’ื™ืœ ุฑูˆู†ูŠู† ุฌูŠู„ ๐Ÿณ๏ธโ€๐ŸŒˆโ™พ๐Ÿ‡น๐Ÿ‡ฉ๐Ÿ‡ฎ๐Ÿ‡ฑ
Ronen Gil ืจื•ื ืŸ ื’ื™ืœ ุฑูˆู†ูŠู† ุฌูŠู„ ๐Ÿณ๏ธโ€๐ŸŒˆโ™พ๐Ÿ‡น๐Ÿ‡ฉ๐Ÿ‡ฎ๐Ÿ‡ฑ - avatar
+ 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.
18th Oct 2018, 11:58 PM
Ronen Gil ืจื•ื ืŸ ื’ื™ืœ ุฑูˆู†ูŠู† ุฌูŠู„ ๐Ÿณ๏ธโ€๐ŸŒˆโ™พ๐Ÿ‡น๐Ÿ‡ฉ๐Ÿ‡ฎ๐Ÿ‡ฑ
Ronen Gil ืจื•ื ืŸ ื’ื™ืœ ุฑูˆู†ูŠู† ุฌูŠู„ ๐Ÿณ๏ธโ€๐ŸŒˆโ™พ๐Ÿ‡น๐Ÿ‡ฉ๐Ÿ‡ฎ๐Ÿ‡ฑ - avatar
+ 3
You are welcome. Glad I could help...
18th Oct 2018, 3:24 PM
Ronen Gil ืจื•ื ืŸ ื’ื™ืœ ุฑูˆู†ูŠู† ุฌูŠู„ ๐Ÿณ๏ธโ€๐ŸŒˆโ™พ๐Ÿ‡น๐Ÿ‡ฉ๐Ÿ‡ฎ๐Ÿ‡ฑ
Ronen Gil ืจื•ื ืŸ ื’ื™ืœ ุฑูˆู†ูŠู† ุฌูŠู„ ๐Ÿณ๏ธโ€๐ŸŒˆโ™พ๐Ÿ‡น๐Ÿ‡ฉ๐Ÿ‡ฎ๐Ÿ‡ฑ - avatar
+ 3
Francis, if you liked the explanation, you can +1โ€™d it... TIA
19th Oct 2018, 4:12 AM
Ronen Gil ืจื•ื ืŸ ื’ื™ืœ ุฑูˆู†ูŠู† ุฌูŠู„ ๐Ÿณ๏ธโ€๐ŸŒˆโ™พ๐Ÿ‡น๐Ÿ‡ฉ๐Ÿ‡ฎ๐Ÿ‡ฑ
Ronen Gil ืจื•ื ืŸ ื’ื™ืœ ุฑูˆู†ูŠู† ุฌูŠู„ ๐Ÿณ๏ธโ€๐ŸŒˆโ™พ๐Ÿ‡น๐Ÿ‡ฉ๐Ÿ‡ฎ๐Ÿ‡ฑ - avatar
+ 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
19th Oct 2018, 12:06 PM
Bhavika Bhatnagar
+ 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.
19th Oct 2018, 10:59 AM
Snehal Kalbhor
Snehal Kalbhor - avatar
+ 2
Ternary operators are smarter in the sense they are easy to catch, time saver and enables faster coding...
24th Feb 2019, 5:22 AM
Atiq 69
Atiq 69 - avatar
+ 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.
18th Oct 2018, 6:39 PM
Chris
Chris - avatar
+ 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.
19th Oct 2018, 2:30 AM
Chris
Chris - avatar
+ 1
Thanks...you have expained it in a better and simpler way.
19th Oct 2018, 4:00 AM
Francis Muigai Njeri
Francis Muigai Njeri - avatar
+ 1
If bracket expression result true then statement before : is execute if false then after statement execute
3rd Feb 2019, 12:28 AM
Rahul
Rahul - avatar
+ 1
Thanks everyone. Now I got it!
27th Mar 2019, 2:11 AM
Jumanazar
Jumanazar - avatar
0
thank you very much for a great explanation!
18th Oct 2018, 3:10 PM
Jumanazar
Jumanazar - avatar
0
?: stands for conditional operator shortcut for if else statement (condition)?statement to be true:statement to be false
29th Jul 2020, 3:13 PM
Utkarsh Shrivastava
Utkarsh Shrivastava - avatar