A.Garg A.Garg - 1 month ago 19
Java Question

Semicolon after for loop header

The code below has a semicolon after the for loop header. The output therefore is 56. When the semicolon is removed the output is 75. I understand how we arrive at 75. Can someone explain how we obtain the output of 56?

public class tester
{
public static void main(String args[])
{
int p = 6;
int m = 20,j;
for(j = 1; j<p; j++);
{
m = m + j*j;
}
System.out.println(m);
}
}

PEF PEF
Answer

56 = 20 + 36 = 20 + 6*6, (6 was the value j reached which terminated the loop.)