I got three variables, what is the quickest and most efficient way to see which one is the smallest?
Got three variables, how see which one is the smallest?
Copying
(Martí Angelats i Ribera)
#2
I guess (a bit blindly actually) that this is the fastest.
smallest = (var1 < var2 ? var1 : var2);
if (var3 < smallest) smallest = var3;
But it’s easier to use the Math class (but this is a bit slower for sure)
Math.min(var1, var2, var3);
Copying
(Martí Angelats i Ribera)
#4
It’s called ternary operator. It’s stroctures this way:
boolean ? if_its_true : if_its_false
The entire exprecions will be if_its_true
or if_its_false
depending of the boolean
. Of course the boolean
can be a comparation (like in the example).
In the case above it’s equivalent to this:
if (var1 < var2)
{
result = var1;
}
else
{
result = var2;
}
PD: In the ternari operator you get the value before assign it into the variable and in the equivalent you assign differents values to the variable dependig of the condition.