How Do You Repeat A Loop

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 9
Reputation: woozy is an unknown quantity at this point 
Solved Threads: 1
woozy woozy is offline Offline
Newbie Poster

How Do You Repeat A Loop

 
0
  #1
Feb 2nd, 2008
Say I have an infinite loop, and I want to start over or terminate depending on what user inputs.

  1. while( 1 ) // start the inifite loop
  2. {
  3. printf( "Enter your number: " ); // prompt
  4. scanf( "%d", &operand1 ); // store user entered number into operand1
  5. if ( operand1 == -1 ) {
  6. // terminates
  7. } else if ( operand1 <= 0 ) {
  8. // repeat from top
  9. }
  10. }

I know I can use break to terminate the loop or exit() to terminate the program, but I don't know how to start over the loop...and I don't wanna use goto.

Thanks in advance!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: How Do You Repeat A Loop

 
-1
  #2
Feb 2nd, 2008
Originally Posted by woozy View Post
Say I have an infinite loop, and I want to start over or terminate depending on what user inputs.

  1. while( 1 ) // start the inifite loop
  2. {
  3. printf( "Enter your number: " ); // prompt
  4. scanf( "%d", &operand1 ); // store user entered number into operand1
  5. if ( operand1 == -1 ) {
  6. // terminates
  7. } else if ( operand1 <= 0 ) {
  8. // repeat from top
  9. }
  10. }

I know I can use break to terminate the loop or exit() to terminate the program, but I don't know how to start over the loop...and I don't wanna use goto.

Thanks in advance!

How about changing the while loop criteria to this:

  1. while (operand != -1)

You'd have to either initialize operand to be something other than -1 or change the loop into a DO-WHILE loop to make sure it went through the loop at least once. You could also get rid of the if-else if statement inside the loop.

That gets you out of the loop if you enter -1, without the use of goto. You can terminate the program after the loop if you like. If by "repeat from top" you mean repeat from the top of the loop, this code does that. If by "top" you mean ABOVE that, I think I'd have to see the rest of the code and where you want to go to offer anything helpful.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: How Do You Repeat A Loop

 
0
  #3
Feb 2nd, 2008
I am not sure what you're looking for, maybe you mean continue; ?
Last edited by invisal; Feb 2nd, 2008 at 8:45 am.
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC