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

Thread: logic problem

  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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);

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,855
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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;
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    brlslngr (11-06-2009)

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by Kor View Post
    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.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,349
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Quote Originally Posted by Philip M View Post
    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.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #6
    Regular Coder
    Join Date
    Mar 2009
    Location
    Portland Oregon
    Posts
    690
    Thanks
    44
    Thanked 63 Times in 62 Posts
    LOL I didn't see the ; either.
    Last edited by TinyScript; 11-07-2009 at 02:48 AM.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts


  •  

    Posting Permissions

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