69
n3xus
7y

GUYS WE HAVE BEEN WRITING FOR LOOPS IN JAVA WRONG THE WHOLE TIME. Here is how it should be done correctly...
int i = 0;
for(;(i<10) ? true : false;)
{
i++;
System.out.println(i);
}

Jokes aside though does anyone have any more horrid ways to write this?

Comments
  • 7
    don't initialize i
  • 21
    Have a loop counter factory giving back a loop counter value object then create a loop counter value changed event handler on the loop counter then send an event
  • 34
    System.out.println(0);
    System.out.println(1);
    System.out.println(2);
    System.out.println(3);
    System.out.println(4);
    System.out.println(5);
    System.out.println(6);
    System.out.println(7);
    System.out.println(8);
    System.out.println(9);
  • 2
    @ilikeglue care to write an example?
  • 6
    I thought you were serious for a moment... I was about to say what the hell is wrong with you lol.
  • 8
    @ilikeglue that sounds just like a Java only thing ;D

    Overcomplicating simple stuff for the sake of abstraction
  • 4
    I was gonna say you can while loop with goto statement.
    But goto doesn't exist in Java..
  • 7
    That hurt. Even as a joke. Lol
  • 18
    int i=10;
    while(true){
    try{
    //code here
    int j = 1/--i;
    }catch(Throwable t){
    //end of loop
    }
    }
  • 11
    @lotd

    TAG: while(true){
    //code
    continue TAG;
    }

    You are welcome :^)
  • 4
    @ilikeglue isnt an IterableFactory more abstract? 🤔
  • 11
    int i = 0;
    for (;;) {
    i = i + 1;
    if (i <= 10) {
    //Do stuff
    } else {
    break;
    }
    }

    How about this? Note that the i++ is at the beginning, because who needs 0 indexed stuff, duh! (I would bever say that, but hey you asked for the worst we can think of...)
  • 7
    @Zennoe *loads gun*
  • 5
    Worst thing I've seen, C++:
    int i; int stop = 0;
    for ( ; stop != 1; i++ ) {
    // some code...
    stop = (condition) ? 1 : 0;
    }

    Oh and 'i' wasn't actually used.
  • 2
    @lotd not with that attitude.
  • 0
    @Spacebrain hahahah that is horrid!
  • 0
    @Zennoe *triggered*
  • 0
    int i = 0;

    for(;;) {
    i++;

    if(i == 10)
    break;
    }
  • 0
    //my mind gave birth this senseless abortion

    bool success = repeat(10, new Callable<void>() {
    public Integer call() {
    System.out.println("repeating this");
    });

    boolean repeat(int times, Callable<T> func) {
    return! repeat(0, times, func);
    }

    int repeat(int i, times, Callable<T> func) {
    func.call();
    return i < times ? repeat(++i, times, func) : 0;
    }
  • 1
    switch(true) {
    case $a > $b:
    ...
    case $c == "hello":
    ...
    ...

    sponsored by the PHPepsi community
Add Comment