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
  1. #1
    fok
    fok is offline
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Confused about this simple code

    Hey guys,
    I am new here, first year university student doing ICT and I have a test tomorrow.
    I went through example questions and came across this question.


    I thought the answer was D but the answer turned out to be B.
    I don't really understand why this is.
    Is anyone kind enough to explain it to me?
    Thanks

    Cheers,
    Fok

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    BTW, when posting here please help us to help you by making it easier to copy, test and debug your scripts by following the posting guidelines and wrapping your code in CODE tags, not using fancy fonts. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

    In Javascript

    Code:
    <script type="text/javascript">
    for (var i=0; i<10; i++) {
    document.write (i + " " );
    document.write ((i+1) + " " );
    }
    </script>
    results in
    0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
    and I would have thought that Java was the same.


    Quizmaster: The Goethe Institute in London promotes the culture of which country?
    Contestant: Nepal.
    Last edited by Philip M; 04-10-2013 at 10:59 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    fok
    fok is offline
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah, sorry.
    I didn't read posting guide lines and not quite sure how to edit my post, can't seem to find button.
    Also, if mod sees this please move this to the correct function.

    @Phillip

    Yes, I too thought that it would result in 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
    but when I execute the code in greenfoot, it prints out:
    0 1 2 3 4 5 6 7 8 9 1

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,734
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Ah. Is it that the opening brace is in the wrong position so the line
    system.out.print ((i+1) + " ");
    is executed only once, on the final pass of the loop?
    That would happen in Javascript, but it would result in 0 1 2 3 4 5 6 7 8 9 11 (not 1, not 10)

    As a newcomer you are not allowed to edit your posts. You can find a list of (and links to) the forum moderators at the bottom of the page.
    Last edited by Philip M; 04-10-2013 at 11:45 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Regular Coder
    Join Date
    May 2012
    Location
    France
    Posts
    216
    Thanks
    0
    Thanked 29 Times in 27 Posts
    It's depends only of braces for the two instructions after the for loop !
    Without braces like initially the response is b, with braces the response is d !
    Last edited by 007julien; 04-11-2013 at 11:23 AM.

  • #6
    fok
    fok is offline
    New to the CF scene
    Join Date
    Apr 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, I understand that the positioning of the braces changes it, but I don't really understand why it's 0 1 2 3 4 5 6 7 8 9 1 and not 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10

  • #7
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    That's because the misplaced braces make the code equivalent to

    Code:
    for (int i = 0; i < 10; i++) {
        System.out.print(i + " ");
    }
    System.out.print((i+1) + " ");
    And I'm sure you understand why this outputs the string given by answer B. The reason the above is equivalent to the original code is because many languages, including Java, will only execute the first line after the for-loop command if there were no braces. So the above (and therefor the original code) is also equivalent to

    Code:
    for (int i = 0; i < 10; i++)
        System.out.print(i + " ");
    System.out.print((i+1) + " ");
    (Note the formatting that signals which line belongs to the loop and which line doesn't).
    Not using braces even for one-line loops is considered bad practice by many developers as it makes code less maintainable.

    The braces surrounding the whole piece of code are completely irrelevant in this case.


  •  

    Posting Permissions

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