# increase while loop && var

• December 9th, 2010, 11:47 AM
Scotty
increase while loop && var
Code :

```while ((x<y) && ((red[0][x] != target) &&( red[1][x] != target) && (red[2][x] != target) && (red[3][x] != target) && (red[4][x] != target) && (red[5][x] != target) && (red[6][x]!= target) && (red[7][x]!= target) && (red[8][x] != target) && (red[9][x] != target))){ x++;```

The problem is red is a variable and i need to check all the values in the red row. i dont know red will have 10 vaules.

any ideas? As im in a while loop i cant use a for loop here. x increases each time to check the columns, which works fine.
• December 9th, 2010, 11:52 AM
KevinWorkman
Re: increase while loop && var
Quote:

Originally Posted by Scotty
As im in a while loop i cant use a for loop here.

Why not? What happened when you tried?
• December 9th, 2010, 11:53 AM
copeg
Re: increase while loop && var
Break the problem down into pieces...you want to check several array values using logic, so to you cold just create a method that checks the array for you, returning the logic boolean your while loop needs.
• December 9th, 2010, 12:36 PM
javapenguin
Re: increase while loop && var
while ((x<y) &&
((red[0][x] != target) &&( red[1][x] != target) && (red[2][x] != target)
&& (red[3][x] != target) && (red[4][x] != target) && (red[5][x] != target) &&
(red[6][x]!= target) && (red[7][x]!= target) && (red[8][x] != target) &&
(red[9][x] != target))){
x++;

You could always check like this
Have another variable for the 0-9.
int z =0;

while ((x<y) && (red[z][x]!=target) && (z < red.length) &&(x < red[z].length))
{
x++;
z++;
}
• December 9th, 2010, 12:45 PM
KevinWorkman
Re: increase while loop && var
Quote:

Originally Posted by javapenguin
while ((x<y) &&
((red[0][x] != target) &&( red[1][x] != target) && (red[2][x] != target)
&& (red[3][x] != target) && (red[4][x] != target) && (red[5][x] != target) &&
(red[6][x]!= target) && (red[7][x]!= target) && (red[8][x] != target) &&
(red[9][x] != target))){
x++;

You could always check like this
Have another variable for the 0-9.
int z =0;

while ((x<y) && (red[z][x]!=target) && (z < red.length) &&(x < red[z].length))
{
x++;
z++;
}

Why.

Do you love giving incorrect answers? Did you test that at all?
• December 9th, 2010, 12:57 PM
Scotty
Re: increase while loop && var
My problem is I dont see how to increase/decrease 0-9 because of the &&. Using a for could increase or decrease the number but not add or take away a term from the while loop.
• December 9th, 2010, 01:02 PM
javapenguin
Re: increase while loop && var
You could always use a for loop.
• December 9th, 2010, 01:09 PM
javapenguin
Re: increase while loop && var
It depends, if the while loop is supposed to end if just one of those conditions is false, then use a boolean

boolean works = true;

if in your for loop, a value equals target, you change works to false and then it'll exit the while loop.
• December 9th, 2010, 02:03 PM
Scotty
Re: increase while loop && var
for loops dont remember the vars...... ie i how do i keep track of the position when it exits the loop?
• December 9th, 2010, 02:21 PM
KevinWorkman
Re: increase while loop && var
Quote:

Originally Posted by Scotty
for loops dont remember the vars...... ie i how do i keep track of the position when it exits the loop?

Code :

```public class ForLoopTest { public static void main(String[] args) { int i; for(i = 0; i < 10; i++){ //do something } System.out.println(i); } }```
• December 9th, 2010, 02:29 PM
Scotty
Re: increase while loop && var
strange, maybe its me, but my i always resets to 0..... even int i = 0; delcared outside....
• December 9th, 2010, 02:32 PM
KevinWorkman
Re: increase while loop && var
Quote:

Originally Posted by Scotty
strange, maybe its me, but my i always resets to 0..... even int i = 0; delcared outside....

Can you post the code, in SSCCE form, that does that? It works just fine for me.