Q:

can't print "FlimFlam" from Loops

Write a program to perform the following task.
• Get a single integer argument n from the command line.
• Print out the integers from 1 to n, one per line, except that for multiples of 3, print “Flim” instead of the number, and for multiples of 5, print “Flam” instead of the number. For multiples of both 3 and
5, print “FlimFlam”.
For example, running java Example 6
should produce the output
1
2
Flim
4
Flam
Flim
class Example {
    public static void main (String argv[]) {
        if (argv.length != 1)
        usage();
        int n = 0;
        try {
        n = Integer.parseInt(argv[0]);
        } catch (NumberFormatException e) {
        usage();
        }
        for (int i = 1; i <= n; i++)
            if (i % 3 == 0) {
                System.out.println("Flim");
            } else if (i % 5 == 0) {
                System.out.println("Flam");
            } else if (i % 3 == 0 && i % 5 == 0){
                System.out.println("FlimFlam");
            } else {
                System.out.println(i);
        }
  }

  private static void usage() {
    System.err.println("usage: java Example count string");
    System.exit(1);
  }
}

A:

The problem is in the conditions. As either i % 3 or i % 5 satisfies first it never reaches to i % 3 == 0 && i % 5 == 0 condition. Hence, you must be first checking for i % 3 == 0 && i % 5 == 0 and then for rest two conditions.
Below is modified condition:
            if (i % 3 == 0 && i % 5 == 0){
                System.out.println("FlimFlam");
            } else if (i % 3 == 0) {
                System.out.println("Flim");
            } else if (i % 5 == 0) {
                System.out.println("Flam");
            }  else {
                System.out.println(i);
            }

Edit: - if-else-if blocks are like if any of the first occurring condition matches, don't check for rest of the conditions in the if-else-if block.

