I want to write a code wherein I have to compare a number with all the elements of an array. The number can be equal to only one element of an array or not equal to any element. I can compare the number with each element of the array using if statement inside a for loop. Problem is when I want to write "the number is not equal to any of the element of the array".The code shown below will execute the else statement 99 or 100 times, but I need that to be executed only once, after number is compared with all X[i] and not found equal.
1 2 3 4 5 6
for(int i=0;i<100;i++)
{if(number==X[i])
{cout<<"number is equal to one of the numbers in array"<<endl;}
else
{cout<<"number is not equal to any number in the array"<<endl;}}
You can use a bool to store if the value was found. After the loop you check if the value is false and output a message.
BTW The comparison operator is ==