简体   繁体   中英

C: How does “if((a+=b) > c)” work?

I am trying to rewrite some C code from another author into Igor Pro (similarish notation to C). The code is available here .

I don't know how to deal with the lines if ((umin+=input[k+1]-vmin)<minlambda) and else if ((umax+=input[k+1]-vmax)>lambda) with regards to the order of how they go about updating umin and umax , and how the if/elseif statements evaluating to true or false affect the update..

Specifically:

On lines 99-107 there is:

        if ((umin+=input[k+1]-vmin)<minlambda) {        
            do output[k0++]=vmin; while (k0<=kminus);
            vmax=(vmin=input[kplus=kminus=k=k0])+twolambda;
            umin=lambda; umax=minlambda;
        } else if ((umax+=input[k+1]-vmax)>lambda) {    
            do output[k0++]=vmax; while (k0<=kplus);
            vmin=(vmax=input[kplus=kminus=k=k0])-twolambda;
            umin=lambda; umax=minlambda;
        } else { /*blah blah */ }

I have refactored this to read:

    if ((umin+=input[k+1]-vmin) < minlambda) //Todo
        do
            output[k0] = vmin
            k0+=1
        while(k0 <= kminus)

        k=k0
        kminus=k
        kplus=kminus
        vmin=input[kplus]
        vmax = (vmin) + twolambda
        umin = lambda
        umax = minlambda

    elseif ((umax+=input[k+1]-vmax) > lambda) //Todo
        do
            output[k0]=vmax
            k0+=1
        while(k0 <= kplus)

        k=k0
        kminus=k
        kplus=kminus
        vmax=input[kplus]
        vmin = (vmax) - twolambda
        umin = lambda
        umax = minlambda        
    else //blah blah

Do umin and umax only get updated if their if statements evaluate to true? Or does it cascade? IF(umin) -> false, umin updated, ELSEIF(umax) -> true, umax updated, but IF(umin) -> true, umin updated, umax not updated? Or some other variant?

Another question about the same code.

Edit: fixed title. Added igor tag

if( a += b > c)

In this first b>c is evaluated as > has higher precedence than += .

Then += will be evaluated. Now , if b>c is true then a will be a+=1 and if it is false then a+=0 will be evaluated .

Now this ( as you updated your title )-

 if ((umin+=input[k+1]-vmin)<minlambda)  

In this first (umin+=input[k+1]-vmin) will be evaluated .Why ? due to brackets () having higher precedence than < .

In (umin+=input[k+1]-vmin) , due to precedence of - is higher than += . input[k+1]-vmin is evaluated and then its result is added to umin and stored in umin .

After this evaluation it is compared with minlamda .

Similarly you can understand how this will work ( ofcourse if condition in if is false ) -

else if ((umax+=input[k+1]-vmax)>lambda) 

here also umax will be updated and then it will be compared with lambda .

umin will be updated every time you enter there.
umax will be updated by (umax+=input[k+1]-vmax) > lambda if and only if (umin+=input[k+1]-vmin) < minlambda is false because it is in else if

a+=b > c works as if(b>c)a+=1; else a+=0; if(b>c)a+=1; else a+=0;

(a+=b)>c works as (a+=b),a>c , which returns a>c after adding b to a .

This:

for (;;) {
....
  if ((umin+=input[k+1]-vmin)<minlambda) {        
        do output[k0++]=vmin; while (k0<=kminus);
        vmax=(vmin=input[kplus=kminus=k=k0])+twolambda;
        umin=lambda; umax=minlambda;
    } else if ((umax+=input[k+1]-vmax)>lambda) {    
        do output[k0++]=vmax; while (k0<=kplus);
        vmin=(vmax=input[kplus=kminus=k=k0])-twolambda;
        umin=lambda; umax=minlambda;
    } else { /*blah blah */ }
}

(from the original source) is more or less equivalent to this

for (;;)
{
    ...
    umin += input[k + 1] - vmin;
    if (umin < minlambda) 
    {
        ...
        continue;
    }
    umax += input[k + 1] - vmax;
    if (umax > lambda)
    {
         ....
        continue;
    }
    /* blah blah */
}

You can do this because the if block is at the end of a loop, otherwise you'd need some else 's and extra indenting which would be moderately less pretty (but probably still easier to understand).

You can look at it this way:

((umin+=input[k+1]-vmin)<minlambda)

is essentially

var umin += input[k+1] - vmin;
if (umin < minlambda) { }
else if ((umax += input[k+1] - vmax)> lambda) { }

umin is just calculated inside the if statement and then compared to minlambda

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM