Either your book is wrong or you understood something wrong.
In c/c++ every non-zero value evaluates to true when used in a boolean context.
Kurt
Either your book is wrong or you understood something wrong.
In c/c++ every non-zero value evaluates to true when used in a boolean context.
Kurt
so...
So, != is just >=?Code:while(n) //is while(n >= 1)
Code:Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)
I don't understand why you guys are so much confused.
Just remember in while or if the statement under them is evaluated only if expression within them returns true
So,in above while loop the parenthesis can contain anything.Code:while() { statements; } False value-0 True value-Any non zero value
Following are all valid declaration.
So if you put a variable inside the parenthesis if the value of variable is 0 then it will get not get executed ,otherwise it will.Code:while(0)//False condition means loop will not get executed {} while(1)//Always True means infinite loop {}
!= is !=. >=1 does not include negative values which !=0 does include.Originally Posted by Blackroot
This will output "Hello" once.Code:int n = -1; while(n) { cout << "Hello" << endl; ++n; }
So, while(n) is like saying while(n!=0). Get it?
"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
> while(n) //is while(n >= 1)
Try it with a negative n and find out...
no,it is n!=0
Above prog prints hello world once as expected.Code:int main() { int n=-1 while(n) { cout<<"Hello World"; n=0; } getch(); }
Or some errors, like "missing semicolon", "undefined reference to cout", and ditto for getch().Code:int n=-1
You got that part right.Code:while(!n) // Same as while(n==0)
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Maybe some compilers work different but as far as I know any non zero value is true and zero is false. While(n) means stay in loop while n is true and exit if it is false. Lets take x=3 then in the program above we have this:
1)) i+1 so it will be 1.
2)) x-1 so x is 2.
3)) Comparing values, if one of (x or x-1) is non zero stay in loop, this is, so it stays in loop.
We should notice that value of x is not changed.
It stays in loop until x==0.
Last edited by siavoshkc; 01-16-2006 at 10:22 PM.
4)) i+1 so it will be 2.
5)) x-1 so x is 1.
6)) x is 1 so stays in loop.
7)) i+1 so it will be 3.
8)) x-1 so x is 0.
9)) exits loop.
10)) prints out 3
As you can see, i is equal to first x (3).
And about !, it will turn true to untrue and vice versa.
In the last loop x is 0 and x-- will be -1, huh? Yes, but after the comparison!
I think it is the difference between --x and x--. Funny isn't it?
If you replace x-- by --x then it never exits loop!
Because then it will first x-1, then determines T or F . So in all times it will be while(true).
In a||b if one of the operands are non zero it will be true.
When we use (a||b), computer takes (a) (Left hand operand). If it is non-zero it will mark all (a||b) equal to true without examining b. If (a) was zero then it will examin b.
For this reason in (x||x--), x-- operation will not be executed until x==0. So only in last loop it will be executed(after "OR" operation of course) and if you add:
x will be -1.Code:cout<<endl<<x;
If we use --x, the operation will be executed in the last loop again but before "OR" operation!
If n is equal to zero, we have !n <=> n!=0
If n is non-zero, we have !n <=> n==0
If we use x-- the code is equal to this
But if we use --x it isCode:int x=5; int i=0; do { i++; x--; }while(x); x--; cout<<i;
In second code, it wont exit the loop.Code:int x=5; int i=0; do { i++; x--; if(x==0)x--; }while(x); cout<<i;
Last edited by siavoshkc; 01-16-2006 at 11:54 PM.