• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

do while loop

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
How're you doing?
One simple question is about the do while loop.
Even though I know that loop will always execute at least once.
The general form is
do{
statments;
}while(condition);

However, I don't get the meaning of one example in my textbook.
The example is as follows:

Is that any problems about the boolean expression in the two do while condition
(ch == '\n'| ch =='\r') , (answer !=ch)
Could anyone explain that code please?
Thank you very much
Larry
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ June 23, 2002: Message edited by: Dirk Schreckmann ]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
basically, the inner do-while keeps reading in a new char and testing to make sure it isn't either CR or LF. If it is, it gets another. Once it has a valid char (not CR or LF) it tests and makes the appropriate comment.
The boolean expression using a bitwise comparison of the boolean expressions c==\n and c==\r. Basically, this just treats the individual booleans as 2 bits and OR's them together. It has the exact same effect as c==\n || c==\r, so I don't really know why a person would use it. I usually reserve bitwise comparisons when the bits of my numbers hold crucial information (a simple array). From what I've read, do-while is not good coding, since the condition test is not immediately visible to the person reading the code. "while(test){}" is much clearer to the reader. You can do anything a do-while can do with a simple while (provided you initialize certain values you want to test for, about the only advantage I know of, for do-while.)
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The boolean expression using a bitwise comparison of the boolean expressions c==\n and c==\r. Basically, this just treats the individual booleans as 2 bits and OR's them together. It has the exact same effect as c==\n || c==\r, so I don't really know why a person would use it.
Using the | or || operators does not produce the exact same effect (necessarily). The || operator is a short-circuiting operator - as soon as a true value is determined or if all conditions have tested false, the test is satisfied. So, if the first condition tests true, then subsequent conditions are not tested.
When using a short-circuiting operator, the programmer must realize that all of the assignments or tests, that are part of some compound boolean test, might not occur.
Of course, in the example presented by Larry, this matter is of little consequence.
[ June 23, 2002: Message edited by: Dirk Schreckmann ]
 
Ian Ohlander
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgot about that short circuiting effect. Now it seems more clear why a person would use it. To force the second boolean to be evaluated no matter what.
 
I don't like that guy. The tiny ad agrees with me.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic