+ Reply to Thread
Page 1 of 3
1 2 3 LastLast
Results 1 to 10 of 21

Thread: Confused about expression evaluation

  1. #1
    Newbie n00bcoder is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    8

    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. #2
    Newbie Naeth is an unknown quantity at this point Naeth's Avatar
    Join Date
    Oct 2009
    Location
    Manchester
    Posts
    5

    Re: Confused about expression evaluation

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

  3. #3
    Newbie n00bcoder is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    8

    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?}

  4. #4
    Learning Programmer R3.RyozKidz is an unknown quantity at this point R3.RyozKidz's Avatar
    Join Date
    Oct 2009
    Posts
    92

    Re: Confused about expression evaluation

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

  5. #5
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,680
    Blog Entries
    57

    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.
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #6
    Newbie n00bcoder is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    8

    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}

  7. #7
    Newbie TheSourceOfX is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    22

    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

  8. #8
    Newbie TheSourceOfX is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    22

    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

  9. #9
    Learning Programmer R3.RyozKidz is an unknown quantity at this point R3.RyozKidz's Avatar
    Join Date
    Oct 2009
    Posts
    92

    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?

  10. #10
    Newbie TheSourceOfX is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    22

    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

+ Reply to Thread
Page 1 of 3
1 2 3 LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. How to start? Confused!!!!
    By harpreetsingh2k in forum General Programming
    Replies: 7
    Last Post: 12-01-2008, 08:49 AM
  2. Convert a generic language expression to excel formula?
    By arunsinbox in forum General Programming
    Replies: 3
    Last Post: 09-24-2008, 10:33 PM
  3. Count words using Regular Expression in c#
    By lartzi in forum C# Programming
    Replies: 1
    Last Post: 06-29-2008, 09:56 AM
  4. Replies: 2
    Last Post: 02-27-2008, 03:04 PM
  5. c# regular expression
    By moonrise in forum C# Programming
    Replies: 3
    Last Post: 05-22-2006, 04:54 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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