An alternative to Do While loops?

Moderator
Posts: 373
Joined: 2006.08
Post: #1
Hello again everyone Smile
I haven't posted in awhile, but I've been lurking... Sneaky

I've run across some (C++) code that I have once before a while loop begins executing and then again at the end of the loop. The solution seems to be to use a do...while loop, but I hate do while loops. In my opinion, they look horrible and are much harder to read than normal while loops.

Any ideas on a different solution that is cleaner and better looking? Perhaps I'm just being stubborn, but I really don't like do while loops Annoyed

Here's the code that I noticed this in:
Code:
frame tempFrame;
tempFrame.texture = -1;
std::string tempString = "";

std::string dataLine;
getline(animReadFile, dataLine);
char* stringPoint;

   while(dataLine.size() > 0)
   {
      for(stringPoint=(char*)dataLine.c_string(); *stringPoint!='\0'; stringPoint++)
      {
      //some code
      }

   //more code
  
   tempFrame.texture = -1;
   tempString = "";
  
   getline(animReadFile, dataLine);
   }

Thanks Smile
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Member
Posts: 35
Joined: 2008.06
Post: #2
Here's a way, .... not saying it is any cleaner Wink .... but less code, and no do-while.


Code:
frame tempFrame;
tempFrame.texture = -1;
std::string tempString = "";

std::string dataLine;
char* stringPoint;

   while ( 1 ) {
      getline(animReadFile, dataLine);
      if ( dataLine.size() <= 0 ) {
     break;
      }
      for(stringPoint=(char*)dataLine.c_string(); *stringPoint!='\0'; stringPoint++)
      {
      //some code
      }

     //more code
  
     tempFrame.texture = -1;
     tempString = "";
  
   }
Quote this message in a reply
Moderator
Posts: 373
Joined: 2006.08
Post: #3
Well, that is definitely an alternate solution Smile
Still, as you said, it's not exactly a clean solution...but I still like it better than do while loops Smile

Any other suggestions?

Thanks,
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #4
wyrmmage Wrote:Any other suggestions?

Learning not to hate do while loops would be the best solution. They're part of the core language syntax, so why avoid them arbitrarily?
Quote this message in a reply
Member
Posts: 312
Joined: 2006.10
Post: #5
Plus, those aren't do-while loops.. (unless I'm misunderstanding what you're saying?)

But yeah, why avoid them? Your code looks more messy because of the indenting than the while loop.
Quote this message in a reply
Moderator
Posts: 373
Joined: 2006.08
Post: #6
I know they aren't do while loops; I was just saying that I was going to convert my code to use do while loops to reduce code duplication.

Personally, I like my style of indentation better, but each to their own Wink

I guess I'll just learn to love do while loops, then Rasp

Thanks everyone Smile
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Member
Posts: 35
Joined: 2008.06
Post: #7
Even with a do-while, you are still going to have to (or should) check after the first read, and then again at the bottom of the loop

Code:
1st read
if ( dataLine.size() > 0 ) {
     do {
           // some code
           2nd read
     } while ( dataLine.size() > 0 )

} // endif
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2008.12
Post: #8
Code:
for(getline(animReadFile,dataLine); dataLine.size()>0; getline(animReadFile,dataLine)) {
    // some code
}

The full example would be:

Code:
frame tempFrame;
tempFrame.texture = -1;
std::string tempString = "";

std::string dataLine;
char* stringPoint;

for(getline(animReadFile,dataLine); dataLine.size()>0; getline(animReadFile,dataLine))
   {
      for(stringPoint=(char*)dataLine.c_string(); *stringPoint!='\0'; stringPoint++)
      {
      //some code
      }

   //more code
  
   tempFrame.texture = -1;
   tempString = "";
   }
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  PasteAll.org: A prettier alternative to RAFB Leisure Suit Lurie 2 3,014 Apr 10, 2008 04:09 PM
Last Post: Duane
  Alternative to a global VLA ferum 6 4,321 Jul 24, 2006 06:47 PM
Last Post: OneSadCookie