Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: logic problem

1. ## logic problem

im trying to add the following numbers: 0+1+2+3+4. The actual answer being 10 obviously. i dont realy get why its coming out as 5 and not 10.

Code:
```var sum = 0;
for (i = 0; i < 5; i++);
{

sum = sum + i;
}

document.write("Sum is: " + sum);```

• for (i = 0; i < 5; i++);
{

sum = sum + i;
}
The above block is equivalent to
Code:
```for(i = 0; i < 5; i++)
`;`//do nothing
sum = sum + i;```

• ## Users who have thanked abduraooft for this post:

brlslngr (11-06-2009)

• This is one of the reasons for I usually write a loop like:
Code:
```for (i = 0; i < 5; i++){
sum = sum + i;
}```
to avoid the temptation to close the line with a semicolon

• Originally Posted by Kor
This is one of the reasons for I usually write a loop like:
Code:
```for (i = 0; i < 5; i++){
sum = sum + i;
}```
to avoid the temptation to close the line with a semicolon
Agreed. The opening brace of a function/if/for should always be on the same line.

• Originally Posted by Philip M
Agreed. The opening brace of a function/if/for should always be on the same line.
i second that, and add "return" to the list of things that should be on one line.

• LOL I didn't see the ; either.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•