• Post Reply Bookmark Topic Watch Topic
  • New Topic

Whileloop question  RSS feed

 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there folks!

Im really hopeless on figuring out how this loop-printing works.
If I put s.length > 2, it prints 105
If I put s.length > 4, it prints 105105
If I put s.length > 6, it prints 105105105
But then when I put >8 it prints the same 105105105 as with >6 ?

So basicly, Im not sure how the count/prints works. Does it print the first 105 before the count starts? Does it print an extra 105 after the loop brakes? Does it also count the (s = "") as a value,like; (s+(10) +(5)) = 3 ?
What Im asking for is a detalied step by step explanation line by line.
Would so much appreciate some help.









 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


s is set to a new string composed of the old string concatenated with the int 10 converted to a string and 5 likewise converted.



timeToBreak is set to true if the length of s is greater than 10.

To understand what happens, write down on a piece of paper what the value of s.length() is at each iteration.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ungerfält wrote:
If I put s.length > 2, it prints 105


That is because three characters (the smallest possible value) is greater than two.

Daniel Ungerfält wrote:
If I put s.length > 4, it prints 105105


That is because six characters (the smallest possible value) is greater than four.

Daniel Ungerfält wrote:
If I put s.length > 6, it prints 105105105


That is because nine characters (the smallest possible value) is greater than six.

Daniel Ungerfält wrote:
But then when I put >8 it prints the same 105105105 as with >6 ?


That is because nine characters (the smallest possible value) is greater than eight.

Henry
 
Stijn Rensen
Ranch Hand
Posts: 48
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to see what the variables have stored during the execution is to add some extra println in your script to keep track.

the Do while loop is a loop that will always execute at least once. As it checks it's conditions at the end of the loop. So this happens:

Example s.length > 2
* Execute what is in the loop s becomes "105"
* Check condition: s.length > 2. This returns true as s.length == 3
* so exit loop
* print s: "105"

Example s.length > 4
* Execute what is in the loop s becomes "105"
* Check condition: s.length > 4. This returns false as s.length == 3
* so run loop again
* Execute what is in the loop s becomes "105105"
* Check condition: s.length > 4. This returns true as s.length == 6
* so exit loop
* print s: "105105"

Example s.length > 6
* Execute what is in the loop s becomes "105"
* Check condition: s.length > 6. This returns false as s.length == 3
* so run loop again
* Execute what is in the loop s becomes "105105"
* Check condition: s.length > 6. This returns false as s.length == 6 (6 is the same as 6 and not bigger than 6)
* so run loop again
* Execute what is in the loop s becomes "105105105"
* Check condition: s.length > 6. This returns true as s.length == 9
* so exit loop
* print s: "105105105"

Example s.length > 8
* Execute what is in the loop s becomes "105"
* Check condition: s.length > 8. This returns false as s.length == 3
* so run loop again
* Execute what is in the loop s becomes "105105"
* Check condition: s.length > 8. This returns false as s.length == 6
* so run loop again
* Execute what is in the loop s becomes "105105105"
* Check condition: s.length > 8. This returns true as s.length == 9
* so exit loop
* print s: "105105105"
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!