better explanation of uses of different loops?

Put your programming skills to the test in these challenges.

better explanation of uses of different loops?

Post by ghostheadx2 on Tue Jun 23, 2015 3:53 pm
([msg=88622]see better explanation of uses of different loops?[/msg])

Here's what the study guide I from class says about the usefulness of different types of loops:

•A for loop may be useful if the number of repetitions is known
•A while loop may be useful if the number of repetitions is not known,
(e.g. sentinel value)
•A do-while loop can be used to replace a while loop if the loop body has to be executed at least once


I have trouble understanding the reason a do-while loop would be useful compared to a while loop based on this info.

-- Tue Jun 23, 2015 3:55 pm --

I understand that a do while loop can replace a while loop if it has to be executed a minimum of one time, I think. But when in practice do I use the different loops. Can someone give examples? :D
ghostheadx2
Contributor
Contributor
 
Posts: 728
Joined: Wed Nov 19, 2014 1:19 am
Blog: View Blog (0)


Re: better explanation of uses of different loops?

Post by NightQuest on Tue Jun 23, 2015 7:44 pm
([msg=88631]see Re: better explanation of uses of different loops?[/msg])

A while loop is only executed while the condition is true
Code: Select all
while( condition ) { do_something(); }


so if I write this, it's never executed:
Code: Select all
while( false ) { do_something(); }


however, if I want that code to execute at least once, I can use a do while loop:
Code: Select all
do { do_something(); } while( false );


with all loops, they only repeat while the condition is true, this allows you to force a single execution.

For instance:
Code: Select all
int ret = 0;
do
{
   ret = recv(hSock, buff, sizeof(buff)-1, 0);
   if( ret != SOCKET_ERROR && ret != 0 )
   {
      for( int x = 0; x < ret; x++ )
      {
         char buf = buff[x];
         final_str.push_back( buf );
      }

      ZeroMemory(buff, sizeof(buff));
   }
} while( ret != SOCKET_ERROR && ret != 0 );

in that example, I know I want to execute recv at least once.

A simple while loop would require two calls to recv:
Code: Select all
int ret = recv(hSock, buff, sizeof(buff)-1, 0);
while( ret != SOCKET_ERROR && ret != 0 )
{
   if( ret != SOCKET_ERROR && ret != 0 )
   {
      for( int x = 0; x < ret; x++ )
      {
         char buf = buff[x];
         final_str.push_back( buf );
      }

      ZeroMemory(buff, sizeof(buff));
      ret = recv(hSock, buff, sizeof(buff)-1, 0);
   }
}
User avatar
NightQuest
Administrator
Administrator
 
Posts: 49
Joined: Sun Feb 22, 2009 6:03 am
Blog: View Blog (0)


Re: better explanation of uses of different loops?

Post by pretentious on Wed Jun 24, 2015 7:24 am
([msg=88636]see Re: better explanation of uses of different loops?[/msg])

Wow, night quest! Didn't think I'd see the day.
Everyone's coming back, this shit cray.
*drops mic*
Goatboy wrote:Oh, that's simple. All you need to do is dedicate many years of your life to studying security.

IF you feel like exchanging ASCII arrays, let me know ;)
Can you say brainwashing It's a non stop disco
User avatar
pretentious
Addict
Addict
 
Posts: 1219
Joined: Wed Mar 03, 2010 12:48 am
Blog: View Blog (0)


Re: better explanation of uses of different loops?

Post by NightQuest on Wed Jun 24, 2015 10:02 am
([msg=88640]see Re: better explanation of uses of different loops?[/msg])

pretentious wrote:Wow, night quest! Didn't think I'd see the day.
Everyone's coming back, this shit cray.
*drops mic*

*picks up mic and puts it away carefully*
;)
User avatar
NightQuest
Administrator
Administrator
 
Posts: 49
Joined: Sun Feb 22, 2009 6:03 am
Blog: View Blog (0)



Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests