Please follow me here!

Sunday, November 18, 2012

Java Programming : For Loop Example

Here's an example of using a for loop in JAVA. This program uses a loop inside another to create a list of integers where the number on the left goes between 1 to 4, and the other goes between 2 to 4, making sure the second number is printed as higher than the first number and going through the cut cycle.

public class LoopingProblemNumber1
{
    public static void main(String[] args)
    {
       
        for (int x = 1; x <= 3; x++)
        {
            for (int y = x + 1; y <= 4; y++)
            {
                System.out.println(x + "\t" + y);
            }
        }
    }
}


Output:

1    2
1    3
1    4
2    3
2    4
3    4

Google+ Followers

Popular Posts by Gregory