Created
September 7, 2017 05:56
-
-
Save jmace01/9a64faf0b7ae24afb11efaeb2e23d6a4 to your computer and use it in GitHub Desktop.
Sample of simulating an IF/ELSE without using any conditionals, loops, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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