Skip to content

Instantly share code, notes, and snippets.

@jmace01
Created September 7, 2017 05:56
Show Gist options
  • Save jmace01/9a64faf0b7ae24afb11efaeb2e23d6a4 to your computer and use it in GitHub Desktop.
Save jmace01/9a64faf0b7ae24afb11efaeb2e23d6a4 to your computer and use it in GitHub Desktop.
Sample of simulating an IF/ELSE without using any conditionals, loops, etc.
import java.io.*;
class Test
{
public static void main(String args[])
{
int a = 1;
IIF.branch(
a % 2 == 0,
() -> System.out.println("A"),
() -> System.out.println("B")
);
}
}
interface Branch
{
public void execute();
}
class IIF {
public static void branch(boolean condition, Branch bTrue, Branch bFalse) {
Branch branches[] = new Branch[2];
branches[0] = bTrue;
branches[1] = bFalse;
branches[Boolean.compare(condition, false)].execute();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment