| | |
How Do You Repeat A Loop
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2007
Posts: 9
Reputation:
Solved Threads: 1
Say I have an infinite loop, and I want to start over or terminate depending on what user inputs.
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!
C Syntax (Toggle Plain Text)
while( 1 ) // start the inifite loop { printf( "Enter your number: " ); // prompt scanf( "%d", &operand1 ); // store user entered number into operand1 if ( operand1 == -1 ) { // terminates } else if ( operand1 <= 0 ) { // repeat from top } }
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!
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
•
•
•
•
Say I have an infinite loop, and I want to start over or terminate depending on what user inputs.
C Syntax (Toggle Plain Text)
while( 1 ) // start the inifite loop { printf( "Enter your number: " ); // prompt scanf( "%d", &operand1 ); // store user entered number into operand1 if ( operand1 == -1 ) { // terminates } else if ( operand1 <= 0 ) { // repeat from top } }
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:
C Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- factorial using a for loop (C++)
- Help With Transposition Cipher Loop ... (C++)
- Procedure Repeat Loop (Pascal and Delphi)
- file save load problems (C++)
- repeat region into declared variable (jmail body) (ASP)
- need help with while loop (Java)
- How Do I Repeat The Programm Without Go Back To Windows? (C++)
Other Threads in the C Forum
- Previous Thread: stack and queues
- Next Thread: check my code please..
| Thread Tools | Search this Thread |
Tag cloud for C
#include ansi array arrays asterisks binarysearch calculate centimeter changingto char convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework getlasterror givemetehcodez grade graphics gtkgcurlcompiling hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard multi mysql number opendocumentformat opensource owf pattern pdf performance pointer posix problem probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windows.h windowsapi






