Conditional statement in question C

I just have a quick question about a conditional statement. Still a novice programmer. I am given x = 1, y = 2 and z = 3.

I want to know why after this statement:

y += x-- ? z++ : --z;

This y is 5. The values ​​after the statement are x = 0, y = 5, and z = 4. I know how the conditional operator works, so it is formatted as follows: variable = condition? value if true: if if false.

For the condition y + = x--, how does y become 5? I see only 2 (2 + = 0) and 3 (2 + = 1) (then x - becomes zero) as an opportunity. Any help is much appreciated.:)

+3
source share
13 answers

(x!= 0), x 1 ( not 0). z++. 3. 2 + 3 = 5. x 0, z 4.

. : x ++, x, , . ++x, , .

+10

?: , + =. ,

y += (x-- ? z++ : --z);

x-- ? z++ : --z z++ ( 3), x-- 1

+7

if:

if (x--)
    y += z++;
else
    y += --z;

, x - 1, "" if. , z++ y, 3 + 2, 5.

, .

+6

, , , !

+1

, -/ (x++ x--) :

  • .

, x-- 1, true, z++, 3.

y = 2, y += 3 5.

0

x-- , x, x 1. Z ++. - -z, , z.

, x 1, z 3. x 0 z 4; y = 2 + 3 = 5

0

, increment and decment , .

, x--, x 1, x, 1. C 1 true, z++.

, ++ , z++ z, 3.

, y += 3, y 5.

0

x-- z ++ , . :

y + = (1)? (3): (-z);

- z , true . x , z .

0

, "" , , (x--) , z ++, 3, 5 y.

0

x-- x, 1. , z++, 3. 3 y, 5.

0

, , , y+= x-- - , - x--. , y += : x-- ? z++ : --z; 5. , 5.

0

y + = (x--? z ++: -z); , ................

, - X- x ++ post increment decment. , . , .....

:

Y + = X -? Z ++: - Z.... : , ... : " , , ... , "

: Y + = X? Z: Z;.... , - ..... post ++/- der der first ...... den go for ++/--.....

... ... i.e

y + = 1: 3: 3// (.. 1)

, , , , x, 0....

Y + = Z. (\\, , , .)

, Z post ++/- () pre ++/-)... hahh..its post increment.. 2, Z....

                     Y+=Z =>Y=Y+Z
                    =>Y=2+3 =>y=5

i.e Y = 5, Z 4

0

, , true i.e 1 true.

false i.e 0,

Since we initialize the value x to 1, it executes the statementtrue

As a result, it deduces the result 5from the true part.y=(y+z++)

0
source

Source: https://habr.com/ru/post/1746245/


All Articles