Loops.java

Download
1/*2 * Loops.java3 *4 * Computer Science S-225 */6 7public class Loops {8 9    private static int count;10 11    public static void mystery1(int n) {12        for (int i = 0; i < n; i++) {13            count++;14        }15    }16 17    public static void mystery2(int n) {18        for (int i = 0; i < n; i++) {19            for (int j = 0; j < n; j++) {20                count++;21            }22        }23    }24 25    public static void mystery3(int n) {26        for (int i = 0; i < n; i++) {27            for (int j = 0; j < 3; j++) {28                count++;29            }30        }31    }32 33    public static void mystery4(int n) {34        for (int i = 0; i < n; i++) {35            for (int j = 0; j < n; j++) {36                for (int k = 0; k < n; k++) {37                    count++;38                }39            }40        }41    }42 43    public static void mystery5(int n) {44        for (int i = 0; i < n; i++) {45            for (int j = 0; j < i; j++) {46                count++;47            }48        }49    }50 51    public static void main(String[] args) {52        int n = 10;53        if (args.length > 0) {54            try {55                n = Integer.parseInt(args[0]);56            } catch (NumberFormatException ignored) { }57        }58 59        mystery1(n);60        System.out.printf("mystery1: %d%n", count);61 62        count = 0;63        mystery2(n);64        System.out.printf("mystery2: %d%n", count);65 66        count = 0;67        mystery3(n);68        System.out.printf("mystery3: %d%n", count);69 70        count = 0;71        mystery4(n);72        System.out.printf("mystery4: %d%n", count);73 74        count = 0;75        mystery5(n);76        System.out.printf("mystery5: %d%n", count);77    }78}