Thread: A Question!

  1. #16
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    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

  2. #17
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    so...

    Code:
    while(n) //is while(n >= 1)
    So, != is just >=?
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  3. #18
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    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
    Code:
    while()
    {
    statements;
    }
    False value-0
    True value-Any non zero value
    So,in above while loop the parenthesis can contain anything.
    Following are all valid declaration.
    Code:
    while(0)//False condition means loop will not get executed
    {}
    while(1)//Always True means infinite loop
    {}
    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.

  4. #19
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by Blackroot
    so...

    Code:
    while(n) //is while(n >= 1)
    So, != is just >=?
    != is !=. >=1 does not include negative values which !=0 does include.

    Code:
    int n = -1;
    while(n)
    {
        cout << "Hello" << endl;
        ++n;
    }
    This will output "Hello" once.

    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

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while(n) //is while(n >= 1)
    Try it with a negative n and find out...

  6. #21
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    no,it is n!=0

    Code:
    int main()
    {
    int n=-1
    while(n)
    {
    cout<<"Hello World";
    n=0;
    }
    getch();
    }
    Above prog prints hello world once as expected.

  7. #22
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    int n=-1
    Or some errors, like "missing semicolon", "undefined reference to cout", and ditto for getch().

    Code:
    while(!n) // Same as while(n==0)
    You got that part right.
    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.

  8. #23
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    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.

  9. #24
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    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.

  10. #25
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    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.

  11. #26
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    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:
    Code:
    cout<<endl<<x;
    x will be -1.

    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

  12. #27
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Lightbulb

    If we use x-- the code is equal to this
    Code:
    int x=5; int i=0;
    do
    {
    	i++;
    	x--;
    }while(x);
    x--;
    cout<<i;
    But if we use --x it is
    Code:
    int x=5; int i=0;
    do
    {
    	i++;
    	x--;
    	if(x==0)x--;
    }while(x);
    
    cout<<i;
    In second code, it wont exit the loop.
    Last edited by siavoshkc; 01-16-2006 at 11:54 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM