• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to convert a while loop to a for loop?  RSS feed

 
Alvaroo Hernandez
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Would it be something like this?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. And you can confirm by running it and seeing you get the same result for sum.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, technically that for loop won't produce the same result. Jeanne was probably answering based on your deceptive indentation.  The code, when properly formatted (I think I've told you several times now to properly format your code!), actually looks like this:

That code will NOT give the same results as this:
 
rajpal chauhan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to ask question??
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37469
539
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Well, technically that for loop won't produce the same result. Jeanne was probably answering based on your deceptive indentation.

Yup! I also said to try it which would have prompted "wait, why not?"
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!