# ternary in the condition of a for loop

• 05-08-2012, 08:35 PM
mikehaas763
ternary in the condition of a for loop
Hi,

I have a method

Code:

```                /*                 * Calculate the number of cards needed per "pile" and then add the                 * PlayingCard from deckOfCards to the cardPiles object                 */                 int baseCardsPerPile = deckOfCards.length / pilesNeeded;                 int remainderForPiles = deckOfCards.length % pilesNeeded;                 for (int i = 0, j = 0; i < pilesNeeded; i++)                 {                         cardPiles[i] = new CardCollection(deckOfCards[j]);                         for (int k = 0; k < baseCardsPerPile + (i < remainderForPiles ? 1 : 0); k++, j++)                         {                                 cardPiles[i].addToBottom(deckOfCards[j]);                         }                         cardPiles[i].setBounds(xpos[i], ypos[i], cardPiles[i].getIcon()                                         .getIconWidth(), cardPiles[i].getIcon().getIconHeight());                         add(cardPiles[i]);                 }```
It is used to split any amount of cards into the specified amount of "piles". I have it set up so it is scalable simply having the array "deckOfCards" to contain cards from multiple decks and then changing the pilesNeeded variable will still split them up into as close to equal sized values as possible. I also have a couple of statements for swing should be able to be ignored.

However, the ternary operator I have in the for condition seems messy to me. It is simply used to distribute the remainder of cards after division if there is a remainder.

How can I do this without having a ternary in the condition? Would I somehow nest the inner for loop in an if condition?

Thanks,
Mike