Is it possible to find out which number is bigger without using an if statement?
Solution 1
Well you can do:
boolean answer = x > y;
The expression x > y
is just an expression of type boolean
. While boolean
expressions are often used for conditions in if
statements, loops etc, they don't have to be - simple assignment works fine too.
It sounds like you want the reverse though:
boolean answer = y > x;
Then you can use the value of answer
to build the string to display...
Solution 2
Use the ternary operator:
System.out.println(x > y ? "It is true that x is greater than y" : "");
Solution 3
ternary operator "?:"
String output = (x > y)? "x is greater than y":"y is greater than x";
Solution 4
The ternary conditional operator that others mentioned will work. Assuming you are looking for creative ways to do this rather than practical ones, here's another method:
int x = 5;
int y = 10;
while(y > x){
System.out.println("It is true that y is bigger than x.");
return;
}
System.out.println("It is false that y is bigger than x.");
The while
is just acting as a fancy if
, because the return
means the otherwise infinite loop will only execute at most once.
Here's another example that instead relies upon short-circuit boolean evaluation:
public static void main(String...args){
int x = 5;
int y = 10;
boolean answer = (y > x);
boolean testTrue = answer && printTrue();
boolean testFalse = testTrue || printFalse();
}
private static boolean printFalse() {
System.out.println("It is false that y is bigger than x.");
return true;
}
private static boolean printTrue() {
System.out.println("It is true that y is bigger than x.");
return true;
}
Of course you shouldn't do this in real production code, but it can be fun to think of unorthodox ways to code something and it can be helpful for exploring the language.
Solution 5
Your question is tagged as Java but you do not specify Java in your question. In Java there are multiple ways to get the same result that involve testing the boolean expression x > y
somehow, such as the ternary operator. I would consider these equivalent to an explicit if
statement.
Other possibilities:
- Compute the square root of
x - y
. This will raise an exception ify
is bigger. Catch the exception in the caller and report thaty
is the larger quantity. If there is no exception, report thatx
is the larger. - In LISP, Ruby or another language that supports the symbol type, form a list
((symbol x, x), (symbol y, y))
and sort the list. Then report the second symbol as the variable with the larger value. - If using assembly, BASIC, PL/1, etc. you can use an arithmetic expression to choose the target of a
GOTO
statement. Depending on whetherx
ory
is larger, execution will resume at a different part of the code. Or use the list-sorting trick in the previous bullet to select theGOTO
label. - In general, the expression
((x - y) / abs(x - y) + 1) / 2
will produce 1 if x is larger and 0 if y is larger. This result could be used to choose data, a function, etc. out of a list of two alternatives, producing conditional behavior without anif
statement.
Popokoko
Updated on June 24, 2022Comments
-
Popokoko almost 2 years
I'm doing some small program for a beginners programming course and in my program I have 2 variables which hold numbers. Anyway I need to find out which number is bigger and print the appropriate message according to it, for example I have:
int x = 5; int y = 10;
I need to print:
"it is true that y is bigger than x";
Now the thing is that I know I can use a simple if statement but I'm not allowed to use it, now it makes me wonder, is it even possible? If so, how can I do that? How can I check which number is bigger WITHOUT doing something like:
if (x > y) answer = true;
...
Thanks in advance.