DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Re: reference, etc. Please Jim, once more

  1. #1
    Al Gruber Guest

    Re: reference, etc. Please Jim, once more


    "James Curran" <JamesCurran@mvps.org> wrote:
    Jim, I don't get the while line, and I certainly don't understand why anybody
    would write code like this.
    I assume that the program gets 2 chars in a row, ch and x. Then it checks
    to see if the first of the 2 is a 'q'. What then would the second one be:
    Enter?

    Also, what if any limits are there on what you can put in the condition portion
    of a while? How about:
    int a=1,b=2,c=3,d;
    char * msg="hello world";
    while(cin.get(ch), d=a+b+c, b=log10(a), cout<<*msg, cin.get(x) , ch != 'q')
    I'm not sure why, but this really bothers me!
    Thanx Al

    >
    >> while(cin.get(ch), cin.get(x) , ch != 'q') //WHAT DOES 'ch'
    >> //AND 'x' REPRESENT??

    >
    > ch & x are character varaibles as defined a few lines earlier.
    >"cin.get(ch)" means "get a character and put it in 'ch'". Now, since
    >everything in the while expression is separated by commas, it's considered
    >one big expression, equal to the value of the last part. So, in other words,
    >we get two characters inside the loop, and then to the comparision to exit
    >the loop. That's pretty much the same as:
    >
    > while (1) // loop forever
    > {
    > ch = getchar();
    > x = getchar();
    > if (ch == 'q')
    > break;
    > // etc...
    >
    >



  2. #2
    Danny Kalev Guest

    Re: reference, etc. Please Jim, once more



    Al Gruber wrote:
    >
    > "James Curran" <JamesCurran@mvps.org> wrote:
    > Jim, I don't get the while line, and I certainly don't understand why anybody
    > would write code like this.
    > I assume that the program gets 2 chars in a row, ch and x. Then it checks
    > to see if the first of the 2 is a 'q'. What then would the second one be:
    > Enter?


    More or less. It would be the carriage return character or CTL-Z.
    >
    > Also, what if any limits are there on what you can put in the condition portion
    > of a while?

    There are no limits as long as the expression evaluates as a boolean
    value.

    How about:
    > int a=1,b=2,c=3,d;

    fine, evaluates as d.
    > char * msg="hello world";

    also fine, evaluates as msg's value. true if it isn't NULL, false
    otherwise (it's never NULL because it's a constant)

    > while(cin.get(ch), d=a+b+c, b=log10(a), cout<<*msg, cin.get(x) , ch != 'q')


    The important thing here is the last sub expression. The condition is
    evaluated according to the final sub expression's value. As long as that
    sub expression is convertible to a boolean result, it's fine. In this
    example, the final sub expression (i.e., the one after the last comma)
    is ch != 'q' which is a boolean expression that may be either true or
    false. If it's false the while loop will terminate; otherwise it will
    iterate once more. I agree that such coding style is a recipe for
    spaghetti code but when used properly, it's a very common programming
    idiom in C and C++.

    Danny

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center