+ 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;
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 :)
+ 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
+ 2
X=3
(5>3 Is true)
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: