• Post Reply Bookmark Topic Watch Topic
  • New Topic

Conversion from while to for loop  RSS feed

 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone. I just started to teach myself loops. I am trying to convert the while loop into for loops, however somehow I do not get the same printout. Can somebody please explain reason behind different prints.

much appreciated!


While loop:


Print:
sum is 1 and i is 2
sum is 3 and i is 3
sum is 6 and i is 4
sum is 10 and i is 5





Print:
sum is 0 and i is 1
sum is 1 and i is 2
sum is 3 and i is 3
sum is 6 and i is 4


 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just made me physically wince with that for loop. Having two variables in the for loop controls is very bad practice and should never be done.

To answer your specific question though, in your while loop you are changing the values of i and sum before you print out their values, but the for loop will change their values at the end of the loop block which is after you print them out.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In while loop you are first incrementing than printing i value but in for loop first printing than it is incrementing...
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:You just made me physically wince with that for loop. Having two variables in the for loop controls is very bad practice and should never be done.

To answer your specific question though, in your while loop you are changing the values of i and sum before you print out their values, but the for loop will change their values at the end of the loop block which is after you print them out.


Well the while loop is written down in a book and i need to rewrite it to for loop.

Thank you both for help.
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What book is that? QuoteYourSources
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:You just made me physically wince with that for loop. Having two variables in the for loop controls is very bad practice and should never be done.

'Fraid we'll have to disagree there. Funnily enough, I looked at that for loop and thought: "that's exactly how I would have done it" (mistake excluded).

If sum isn't needed outside the loop, why define it outside?

@Lovro: Everything else that Tim (and Tushar) said though is absolutely correct. Your basic problem is that your for loop is not doing things in the same sequence as your while loop (which I would say is actually wrong too; at least if it's intended to print out triangular numbers).

Winston
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:What book is that? QuoteYourSources


Introduction to Java Programming; comprehensive Version, 10e, by Daniel Lang

Winston Gutkowski wrote: @Lovro: Everything else that Tim (and Tushar) said though is absolutely correct. Your basic problem is that your for loop is not doing things in the same sequence as your while loop (which I would say is actually wrong too; at least if it's intended to print out triangular numbers).



I i will play around a bit and try to find out how to rewrite the for loop. I just finished the whole chapter on loops and now its exercise time

thanks Winston
 
Lovro Bajc
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: If sum isn't needed outside the loop, why define it outside?


I have tried to initiate sum outside the while loop but it does not allow me. Bellow is my correction in both loops to get the same result.





 
emeka malachy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one please help and explain the code one by one am confuse about the print out having problem about the math hope some one will help

Sum is 0 and I 1
Sum is 1 and I 2
Sum is 3 and I 3
Sum is 6 and I 4
Sum is 10 and I 5
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
emeka malachy wrote:Can any one please help and explain the code one by one am confuse about the print out having problem about the math hope some one will help


Not sure what explanation is needed. The third part of a for-loop is for re-initialization... ie. it is for code that executes after the body of the loop. In this topic, the OP took code from the beginning of the body and put it into the re-initialization. Of course, it is not going to work exactly -- and going to need some work to get it to work the same.

Henry
 
Ngml Bishop
Greenhorn
Posts: 16
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
taking the while loop as a example (look at lovro's last post):
both variables, i & sum , are initialized to 0.
since sum += i; comes first and then i++, in the first iteration:
sum += i ===>> 0 + 0
so sum is 0 , after this line the loop will perform i++ which makes the i = 1 , and the current iteration ends after the print
so in one iteration of a loop the result is sum = 0 , i = 1
___
in second iteration just use the result of the last iteration as the initialized values in your mind or on paper.
 
emeka malachy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks bishop I totaly understand don't mind my novice mind
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!