A Question!

This is a discussion on A Question! within the C++ Programming forums, part of the General Programming Boards category; ... If we have this segment of a program: Code: int x=5, i=0; do { i++; x--; } while(x || ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Jerusalem
    Posts
    11

    Question A Question!

    ... If we have this segment of a program:
    Code:
    int x=5, i=0;
    do
    {
           i++;  
           x--;
    } while(x || x--);
    cout<<i;
    may anyone tell me wut does "while(x || x--)" mean??? the answer would be 5! but i don't know why!

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    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.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Jerusalem
    Posts
    11
    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 :
    Code:
    int n=0, m=1;
    while(n)
    { ++n;
    m+=n;
    cout<<m<<" "<<n;
    }
    this is can't be right... right? cuz n=0 and while(0) it doesnt mean anything? so this program would not run? right?

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by csc_paradise28
    this is can't be right... right? cuz n=0 and while(0) it doesnt mean anything? so this program would not run? right?
    No, it will run perfectly fine. It just won't produce any output since you would not enter the body of the loop.
    "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

  7. #7
    Registered User
    Join Date
    Jan 2006
    Location
    Jerusalem
    Posts
    11
    i didn't get it! in the while loop there z
    Code:
    cout<<m<<" "<<n;
    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????

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The program will run. The loop will not be entered.
    Kurt

  9. #9
    Registered User
    Join Date
    Jan 2006
    Location
    Jerusalem
    Posts
    11
    while(0) means wut? and wut does it differ from while(++n)?????

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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.

  11. #11
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    while(0) means wut? and wut does it differ from while(++n)?????
    wut means wut? and how does wut differ from what?

    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.

  12. #12
    Registered User
    Join Date
    Jan 2006
    Location
    Jerusalem
    Posts
    11
    i mean that if we have in any program this segment:
    Code:
    int n=0;
    while(n){
    ++n;
    cout<<n;
    }
    i meant wut does the while(n) do in this case? hope you got it now...
    but i guess i understood it now...
    thnx!

  13. #13
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Code:
    while(n);
    is the same as

    Code:
    while(n==1);
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

  15. #15
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    ?
    Code:
    while(n);
    is the same as

    Code:
    while(n != 0)
    ?

    I thought it was the other way around...

    Code:
    while(!n) // Same as while(n==0)
    
    while(n) //Same as while(n==1)
    I got that straight out of a C++ book -,-. I'm very much confused right now.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Page 1 of 2 12 LastLast
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, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21