TLET
TLET

Reputation: 33

For Loop with two variables

Is it illegal to set j = i in the second for loop? It seems to work for the first iteration, however, after that it doesnt print anything, is there a more appropriate way to do this? I rewrote it to use a while loop, and it worked perfectly, so whats wrong with the for loop?

public class DaysOfChristmas {
public static void main ( String[] args )
{
    int i,j;
    String day = "";
    String verse = "";

    for ( i = 1; i <= 12; i++)
    {
        switch (i)
        {
            case 1: day = "first";
            break;

            case 2: day = "second";
            break;

            case 3: day = "third";
            break;

            case 4: day = "fourth";
            break;

            case 5: day = "fith";
            break;

            case 6: day = "sixth";
            break;

            case 7: day = "seventh";
            break;

            case 8: day = "eight";
            break;

            case 9: day = "ninth";
            break;

            case 10: day = "tenth";
            break;

            case 11: day = "eleventh";
            break;

            case 12: day = "twelfth";
            break;
        }

        System.out.printf("On the %s day of Christmas my true love gave to me\n", day);

        for ( j = i; j == 1; j--)
        {
            switch (j)
            {
                case 1: verse = "A Partridge in a Pair Tree";
                break;

                case 2: verse = "Two Turtle Doves";
                break;

                case 3: verse = "Three French Hens";
                break;

                case 4: verse = "Four Colly Birds";
                break;

                case 5: verse = "Five Golden Rings";
                break;

                case 6: verse = "Six Geese-a-Laying";
                break;

                case 7: verse = "Seven Swans-a-Swimming";
                break;

                case 8: verse = "Eight Maids-a-Milking";
                break;

                case 9: verse = "Nine Ladies Dancing";
                break;

                case 10: verse = "Ten Lords-a-Leaping";
                break;

                case 11: verse = "Eleven Pipers Piping";
                break;

                case 12: verse = "Twelve Drummers Drumming";
                break;
            }
            System.out.printf("%s ", verse);
        }
        System.out.println();

    }
}

}

Upvotes: 0

Views: 199

Answers (2)

Sidharth Mudgal
Sidharth Mudgal

Reputation: 4264

for ( j = i; j >= 1; j--)

should work. The problem was that the second loop ONLY EXECUTES when j is 1. Otherwise it doesn't execute at all. And j is 1 only the first time when i is 1.

Upvotes: 4

Makoto
Makoto

Reputation: 106440

It's not illegal to do that. However, consider your condition in the loop:

 for ( j = i; j == 1; j--)

That loop will only ever execute when j is exactly 1. You should definitely rethink your logic here.

Upvotes: 0

Related Questions