Misplaced semicolon in for loop [closed]

2020-01-25 03:10发布

When I am doing my assignments I done a small mistake by placing ; in the for loop like the following code.

for(i=0;i<n;i++);{
    n=n*i;
}

When I compiled the program its compiled with no error but I did not get the output for a while. Then later figured out that I misplaced the semicolon in for loop. What was the bug when I place semicolon after for loop.

class A
{
    public static void main(String args[])
    {
        int i, n=1;
        for(i=1; i<=5; i++);
        n=n*i;
        System.out.println(n);
    }    
}

I am getting output for the following code as 6 instead of 120.

标签: java for-loop
5条回答
够拽才男人
2楼-- · 2020-01-25 03:16

The reason why you're getting 6 is simple. At the last iteration the value of i turns from 5 to 6, the loop won't do another iteration because i=6 and it doesn't satisfy the condition for the for loop any more. Since n=1 and i=6 then the output of n*i is 6.

查看更多
We Are One
3楼-- · 2020-01-25 03:30

You didn't get an output because the semi-colon ended the statement, [ for(I = 0; I < n; I++ ]

The code should be:

for(i=0;i<n;i++) {
     n=n*1;
}
查看更多
放荡不羁爱自由
4楼-- · 2020-01-25 03:31

If you place semicolon after a for loop then it is technically correct syntax. Because it is considered as an empty statement - that means nothing to execute.

In your case -

for(i=0;i<n;i++);  

An empty statement executed up to n times. Or in other words the for-loop simply runs n times.

The curly braces {} after the for loop executed only once. Now the code block inside the curly braces {} considered as a floating block.

查看更多
仙女界的扛把子
5楼-- · 2020-01-25 03:32

When you do this: for(i=0;i<n;i++); you are essentially doing this: for(i=0;i<n;i++) {}. This translates to a loop with no body.

This also happens for while loops: while(..);{<Foo>}. The extra ; will make execute only once.

The same goes for if statements. Doing if(a==b);{<Foo>} will still execute <Foo> for the same reason. If a == b, then, the empty statement will be taken into consideration. Afterwards, <Foo> will be executed. This can give the wrong impression that Java will treat a false as a true since if a != b, then, <Foo> would still be executed.

查看更多
三岁会撩人
6楼-- · 2020-01-25 03:37

for loop can execute only one code block or one statement, even empty ones. So semicolon here represents empty statement (statement which does nothing).

In other words you can think of for(int i; i<n; i++); as for(int i; i<n; i++){}.

So code like

for(int i; i<n; i++);{
    foo();
}

is same as

for(int i; i<n; i++){
    //do nothing, except i++
}
//after loop finishes 
{
    foo();
}

and

for(int i; i<n; i++);
    foo();

is simply

for(int i; i<n; i++){
}
foo();

About

int i, n=1;
for(i=1; i<=5; i++);
n=n*i;
System.out.println(n);

I am getting output for the following code as 6 instead of 120.

As explained earlier your code

for(i=1; i<=5; i++);
n=n*i;

is same as

for(i=1; i<=5; i++){}
n=n*i;

which means that it your loop will not execute n=n*i but will try to execute {} which does nothing. Also at end of each iteration i will be incremented because of i++ so when i will become 5 your and i<5 will be false flow of control will leave loop and will execute n=n*5 which means that n will become n=1*5 which is 5.

If you want to get as result 120 by executing n=n*i in each loop simply remove semicolon after loop.

for(i=1; i<=5; i++)//<--removed semicolon
    n=n*i;

BTW prefer to place code which should be executed by loop or if statement in code blocks,

for(i=1; i<=5; i++){
    n=n*i;
}

This makes your code easier to read and maintain. Also if you will make again your mistake and you will use some auto formatting tool which will indent your code for you you will see that it will be formatted as

for(i=1; i<=5; i++)
    ;
{
    n=n*i;
}

which makes spotting such errors very easy.

查看更多
登录 后发表回答