Results 1 to 14 of 14
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By jim829

Thread: Can smb explain this question?

  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Can smb explain this question?

    Hi! How does the answer happen to be 12?? Thanks in advance!

    Java Code:
    public static void main(String [] args)
    	{
    		int i = 5;  
    		for(i=0;i<=10;i++)
    		{  
    		   i=i+1;  
    		}  
    		System.out.println(i);
     
    		
    	}
    Last edited by ssk2659; 01-29-2015 at 06:31 AM.

  2. #2
    sagarvk is offline Member
    Join Date
    Jan 2015
    Posts
    25
    Rep Power
    0

    Default Re: Can smb explain this question?

    In the loop u have set the variable i to 0 and also put a limit upto 10.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Can smb explain this question?

    Try to figure it out on paper. When you enter the loop for the first time, the value of i is set to 0, throwing away the earlier value of 5.

    That first time through the loop, i get a value of 1 after line 6.

    What happens to the value of i in the next time around the loop (the next iteration)? And the next, and the next, until the value of i does not satisfy i<=10 and the loop terminates?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Can smb explain this question?

    Quote Originally Posted by sagarvk View Post
    In the loop u have set the variable i to 0 and also put a limit upto 10.
    How does that even begin to answer the question?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Can smb explain this question?

    Quote Originally Posted by DarrylBurke View Post
    How does that even begin to answer the question?

    db
    I'm still trying to figure out why the Super Mario Brothers would be able to explain this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Can smb explain this question?

    Quote Originally Posted by gimbal2 View Post
    I'm still trying to figure out why the Super Mario Brothers would be able to explain this.
    Cos if u tlk lk dis, thn it will b much bttr 2 undrstnd.

    I would like to buy a vowel.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Can smb explain this question?

    Quote Originally Posted by ssk2659 View Post
    Hi! How does the answer happen to be 12?? Thanks in advance!
    Did you think about putting in some print statements and examining the output?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: Can smb explain this question?

    Quote Originally Posted by jim829 View Post
    Did you think about putting in some print statements and examining the output?

    Regards,
    Jim
    I tried, I'm getting 11. Do we still take into account the int = 5 ?

  9. #9
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: Can smb explain this question?

    How does one get a value of 1 after line 6??

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Can smb explain this question?

    Quote Originally Posted by ssk2659 View Post
    Do we still take into account the int = 5 ?
    No, its irrelevant.

    Java Code:
    public class Test {
       public static void main(String[] args) {
          int i = 5;
          for (i = 0; i <= 10; System.out.println("i about to increment, i = " + i), i++, System.out.println("i increments to " + i)) {
             System.out.println("i = " + i);
             i = i + 1;
             System.out.println("now i = " + i);
          }
          System.out.println(i);
    
       }
    }
    
     // now rewrite it like this;
    public class Test {
       public static void main(String[] args) {
          int i = 5; // corrected thanks to Gimbal
          for (i = 0; i <= 10;) {
             System.out.println("i = " + i);
             i = i + 1;
             System.out.println("now i = " + i);
             System.out.println("i about to increment, i = " + i);
             i++;
             System.out.println("i increments to " + i);
          }
          System.out.println(i);
    Notice anything similar about the two snippets?

    You can read about it here -> Chapter*14.*Blocks and Statements

    Regards,
    Jim
    Last edited by jim829; 01-29-2015 at 05:48 PM.
    ssk2659 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Can smb explain this question?

    Just a quick note: that second snippet doesn't compile since you were a little too eager when removing 'int' keywords ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Can smb explain this question?

    Quote Originally Posted by gimbal2 View Post
    Just a quick note: that second snippet doesn't compile since you were a little too eager when removing 'int' keywords ;)
    Thanks, in my test case, I only had one class. I will correct it now.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: Can smb explain this question?

    Thank you, Jim! Really helpful! One question: how many years of experience do you have with Java?

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Can smb explain this question?

    Quote Originally Posted by ssk2659 View Post
    Thank you, Jim! Really helpful! One question: how many years of experience do you have with Java?
    Hard to say. Probably 10 (that's decimal by BTW) altogether (and I am continuously learning). But I don't do it professionally. My degree is in CS so I enjoy reading related stuff.

    Regards,
    Jim
    ssk2659 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Don't know how to explain this
    By Jhaz in forum New To Java
    Replies: 0
    Last Post: 08-16-2014, 12:45 PM
  2. Replies: 8
    Last Post: 01-10-2012, 04:40 AM
  3. Replies: 2
    Last Post: 12-13-2010, 01:34 PM
  4. Replies: 3
    Last Post: 12-13-2010, 07:22 AM
  5. Can somebody explain me this plz
    By ccie007 in forum New To Java
    Replies: 4
    Last Post: 05-20-2010, 07:47 PM

Tags for this Thread

Posting Permissions

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