Results 1 to 7 of 7
  1. #1
    jenkins is offline Member
    Join Date
    Jan 2017
    Posts
    1
    Rep Power
    0

    Default why does this only loop 2 times ?


    for (int i = 2; i < 10; i = i*i) {
    System.out.println(i);
    }


    The answer i was given was 2 but shouldn't it be 3 times?

    2
    4 (2*2)
    8 (4*2)


    Doesn't a loop always begin with the first number?

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

    Default Re: why does this only loop 2 times ?

    Go thru the loop by hand and write the values. Think about i*i.

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

  3. #3
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: why does this only loop 2 times ?

    Quote Originally Posted by jim829 View Post
    Think about i*i.
    How many different values can i have?

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

    Default Re: why does this only loop 2 times ?

    Are you asking me? Actually, the OP should answer that question. And he did his calculations wrong anyway, which is why I suggested he focus on i*i.

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

  5. #5
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    6

    Default Re: why does this only loop 2 times ?

    Quote Originally Posted by jim829 View Post
    Are you asking me?
    Nope, that was a question for the OP. I figured you saw the bug right away.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: why does this only loop 2 times ?

    What was printed when the loop executed?
    What would be the value of i*i at the end of the second loop?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: why does this only loop 2 times ?

    Also posted at: https://coderanch.com/t/675145/java/...s-JAVA#3161603

    Please let everyone know about other sites to prevent wasted effort.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2013, 09:50 AM
  2. Add two times together
    By rover123 in forum New To Java
    Replies: 3
    Last Post: 04-16-2012, 11:21 PM
  3. while loop tries 20 times before giving up
    By JMAsterson in forum New To Java
    Replies: 3
    Last Post: 04-10-2012, 10:02 PM

Posting Permissions

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