-2

I am fairly new to Java and I ran into this problem once or twice in one of my books. This is a super simple program and I just don't understand why it's not working. I know when you use return, anything after it in the method is meaningless. So does this mean after you perform an for statement or an if statement that is a return?

I am using Java 8 on Windows 8 in the latest version of Eclipse.

This is my simple program:

// Find the sum of 1 through 50 and the average.

class SumAndAverage
{
    public static void main(String args[])
    {
        int sum = 0;
        double average = 0;
        for(int i = 1; 1 <= 50; i++)
        {
            sum += i;
        }

// the following code is "unreachable"
        average = sum / 100;

        System.out.println("The sum is: " + sum);
        System.out.println("The average is " + average);
    }
}
Angelo Alvisi
  • 480
  • 1
  • 4
  • 15
killerx615
  • 45
  • 1
  • 4

2 Answers2

7

1 is always less than or equal to 50, isn't it? You probably meant to compare i with 50:

for(int i = 1; i <= 50; i++)
{
    sum += i;
}
August
  • 12,410
  • 3
  • 35
  • 51
0
for(int i = 1; 1 <= 50; i++)

1 is always less than or equal to 50.

Shahar
  • 1,687
  • 2
  • 12
  • 18