• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can a for loop be nested inside an if?  RSS feed

 
Richard Warner
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what will probably turn out to be a silly "Why doesn't this work?" question. It concerns a small method which is part of a card game.

I'm trying to check a condition from a section of an array, without a predetermined number of times any given call to this method will require a check. How much of the array I'm checking could vary greatly.

Is it at all possible to nest a for loop yielding a variable number of boolean && conditions inside an if? Am I just missing the right bracketing or is this nesting (if that's the word) even possible in the language?

To clarify, below is broken code. Compiler isn't letting me accomplish this goal as I envision it.

If I need to approach this differently, that's great, just so I know I am in fact, barking up the wrong tree and not just missing a few brackets somewhere.

Thanks!

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a bit hard to understand what exactly you want to do.

If you want to check some condition on elements of an array, then do the loop outside of the if-statement, and then put the if-statement below it.

Here is some code to give you an idea.

 
Richard Warner
Greenhorn
Posts: 16
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Essentially, turning inside out which is nested in which?

I think that would work! Putting the ifs inside the for should work, though I do have two fors at the moment, I won't once they're outside the first if.

Thank you so much! That should solve it! And you're super quick!!!
 
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

Agree with Jesper. The "if" statement expects a conditional expression, and you cannot put another "if" or a "for" statement into a condition expression. Reversing the logic (actually, redesigning the logic), by having a boolean variable that is calculated, and by having the "if" inside the block of the "for", is a possible alternative.

Other options that may help ... (1) You can use the Ternary operator, which is good for simulating the placing of "if" conditions within "if" conditions, but obviously, not all cases. (2) You can break it into methods, that passes boolean (partial) results, but even with this, this will still likely need a boolean variable.

Henry



 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just a style tip...

never do this:



Just do this:



it is simpler and cleaner. More importantly, it is less likely to introduce bugs. Granted, when you do a method call, the possibility of error are often reduced...but if you have a variable, it is easy to write this by mistake:

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