• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with while loop into while loop  RSS feed

 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So this is the code and I need to find the value of i .



When I run the code, "i" would be 16. But what I understand is since i = 1, the outer while loop (i <10) would run, then j = 10, and this would cause the inner loop to run ( j > i), "j" would then decrease by 1 which become 9
then "i" is i+j which is 10. Won't the while loop stop at this point since 10 < 10? Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blaze Shadow wrote:since 10 < 10?

10 is not less than 10.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the edit! Yea, what I mean was since 10 isn't < 10 won't the code stop running.
 
Steve Myers
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're missing curly brackets around the inner loop and what you're expecting to happen isn't happening.

is not happening in the inner loop.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would the value of i when the while loop run once? Would "i" be 10?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:What would the value of i when the while loop run once? Would "i" be 10?

Ok, first, that question isn't very clear. you ask about "the while loop", however, you code has TWO while loops. So WHICH while-loop are you asking about running once?

The easiest way to tell what you program is doing is to put in "System.out.println()" statements. Be careful doing that, though, since you don't have curly brackets around the body of the inner while. If you put in the S.O.P between what is currently your lines 5 and 6 without adding them, you kick the j-- out of the body, causing strange and unexpected things to happen. That is why you really want to ALWAYS use the curly brackets, even if they aren't strictly necessary.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about the unclear question. What I mean is if the outer loop run once, would the value of i = 10? ( Hopefully this is clearer)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:Sorry about the unclear question. What I mean is if the outer loop run once, would the value of i = 10? ( Hopefully this is clearer)

What did you find when you put in the System.out.println() statements?
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get 16.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you really understood what was meant by adding print statements. Try running this code and see if you can work out what is happening:
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for that print statement! Could you help me understand why is j=1 for the first run? If I interpret the code correctly, it say j=10 then j--, wouldn't that make j =9?
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:Thank you for that print statement! Could you help me understand why is j=1 for the first run? If I interpret the code correctly, it say j=10 then j--, wouldn't that make j =9?


The first time it runs yes. What do you think happens after j-- runs the first time? What could be happening if the first time you encounter System.out.println("i="+i+", j="+j); is when j=1?
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Won't the first time it run, j = 9 then the next time it run j = 8? since it j--? I can't seem to understand why did j=10 become j=1 after it first run.
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, every time it runs it decreases j by one. By the time you hit your debug statement j=1 so what must have happened?

Different tack. Which logical condition in your code is satisfied (or more importantly not satisfied) when i=1 and j=1 and, for that matter, every other pair of values your debug print returns?
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil English wrote:Different tack. Which logical condition is satisfied (or more importantly not satisfied) when i=1 and j=1 and, for that matter, every other pair of values your debug print returns?


Would it satisfy (i<10)? May I also asked if in this case, a while loop inside a while loop, can this be call "Nested Loops"? Thank you
When i=1 and j=1 I'm thinking it would not satisfy j> i ?
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:When i=1 and j=1 I'm thinking it would not satisfy j> i ?


Jackpot. So here is the big question: You only get to the debug statement when your inner while loop is satisfied - what code do you think is running when your inner loop runs?

I would call these nested loops.

Also, happy GMT Christmas.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
People have mentioned this before, but here's a big hint. Curly brackets or their absence after a while loop statement make a big difference regarding which statements are run inside the loop
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil English wrote:
Jay Mize wrote:When i=1 and j=1 I'm thinking it would not satisfy j> i ?


Jackpot. So here is the big question: You only get to the debug statement when your inner while loop is satisfied - what code do you think is running when your inner loop runs?

I would call these nested loops.

Also, happy GMT Christmas.


I just notice it 12/25 in GMT . Merry Christmas too you too!

What code is the inner loop running when the condition satisfy? Wouldn't it be j--; ?
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:People have mentioned this before, but here's a big hint. Curly brackets or their absence after a while loop statement make a big difference regarding which statements are run inside the loop



This would be for the outer loop while j--; would be for the inner loop right?
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it would hit System.out.println("i="+i+", j="+j); before it hit i+=j so the first iteration would return i=1, j=9 so that can't be the case.

How many times does j-- run before you hit System.out.println("i="+i+", j="+j); for the first time? You are close!
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:
Jayesh A Lalwani wrote:People have mentioned this before, but here's a big hint. Curly brackets or their absence after a while loop statement make a big difference regarding which statements are run inside the loop



This would be for the outer loop while j--; would be for the inner loop right?


Yes. The only thing your inner loop does is run j-- until the while loop (j>i) is satisfied.

If you don't define the extend of a loop using curly braces the compiler assumes that the loop consists of the first statement (roughly speaking - someone better might qualify that!)

Try these two examples:





You'll have to break out of the second one. Ctrl+C or whatever stop command your IDE has.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil English wrote:But it would hit System.out.println("i="+i+", j="+j); before it hit i+=j so the first iteration would return i=1, j=9 so that can't be the case.

How many times does j-- run before you hit System.out.println("i="+i+", j="+j); for the first time? You are close!


Outer loop run first and then the inner right? I didn't know the loop will hit the println before it execute the i+=j. Thank you! Now that I understand this, I'll try to see if I can figure out why the first run is j=1.

 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Does the i--: in this case do anything? I keep getting 16 so I'm guessing it do nothing.
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:
Phil English wrote:But it would hit System.out.println("i="+i+", j="+j); before it hit i+=j so the first iteration would return i=1, j=9 so that can't be the case.

How many times does j-- run before you hit System.out.println("i="+i+", j="+j); for the first time? You are close!


Outer loop run first and then the inner right? I didn't know the loop will hit the println before it execute the i+=j. Thank you! Now that I understand this, I'll try to see if I can figure out why the first run is j=1.



The inner loop will run every time the outer loop runs.
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:

Does the i--: in this case do anything? I keep getting 16 so I'm guessing it do nothing.


Exactly, no it doesn't.

If you declare a loop like this:


The what the compiler sees is while condition is true do everything inside the {}

If you write this


The complier implicitly inserts {} for you but it only ever assumes you want the first statement to be included so it does this:


So all that happens is doThis runs until condition is false then and only then does it do thenThis

So, if you want to have more than one line of code in a loop you have to use {} to tell the compiler where the loop ends
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So while ( j > i ) j--; will make j keep decreasing until j > i?, since i =1 on the first run, j would have to be 2 in order to satisfy (j>i)?
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:So while ( j > i ) j--; will make j keep decreasing until j > i?, since i =1 on the first run, j would have to be 2 in order to satisfy (j>i)?

Exactly the inner loop is just j-- which runs until ( j > i ) is untrue which only happens when j=1 (when i=1 as it does on the first iteration of your outer loop)

Then and only then does it go back to the remainder of the outer loop which is your debug statement and i+=j. On the next iteration j-- runs until j>i because now i=2 this becomes true only when j=2
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil English wrote:
Jay Mize wrote:So while ( j > i ) j--; will make j keep decreasing until j > i?, since i =1 on the first run, j would have to be 2 in order to satisfy (j>i)?

Exactly the inner loop is just j-- which runs until ( j > i ) is untrue which only happens when j=1 (when i=1 as it does on the first iteration of your outer loop)

Then and only then does it go back to the remainder of the outer loop which is your debug statement and i+=j. On the next iteration j-- runs until j>i because now i=2 this becomes true only when j=2


When i = 2, j--; must run until it hit j=3?
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:
Phil English wrote:
Jay Mize wrote:So while ( j > i ) j--; will make j keep decreasing until j > i?, since i =1 on the first run, j would have to be 2 in order to satisfy (j>i)?

Exactly the inner loop is just j-- which runs until ( j > i ) is untrue which only happens when j=1 (when i=1 as it does on the first iteration of your outer loop)

Then and only then does it go back to the remainder of the outer loop which is your debug statement and i+=j. On the next iteration j-- runs until j>i because now i=2 this becomes true only when j=2


When i = 2, j--; must run until it hit j=3?


No, while(j>i) means that the loop will run until j>i is untrue. When i=2 and j=3 then j>i is still true and the loop will run again, rendering j=2. However, once j=2 the condition becomes false and your loop terminates.

I suggest you have a look here. The Java Trail really is excellent for the basics - I've ha mixed results from some other official tutorials but most the content you can navigate to from that page is really excellent.

Anyway, bedtime for me, otherwise Santa won't come apparently. Good luck with it.
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Phil English wrote:
Jay Mize wrote:
Phil English wrote:
Jay Mize wrote:So while ( j > i ) j--; will make j keep decreasing until j > i?, since i =1 on the first run, j would have to be 2 in order to satisfy (j>i)?

Exactly the inner loop is just j-- which runs until ( j > i ) is untrue which only happens when j=1 (when i=1 as it does on the first iteration of your outer loop)

Then and only then does it go back to the remainder of the outer loop which is your debug statement and i+=j. On the next iteration j-- runs until j>i because now i=2 this becomes true only when j=2


When i = 2, j--; must run until it hit j=3?


No, while(j>i) means that the loop will run until j>i is untrue. When i=2 and j=3 then j>i is still true and the loop will run again, rendering j=2. However, once j=2 the condition becomes false and your loop terminates.


Oh, so the inner loop must terminate for the outer loop to get a values for j to run i+=j;?
At i=1, and i=1, the inner loop terminate so it become i+=j , which i=2, then since i<10, it run again when j= 2, it terminate, then i+=j , which i=4. It will run this until i < 10. Am I correct?
 
Jay Mize
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for your time, Phil! I understand it now.
 
Phil English
Ranch Hand
Posts: 62
MySQL Database Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay Mize wrote:
Phil English wrote:
Jay Mize wrote:
Phil English wrote:
Jay Mize wrote:So while ( j > i ) j--; will make j keep decreasing until j > i?, since i =1 on the first run, j would have to be 2 in order to satisfy (j>i)?

Exactly the inner loop is just j-- which runs until ( j > i ) is untrue which only happens when j=1 (when i=1 as it does on the first iteration of your outer loop)

Then and only then does it go back to the remainder of the outer loop which is your debug statement and i+=j. On the next iteration j-- runs until j>i because now i=2 this becomes true only when j=2


When i = 2, j--; must run until it hit j=3?


No, while(j>i) means that the loop will run until j>i is untrue. When i=2 and j=3 then j>i is still true and the loop will run again, rendering j=2. However, once j=2 the condition becomes false and your loop terminates.


Oh, so the inner loop must terminate for the outer loop to get a values for j to run i+=j;?
At i=1, and i=1, the inner loop terminate so it become i+=j , which i=2, then since i<10, it run again when j= 2, it terminate, then i+=j , which i=4. It will run this until i < 10. Am I correct?


Yes. the inner loop runs to completion every time the outer loop runs. The outer loop doesn't 'see' any of the variables in the inner loop until the inner loop completes. Glad you found it useful!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can think of nested loops as being similar to old manual car odometers (note to self: come up with more modern analogy).

The right-most digit must go all the way around before the next-right most one moves a tick, then the right-most one goes all the way around again.

With loops, the inner one must complete before the outer one does it's check and update...so


This should print out 100 numbers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!