# Checking value against array

Hi i am trying to check a value against an array and it does not seem to be working, It is actually only detecting the second number in the array, is there some other way of going about this? I would really appreciate any help, Thankyou!

``````int num[3] = {10, 40, 70};

void setup(){

}

void loop(){

for (int i = 0; i < 2; i++)
if( num[i] == 10 ){

// do something

}
else{

// do this

}
``````
``````int num[3] = {10, 40, 70};
``````
``````for (int i = 0; i < 2; i++)
``````

How many elements are there in the array ?

UKHeliBob:

``````int num[3] = {10, 40, 70};
``````
``````for (int i = 0; i < 2; i++)
``````

How many elements are there in the array ?

Yeah your right sorry about that but changing the 2 in the for loop to 3 does not fix the problem, i have tried it with 2 elements and more than three does not work

does not fix the problem

Can you please explain what the problem is? Your conditional test can indeed match an array element to its value.

I think the problem may be in the //do something code rather than the conditional test.

cattledog:
Can you please explain what the problem is? Your conditional test can indeed match an array element to its value.

I think the problem may be in the //do something code rather than the conditional test.

Yes i just figured out the problem i am having. it's actually that if it finds the number is in the array it does the true code but still does the false code twice because they are not equal to the number i am looking for.So is there a way of not activating the else statement until no number in the array is equal to the number i am looking for?

pempi78:
So is there a way of not activating the else statement until no number in the array is equal to the number i am looking for?

Your phrasing is awkward but I'll give it a shot. First, delete the `[color=blue]else[/color]`. Before starting the loop set (to 1) a flag , using a bool , called `[color=blue]notFound[/color]`.
If, somewhere in the loop a match is found, execute the desired code and clear (to 0) `[color=blue]notFound[/color]`.

After the loop finishes check `[color=blue]notFound[/color]`. If it's true that means there were no matches. Take action accordingly.