• Post Reply Bookmark Topic Watch Topic
  • New Topic

while loop instead of for, if and break  RSS feed

 
Ioannis Michailidis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

a couple of weeks ago, I started reading your book. I reached page 150, i thought this might be a good point trying something on my own.

I want to run through an array until I find a specific element, then print "success", otherwise print "failure". I guess, one option would be:




I`ve been wondering, if there is another, more compact and elegant way, without the use of for, if and break, only using a while loop. I`ve brought it so far...





... which is obviously not enough. Is there a way to tell java to do something (change the string variable to "failure") when the while loop is not early aborted, without the use of any if`s? And

2. by bringing cell++ wheresoever in the parenthesis, either an exception, or an infinite loop is thrown. Is there a way to tell the loop to take the value cell = cell + 1, in the second iteration?

Thanks in advance for your time.

ioannis
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think a while loop is more elegant that the for:each loop?

Is there a way to tell java to do something (change the string variable to "failure") when the while loop is not early aborted...

yes - you use an if-statement
without the use of any if`s

oh...umm...no.

by bringing cell++ wheresoever in the parenthesis, either an exception, or an infinite loop is thrown. Is there a way to tell the loop to take the value cell = cell + 1, in the second iteration?
I don't understand what you mean by "bringing cell++ in the parenthesis". Can you show us in code what you mean?
 
Ioannis Michailidis
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the prompt answer. Goal is to get rid of the cell++ command in the loop, and put it in the right place in the condition, e.g.



 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find the original code easier to read. It's a more standard idiom, and the flow is easier to follow. I don't think your revised code has the same behavior. The original gives success if a single match is found. I think yours will give failure if a single non-match is found. Although, because your code is harder to follow, I may be mistaken there. I didn't bother to really think that hard about it.

Finally, comments like

are just clutter and make the code more difficult to read. They should be removed. If your blocks are so long or so deeply nested that you need those comments, it's probably time to refactor.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioannis Michailidis wrote:Goal is to get rid of the cell++ command in the loop, and put it in the right place in the condition, e.g.

Who says the 'right place' is in the condition? If you were using a for-loop, then yes, it would belong there. But I don't think i have ever seen it in a while loop. That isn't standard practice from what I've seen. IMHO, the while condition should be a test for true/false, and that's it. putting stuff in there (like increment operators) make it harder to understand what your code is doing.
this is easy to understand:

this is not:
 
Omar Perez
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also use while with booleans (true/false);

While(boolean variable or just true){

//code in here
if
condition 1
else if
condition 2
else
boolean variable false or just false.
}

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!