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–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:12:31 EDT 2022.