|
||||||
| 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. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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; 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 |
|
||||
|
Re: Confused about expression evaluation
Code:
int i = -3, j = 2, k = 0 m = ++i && ++j || ++k; ++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 |
|
|||
|
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; {sorry for being such a nag... i'm only trying to get hold of the basics} |
|
|||
|
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; Code:
m = ++i || ++j && ++k; Code:
m = ++i || (++j && ++k); -TheSourceOfX |
|
|||
|
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; 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 |
|
|||
|
Re: Confused about expression evaluation
Quote:
|
|
|||
|
Re: Confused about expression evaluation
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;
}
-TheSourceOfX |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| 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.
Amrosama.cc
Arekbulski.cc
Debtboy.cc
Guest.cc
Jaan.cc
James.cc
Mathx.cc
Tsz.cc
Vswe.cc