Closed Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Confused about expression evaluation

  1. #1
    n00bcoder is offline Newbie
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Red face Confused about expression evaluation

    In school we're to give a test in a couple of days where the questions are mainly gonna be on Expression evaluation stuff.

    I'm particularly confused about how some of the expressions get evaluated.

    This one for example:

    Code:
    int i = -3, j = 2, k = 0
    
    m = ++i && ++j || ++k;
    when i, j, k and m are printed, the output is -2, 3, 0, 1 respectively.

    Now my question is why doesn't K get incremented?

    and how on earth are such expressions evaluated? In what order etc.

    Please be kind enough to explain this to me.
    thanks

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Naeth's Avatar
    Naeth is offline Newbie
    Join Date
    Oct 2009
    Location
    Manchester
    Posts
    5
    Rep Power
    0

    Re: Confused about expression evaluation

    It runs through ++i && ++j which is obviously true so it never gets to the or?

  4. #3
    n00bcoder is offline Newbie
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Re: Confused about expression evaluation

    ^ ++i would be -2 and ++ j would be 3 {right?}

    then can you please tell me how -2&&3 is evaluated? {bitwise?}

  5. #4
    R3.RyozKidz Guest

    Re: Confused about expression evaluation

    shouldn't the( m = ++i && ++j || ++k;) ===>( m == ++i && ++j || ++k;)
    Last edited by TkTech; 10-18-2009 at 05:48 AM. Reason: Smilies

  6. #5
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Confused about expression evaluation

    Code:
    int i = -3, j = 2, k = 0
    
    m = ++i && ++j || ++k;
    What you're running into is lazy evaluation of logical operators.
    ++i increments i and returns -2
    && interprets that left-hand value as true, and requires the right-hand value to return a value so...
    ++j increments j and returns 3
    && interprets 3 as true. true and true is true
    || takes the result of true, and doesn't need to process ++k to know that it will return true (or 1) to m. As a result, ++k never gets processed.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    n00bcoder is offline Newbie
    Join Date
    Oct 2009
    Posts
    8
    Rep Power
    0

    Re: Confused about expression evaluation

    ^ is it that simple?

    I was thinking of all the precedence involved and if there would be any issues in the evaluation of {++i && ++J} because of i's value {-2}.

    Btw, in case the situation were to be reversed:

    Code:
    int i = -3, j = 2, k = 0
    m = ++i || ++j  && ++k;
    then the value of i would be -2, j =3 and k = 1. is that correct?

    {sorry for being such a nag... i'm only trying to get hold of the basics}

  8. #7
    TheSourceOfX is offline Learning Programmer
    Join Date
    Oct 2009
    Posts
    45
    Rep Power
    0

    Re: Confused about expression evaluation

    && always treats non-zero numbers as true. Also WingedPanther is correct about the lazy evaluation. Conditional statements always evaluate lazily when they can.

    However, you made a mistake in evaluating the reverse of the equation.
    Code:
    m = ++i || ++j  && ++k;
    && has higher order than || so it is evaluated first, which means that ++i would never happen. In other words
    Code:
    m = ++i || ++j  && ++k;
    and
    Code:
    m = ++i || (++j  && ++k);
    are the same thing. Since the second part is true, because ++k and ++j are both non-zero, then the || operation receives true for one of it's inputs, and ignores the other one.

    -TheSourceOfX

  9. #8
    TheSourceOfX is offline Learning Programmer
    Join Date
    Oct 2009
    Posts
    45
    Rep Power
    0

    Re: Confused about expression evaluation

    I'm sorry, but I made a mistake. Your answer to the reversed equation was correct. The ++i does get evaluated. Lazy evaluation only works if the first input satisfies the operation.
    Code:
    i = 1;
    j = 1;
    m = ++i || true;
    n = true || ++j;
    After this code i=2 but j=1 because of lazy evaluation. Even though the second parameter in the first equation is "true" it goes from left to right, so ++i is still evaluated. The same goes for &&, just with false instead of true.

    Even though I made that mistake, however, you still need to keep in mind order of operations as I said before. && has a higher order than ||, so it happens first if there aren't any parentheses.

    -TheSource

  10. #9
    R3.RyozKidz Guest

    Re: Confused about expression evaluation

    Quote Originally Posted by TheSourceOfX View Post
    I'm sorry, but I made a mistake. Your answer to the reversed equation was correct. The ++i does get evaluated. Lazy evaluation only works if the first input satisfies the operation.
    Code:
    i = 1;
    j = 1;
    m = ++i || true;
    n = true || ++j;
    After this code i=2 but j=1 because of lazy evaluation. Even though the second parameter in the first equation is "true" it goes from left to right, so ++i is still evaluated. The same goes for &&, just with false instead of true.

    Even though I made that mistake, however, you still need to keep in mind order of operations as I said before. && has a higher order than ||, so it happens first if there aren't any parentheses.

    -TheSource
    so the value in m is 2 and n is 1?

  11. #10
    TheSourceOfX is offline Learning Programmer
    Join Date
    Oct 2009
    Posts
    45
    Rep Power
    0

    Re: Confused about expression evaluation

    Quote Originally Posted by R3.RyozKidz View Post
    so the value in m is 2 and n is 1?
    Actually the value of both is 1 since true=1 and false=0 and both of the OR operations take in at least 1 value of "true". However i=2 and j=1 after all 4 lines are executed. That is because of the lazy evaluation that ignores the ++j statement because the || operation already received one value of true, and that's all it needs to output true. You can think of || and && as functions such that

    Code:
    bool || (bool a, bool b){
        if(a == true){
            return true;
        }
        if(b == true){
            return true;
        }
        return false;
    }
    
    bool && (bool a, bool b){
        if(a == false){
            return false;
        }
        if(b == false){
            return false;
        }
        return true;
    }
    The code above illustrates exactly the type of lazy evaluation that is done for both the && and || operations. The only thing not represented in the code is that && is evaluated before || in order of operations just like multiplication is evaluated before addition.

    -TheSourceOfX

Closed Thread
Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Expression Evaluation:Urgent help needed
    By avm08 in forum C# Programming
    Replies: 4
    Last Post: 12-01-2010, 02:39 PM
  2. if expression
    By Bertan in forum C and C++
    Replies: 1
    Last Post: 10-20-2010, 10:50 AM
  3. postfix evaluation
    By mayra in forum C and C++
    Replies: 7
    Last Post: 07-13-2010, 11:07 PM
  4. Expression is not a method.
    By Sniped Sniper in forum Visual Basic Programming
    Replies: 2
    Last Post: 04-23-2009, 02:06 PM
  5. c# regular expression
    By moonrise in forum C# Programming
    Replies: 3
    Last Post: 05-22-2006, 02:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts