... If we have this segment of a program:
may anyone tell me wut does "while(x || x--)" mean??? the answer would be 5! but i don't know why!Code:int x=5, i=0; do { i++; x--; } while(x || x--); cout<<i;
... If we have this segment of a program:
may anyone tell me wut does "while(x || x--)" mean??? the answer would be 5! but i don't know why!Code:int x=5, i=0; do { i++; x--; } while(x || x--); cout<<i;
It's a demonstration of lazy evaluation of logical operators. (x || x--) effectively says:
Take x. If its value evaluates to true (i.e. is not 0), let the expression evaluate to true and stop.
Otherwise, take the next expression, x--. Evaluate it - this decrements x and yields the value it had before - and if it evaluates to false AGAIN (this is the only logical possibility in this case, but don't let that distract you), return false for the whole expression, otherwise true.
In other words, (expr1 || expr2) means, "either expr1 is true OR ELSE expr2 is true - but if expr1 is true, don't bother looking at expr2".
It's a rather poor example, because it obfuscates the important stuff with lots of decrements and a loop. Here's a simpler example:
Code:bool b1 = true || (std::cout << "Shortcut in first line not taken.\n"); bool b2 = false || (std::cout << "Shortcut in second line not taken.\n");
Last edited by CornedBee; 01-11-2006 at 05:53 PM.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
It does not mean anything. || is left-to-right associative, and the x-- is never executed (short circuit evaluation occurs). x is decremented after it evaluates to a false 0, making it -1 however.
The loop runs 5 times. After the fifth run, x is 0, so the first part of the while control evaluates to false. Then the second part (x--) is evaluated. It is also falso because it returns the value of x before decrementing it, which in this case is zero. So false or false is false and the loop exits. i is 5 because the loop ran 5 times, and x is -1 because it stopped when x was zero but the x-- decrement ran inside the while control the last time through.
oh.. ok.. i got it perfectly well! bunch of thanks....
but i want to make sure of something else depending on the example above;
if we have this :
this is can't be right... right? cuz n=0 and while(0) it doesnt mean anything? so this program would not run? right?Code:int n=0, m=1; while(n) { ++n; m+=n; cout<<m<<" "<<n; }
No, it will run perfectly fine. It just won't produce any output since you would not enter the body of the loop.Originally Posted by csc_paradise28
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
i didn't get it! in the while loop there z
if it runs then it sould print the values of both of them??? and if it runs wiit does while( 0) mean? and if replaced while(0) with while(++n) wut while(++n) mean????Code:cout<<m<<" "<<n;
The program will run. The loop will not be entered.
Kurt
while(0) means wut? and wut does it differ from while(++n)?????
The control of the while loop (what's between the parentheses) is a boolean expression that determines how long the loop executes and when it stops. A boolean expression evaluates to either true or false. If it evaluates to true, the loop is run, if it evaluates to false, the loop is not run.
If you put in 0, then it evaluates to false because in C++ 0 means false. That means the loop is not run. If you put in something like ++n, then before each time through the loop it will execute the code ++n and evaluate it to see if it is true or false. When at some point it becomes 0 then it evaluates to false and the loop stops.
wut means wut? and how does wut differ from what?while(0) means wut? and wut does it differ from while(++n)?????
Sorry, I couldn't help myself.
The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.
i mean that if we have in any program this segment:
i meant wut does the while(n) do in this case? hope you got it now...Code:int n=0; while(n){ ++n; cout<<n; }
but i guess i understood it now...
thnx!
is the same asCode:while(n);
Code:while(n==1);
Code:Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)
No, it's not. It's the same as
Code:while(n != 0)
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
?
is the same asCode:while(n);
?Code:while(n != 0)
I thought it was the other way around...
I got that straight out of a C++ book -,-. I'm very much confused right now.Code:while(!n) // Same as while(n==0) while(n) //Same as while(n==1)
Code:Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)