+ 2

The Operator: ? :

Need some help on how to break this problem down. What is the value of x after this code? int x = 5; int y = 3; x = (x > y) ? y : x;

11th Aug 2020, 1:23 PM
Armel Leal
Armel Leal - avatar
4 Answers
+ 5
The ?: operator is pretty much like an if-else statement. Maybe it helps if we rewrite it, like so: int x = 5; int y = 3; if (x > y) { x = y; } else { x = x; } The value of x would be 3 then :)
11th Aug 2020, 1:26 PM
Schindlabua
Schindlabua - avatar
+ 4
int x = 5; int y = 5; if (x > y) { x = y; } else { x = x; } This is called ternary operator! if x>y is true then it will return the element before the ":" (colon) and if that's false then it will return the element after the colon
11th Aug 2020, 1:26 PM
Namit Jain
Namit Jain - avatar
+ 2
X=3 (5>3 Is true)
11th Aug 2020, 3:20 PM
Gabriel Caldeira Bicalho
Gabriel Caldeira Bicalho - avatar
0
This is called a ternary operator, imagine it as an if statement, but shorter. Learn more about it and how it works here: https://en.wikipedia.org/wiki/%3F:
11th Aug 2020, 9:15 PM
0x90
0x90 - avatar