Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Add condition for-each loop  RSS feed

 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to add condition to the for-each loop body?
 
Steve De Costa
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean? Can you pleaase be a little more specific about your question? What exactly are you trying to do?

Every loop has a condition to exit....I'm not sure what do you mran by add condition?
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Single condition :


Dual condition:
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One possibility is to add an "if" condition, along with a break, as the fist line of the for-each loop.

However, the for-each loop is just syntactic sugar for the regular for loop. If you need a more elaborate condition, why don't you just use the regular for loop?

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"ZiggyMon", please check your private messages. You can see them by clicking My Private Messages.
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just thought there might be syntax I am not familiar with
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!