Not using an if statement in Java

4.3k views Asked by At

This is a very odd, and quite specific question.

Ultimately I am trying to write a program convert that takes in java source, and transforms it such that it does not use (Among other things)

  • Arrays
  • Loops
  • User defined methods
  • If statements

This is a challenge that I set for myself, after my teacher told me that it was impossible to write a program without using these things.

I have most of these worked out, including function inlining and array substitution, however I cannot work out how to manage an if statement.

In C++ I'd use labels and gotos and maybe ?:, however Java does not support GOTO statements.

My question is this: Given a section of code,

if(CONDITION)
{
   //More code in here
}

How can transform it such that it is functionally the same, however does not use the if keyword. Note that loop structures are also out of the question.

Given this, it would be easy to create else and else if statements. However I am also unsure of how to create loops using this, as there is no GOTO statement and methods are out of the question.

Edit: Please note that switches are also not allowed, nor is recursion (Ruled out by the fact that you cannot define user methods, and a recursive main function wouldn't work with every program) The ?: operator does not work for all situations. AFAIK you cannot call a void function with ?: as it wants to assign a value as part of its operation.

These conditions come from the IB Computer Science SL requires course, I am taking HL and as a class we were laughing at the 'mastery' factors for SL which include 'if' statements (And if fact 3/15 of them are 'User defined methods with params and return types) The challenge is effectively to FAIL a mastery test in SL while still producing a program that functions correctly.

Answer: (By bdares)

String result = (CONDITION)?"0":"A";
try{
    Integer.parseInt(result);
    //Condition is true
} catch(NumberFormatException e){
    //Condition is false
}
5

There are 5 answers

8
AudioBubble On BEST ANSWER
if(A) {
    X();
}
else{
    Y();
}

can be converted to:

A?X():Y();

You can nest these all you want, or simply remove one side of the : and get a simple if. The conditionals are easy.

If you want it to work for void methods, here's a way:

String result = A?"0":"A";
try{
    Integer.parseInt(result);
    X();
} catch(NumberFormatException e){
    Y();
}
0
AudioBubble On

If you were allowed to use anonymous inner classes (these probably classify as user-defined methods, but I'll let you be the judge):

if(COND) {
  X();
} else {
  Y();
}

becomes:

ifReplacement(COND, 
              new Runnable() { public void run() { X();}},
              new Runnable() { public void run() { Y();}});

with signature:

public static void ifReplacement(boolean condition,
                                 Runnable ifBranch,
                                 Runnable elseBranch)

Of course, JDK8 lambdas would make this much nicer:

ifReplacement(COND, ()->X(), ()->Y());
2
x4u On

You can use the conditional operator and a switch:

switch( CONDITION ? 1 : 0 )
{
    case 1:
        //... true code
        break;
    case 0:
        //... false code
        break;
}

For the loops you can unroll your code to some predefined maximum and use labeled breaks to jump out of the unrolled code early based on some condition. You can use break to end any code block in Java not just loops.

The Java language has no goto but the Virtual Machine has it, so you could of course also generate JVM instructions directly although this would be not much different from a regular Java compiler which also translates all ifs an loops into jump instructions.

1
Adamski On

I'm not sure it's possible to write an entire useful program without using an if statement. However, I think what your teacher may be getting at is that you can write code to follow the same "logical" path by using a more object-oriented approach in place of an if statement. For example:

public interface Moveable {
  void move(int x, int y);
}

public class Ball implements Moveable {
  private int x;
  private int y;

  public void move(int x, int y) {
    this.x = x;
    this.y = y;
  }
}

public class NullMoveable {
  public void move(int x, int y) {
    // Do nothing.
  }
}

... and then in your main application code:

Moveable mv = new NullMoveable();    
// Move object; don't care if it's a Ball or a NullMoveable
// so no need to explicitly check with an if-statement.
mv.move(10, 50);

The principle here is that the fewer possible paths there are in your code (due to if statements) the easier it is to test and maintain.

2
Eng.Fouad On

In some cases, you can use bit manipulation. For example:

if(x > 0) // positive number
{
    isPositive = true;
}
else // negative number
{
    isPositive = flase;
}

is equivalent to:

isPositive = (x >> 31) == 0;

EDIT:

This is just an example, of course you can do much more complex bit manipulation with one statement instead of doing it using bunch of if statements.