I!=0 in a function; what does it do and how? I'm trying to check that the elements of an array match

I'm trying to solve a problem with code I found. I modified the code I found into an example I could run but I don't understand what is happening. The purpose of the code is to determine whether all of the elements of an array are identical to each other or whether there are one or more differences. It works, I just don't know how.

When all of the elements are identical to each other, the function returns 0. When they are not all identical, the function returns 1. How does this happen? What is i!=0 doing? i does not equal 0? What? Where is it getting the 1 or 0 from? I understand that the while loop is checking each element against the first element. What I don't understand is what happens next. When I delete i!=0, the function always returns 0.

bool array[30] = { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int n = 0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
Serial.print(check(array, 30));
delay(1000);
}

int check(bool a[], int i) {

while(--i>0 && array[i]==array[0]);
    
return i!=0;
}

The expression returns a logical value (true, or 1, if i is not equal to zero), (false, or 0, otherwise).

1 Like

It's not your fault, you have been punked by someone that has used an integer type to hold a boolean value. It used to be common practice, which is why the habit still persists.

Your other questions can be answered with a "structured walk through" of the code. Take a pencil and paper, and "walk" through the code as if you are the processor, keeping track of all the important values as you go along.

Truthfully, this:

bool array[30] = { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

Should be

bool array[30] = { false, false, false, ....

If all elements are equal then i will equal 0 when exiting the while loop, if not then i will not equal 0 after exiting the while loop.

@bimetallic82

To help me figure out what was going on I mentally replaced the i with a number for each iteration of the while loop, and eventually it made sense.

while(--i>0 && array[i]==array[0]);

First it's important to realize this while loop has 2 conditions that need to valuate to true for the loop to continue. The first condition is --i > 0 and the second condition is array[i] == array[0]. These both need to be true because of the && part, this means the word "and". If a || was there instead it would mean "or". So because we have an "and", if both conditions are true the loop will continue but if either of these tests is false, looping stops and the next line of code, the tricky i != 0 is executed. So let's study each of the two conditions...

The --i part of the first condition subtracts 1 from the variable before any other logic is applied, so when i starts as 30 it will become 29. Then the 29>0 will result in a true. So this part of the loop changes the value of i every time the loop runs and effectively gives us a way to look at each element in the array of 30 items. Due to how it starts at 30, it will look at the last element in the array first and work its way to the start of the array. Once --i evaluates to 0, the test of 0>0 results in a false, and execution of the loop will stop and i will be left as the number 0. Let's pretend that happened and consider the code the executes right after the while loop, i != 0 which is a test to see if i made it down to 0. If i is not equal to 0, it means the array index variable never made it all the way down to 0, so it didn't check every element in the array. There's only 1 reason why it might not make it down to 0, and that is the second condition of the loop.

The second condition array[i]==array[0] "looks up" the element at a position within the array, and on the first iteration of the loop it will look up array[29]. It will then check to see if this element of the array is the same as the very first element of the array, the array[0] part. If the two items are the same, the result will be true and the loop will continue onto its next iteration where the index will be lowered to 28 and then the comparison will happen again. But if the comparison ever results in a false, the loop will stop executing. It will stop executing because the while loop will only continue if both conditions are true. Since the second condition is false, the loop will stop and the value of i will be some number greater than 0, like 28 or 15, it just depends on which element of the array was not equal to the first element. So if the loop stops and i = 15, the next line that tests the value of i, the i != 0 part, results in a way to validate that we moved through every element in the array and they all equaled the value of the first element in the array.

2 Likes

Yes, this is an example of a walk through. Thanks.

Thank you very much. I get it now.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.