6

I don't quite understand how the if statement in this case works. It evaluates the x != 0 statement and when that is not true anymore, it assigns z to y and then breaks the if statement?

int main()
{
    int x, y, z, i;
    x = 3;
    y = 2;
    z = 3;

    for (i = 0; i < 10; i++) {
        if ((x) || (y = z)) {
            x--;
            z--;
        } else {
            break;
        }
    }

    printf("%d %d %d", x, y, z);
}
MD XF
  • 7,860
  • 7
  • 40
  • 71
Mike B.
  • 143
  • 1
  • 5
  • 6
    im not fluid in C but i believe it assigns `z` to `y` and then checks if `y` is true. – Nils Lindemann Mar 08 '17 at 19:20
  • 5
    `y = z` is assignment not comparison! – πάντα ῥεῖ Mar 08 '17 at 19:20
  • 5
    If `x` is non-zero, it doesn't do the `y = z` part, and `if` body executes. When `x` *is* zero, it does the `y = z` and if `z` was non-zero, the `if` body executes. The `break` only happens when both `x` and `z` are zero. – Dmitri Mar 08 '17 at 19:22
  • Yes, I know. This code is from a book, and I'm trying to understand it. – Mike B. Mar 08 '17 at 19:23
  • Thank you Dimitri, It's all clear now. – Mike B. Mar 08 '17 at 19:24
  • 13
    If your book _really_ has `void main()` time to throw out that book. – Chad Mar 08 '17 at 19:24
  • @Chad why is it bad to have `void main()` ? – Tony Tannous Mar 08 '17 at 19:30
  • 12
    @TonyTannous Because main **always** returns an `int`. – NathanOliver Mar 08 '17 at 19:31
  • I think this example only exists to throw off the javascript kids. – Filburt Mar 08 '17 at 19:32
  • `||` is a conditional `or` it will execute the statement on the right only if the first condition is false. – noel zubin Mar 08 '17 at 19:32
  • 1
    @TonyTannous `main` is the one C function that does not *have to* explicitly return a value. Please read [What should main() return in C and C++?](http://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – Weather Vane Mar 08 '17 at 19:33
  • @WeatherVane, Chad this is why I love being on StackOverflow. I learn everyday things I didn't know. Thanks guys! – Tony Tannous Mar 08 '17 at 19:36
  • @TonyTannous me too: Weather Vane, aged 105 3/4 .... if `main` returns at all, which in embedded it won't if there is nowhere to return *to*. – Weather Vane Mar 08 '17 at 19:40
  • Is this what we call short-circuiting of C? – Aesthetic Mar 09 '17 at 07:08
  • @SouravGhosh: To be fair, debuggers are not necessarily that great at showing the inner workings of single expressions. – Matthieu M. Mar 09 '17 at 08:23
  • @MatthieuM. Maybe you're right but that is not a reason to stop me or anybody from trying at least. :) For further confusions, we are anyways here. – Sourav Ghosh Mar 09 '17 at 08:39
  • @SouravGhosh: I agree, it's always better to try to debug first, but in this particular case I'm afraid it won't be very helpful. – Matthieu M. Mar 09 '17 at 08:52
  • 1
    @NathanOliver C99 5.1.2.2.1 "It shall be defined with a return type of int ... or in some other implementation-defined manner." The book may be for some implementation that defines main to return void? – Vality Mar 10 '17 at 06:20
  • @NathanOliver In addition "In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined." It is also possible the book regards programming for some embedded freestanding environment. – Vality Mar 10 '17 at 06:28

6 Answers6

30

Let's decompose that into smaller bits.

  1. if (x) is the same as if (x != 0). If x != 0, then you know the condition is true, so you don't do the other portion of the if.

  2. If part 1. was false, then y = z assigns z into y and returns the final value of y.

  3. From point 2., we can understand that if (y = z) is equivalent to y = z; if (y != 0)


Thus, from points 1. and 3., we can understand that :

if ((x) || (y = z)) {
    doSomething();
}
else {
    doSomethingElse();
}

Is the same as :

if (x != 0) {
    doSomething();
}
else {
    y = z;
    if (y != 0) {
        doSomething();
    }
    else {
        doSomethingElse();
    }
}

It's true it's not particularly readable code though.

adentinger
  • 1,367
  • 1
  • 14
  • 30
18

No. if ((x) || (y = z)) { in C-English is basically:

  • if x is nonzero, evaluate the following code.
  • if x is zero, set y to z.
  • if y is nonzero, evaluate the following code.
  • otherwise, break out of the loop.

If x is zero or y is zero, it breaks out of the loop.

MD XF
  • 7,860
  • 7
  • 40
  • 71
7
int main()
{
    int x = 3;
    int y = 2;
    int z = 3;
    unsigned int i;

    for (i = 0; i < 10; i++)
        if (x != 0) {
            x = x-1;
            z = z-1;
        } 
        else {
            y = z;

            if (y != 0) {
                x = x-1;
                z = z-1;
            }
            else {
                break;
            }
        }
    }
    printf("%d %d %d", x, y, z);
}
Adrian Bobrowski
  • 2,681
  • 1
  • 16
  • 26
3

In C, there is short-circuiting, so the statement y=z will not be evaluated until x becomes zero.

When x == 0, since z also decrements the same way, z == 0. Hence y will also be zero at that time due to the assignment. The statement y=z also returns y at this point which will be evaluated as a condition, and since that is also 0, the else break will be hit.

Hence I believe the answer should be 0 0 0.

user1952500
  • 6,611
  • 3
  • 24
  • 37
1

When you use assignment in an if statement, the result of the assignment is returned. so when you write :

if (x = y)

It will be always true unless the value of y is 0, so 0 is returned as the result of assigning and the if statement is not executed.(anything except 0 is considered as true.)

So when you write :

if ( x || (x = y))

The if statement doesn't execute only if x is 0 & y is 0.

Fatemeh Karimi
  • 914
  • 2
  • 16
  • 23
0

Here

if ((x) || (y = z))

there are two condition one condition is if ((x)) and another condition is if ((y = z)) if one of them is true then if portion is execute otherwise else condition work

  • only and only when both condition are false then else execute.
anis programmer
  • 989
  • 1
  • 10
  • 25