confused
confused

Reputation: 121

Changing the if statement

I was playing with my code and started wondering if this change from:

if(array[index] == 0)

to this:

if(!array[index] != 0)

could affect any code or does it just do the same thing and I don't need to worry about how I write this?

Upvotes: 2

Views: 77

Answers (5)

mcleod_ideafix
mcleod_ideafix

Reputation: 11438

I've just checked a simple function to see what happens:

int matrix (int *array, int n)
{
  int c = 0;
  int index = 0;

  for (index=0;index<n;index++)
    if (array[index]==0)
      c++;

  return c;
}

BTW: you don't specify what type of elements array has. I assume they are ints, but if they are floats or doubles, that might change the following argument:

No matter what I change the if line to. I've tried:

if (array[index]==0)

if (!(array[index]!=0))

if (!array[index])

After compiling this with:

    gcc -O0 -c -S -m32 ifs.c

The three options generate the same assembly code:

        movl    (%eax), %eax
        testl   %eax, %eax
        jne     .L3
        addl    $1, -4(%ebp)
.L3:
        addl    $1, -8(%ebp)

If I enable optimizations:

    gcc -O3 -c -S -m32 ifs.c

The three versions generate this code:

        cmpl    $0, (%edx)
        leal    1(%eax), %ecx
        jne     .L4
        movl    %ecx, %eax
.L4:
        addl    $4, %edx

So it doesn't really matter how you write it; the compiler will infer the same meaning and generate the same code, so write it the most legible way to ease maintentace.


For floating point numbers, there is indeed a difference between the code compiled with -O0 and -O3. For floating point numbers some options exists, as code for SSE2 can be generated instead of code for the math coprocessor.

Upvotes: 1

Jeff Mercado
Jeff Mercado

Reputation: 134591

Consider some truth tables, assuming array is an array of integers:

array[index] | array[index] == 0 | !array[index] | !array[index] != 0
0            | T                 | 1             | T
1            | F                 | 0             | F
non-zero     | F                 | 0             | F

Both expressions are logically equivalent so either one should work as you might expect.

But, you should write it in the first form. You clearly knew the first form works. Anyone who would see it would understand it works. But the second, you had your doubts because it wasn't clear. It's unreadable. You should always go for what is clear over what isn't. Don't try to be clever with things that should be simple.

Upvotes: 2

Javia1492
Javia1492

Reputation: 892

They are different. If you're trying to change the == to using !, then it would be (array[index] !=0) where you check the condition when the value at the index is not equal to 0.

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172618

They are different. If you want to have it as like if(array[index] == 0) then you need if(!(array[index] != 0))

Upvotes: 1

haccks
haccks

Reputation: 106102

Both are different conditions. ! has higher precedence than == therefore, !array[index] != 0 is equivalent to (!array[index]) != 0.
Equivalent to array[index] == 0 is !(array[index] != 0).

Upvotes: 0

Related Questions