Go Back   CodeCall Programming Forum > Software Development > C and C++
Register Blogs Search Today's Posts Mark Forums Read

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-18-2009, 06:33 AM
Newbie
 
Join Date: Oct 2009
Posts: 8
n00bcoder is an unknown quantity at this point
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-18-2009, 06:36 AM
Naeth's Avatar
Newbie
 
Join Date: Oct 2009
Location: Manchester
Posts: 5
Naeth is an unknown quantity at this point
Send a message via MSN to Naeth
Re: Confused about expression evaluation

It runs through ++i && ++j which is obviously true so it never gets to the or?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-18-2009, 06:40 AM
Newbie
 
Join Date: Oct 2009
Posts: 8
n00bcoder is an unknown quantity at this point
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?}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-18-2009, 07:50 AM
Learning Programmer
 
Join Date: Oct 2009
Posts: 58
R3.RyozKidz is an unknown quantity at this point
Re: Confused about expression evaluation

shouldn't the( m = ++i && ++j || ++k;) ===>( m == ++i && ++j || ++k;)

Last edited by TkTech; 10-18-2009 at 08:48 AM.. Reason: Smilies
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-18-2009, 09:04 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 10-19-2009, 11:27 AM
Newbie
 
Join Date: Oct 2009
Posts: 8
n00bcoder is an unknown quantity at this point
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}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 10-19-2009, 11:53 AM
Newbie
 
Join Date: Oct 2009
Posts: 22
TheSourceOfX is an unknown quantity at this point
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 10-20-2009, 01:32 PM
Newbie
 
Join Date: Oct 2009
Posts: 22
TheSourceOfX is an unknown quantity at this point
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 10-21-2009, 01:11 PM
Learning Programmer
 
Join Date: Oct 2009
Posts: 58
R3.RyozKidz is an unknown quantity at this point
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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 10-21-2009, 06:31 PM
Newbie
 
Join Date: Oct 2009
Posts: 22
TheSourceOfX is an unknown quantity at this point
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to start? Confused!!!! harpreetsingh2k General Programming 7 12-01-2008 09:49 AM
Convert a generic language expression to excel formula? arunsinbox General Programming 3 09-24-2008 11:33 PM
Count words using Regular Expression in c# lartzi C# Programming 1 06-29-2008 10:56 AM
Facial Expression Recognition Software Developed Kernel News 2 02-27-2008 04:04 PM
c# regular expression moonrise C# Programming 3 05-22-2006 05:54 PM


All times are GMT -5. The time now is 10:34 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0