Confusion.java


Below is the syntax highlighted version of Confusion.java from §1.3 Conditionals and Loops.


/******************************************************************************
 *  Compilation:  javac Confusion.java
 *  Execution:    java Confusion
 *  
 *  Poorly written code intended to confuse your fellow programmer.
 *
 *  % java Confusion
 *  i = 10
 *  (i = i++) = 10
 *  (i = ++i) = 11
 *  (i = i++ + ++i) = 24
 *
 ******************************************************************************/

public class Confusion {

    public static void main(String[] args) { 
        int i = 10;
        System.out.println("i = " + i);

        i = i++;
        System.out.println("(i = i++) = " + i);

        i = ++i;
        System.out.println("(i = ++i) = " + i);

        i = i++ + ++i;
        System.out.println("(i = i++ + ++i) = " + i);
    }
}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.