0

Here is an example. For small programs that's fine to use , but what if we are developing a real time project or Application . Need some suggestions

while (TRUE )
{
    int temp =0 ;
    printf ( "How many no's would you like to enter : " ) ;
    temp = scanf ( "%d" , &n )  ;
    if ( temp==1 )
        break ;
    else
    {
        printf ("Invalid input. Try again. \n" ) ;
        fflush ( stdin ) ;
    }
}
  • I believe the C and C++ frameworks are full of library code which has `while` loops depending on boolean conditions (e.g. waiting for a mutex), so I don't see anything wrong with per se, assuming your logic is sound. – Tim Biegeleisen Oct 06 '15 at 05:28
  • Firstly, the code which you have shared, can it be considered as an example for a real time application? Do you think the above code produces latency for the user and that would be increased by existence of infinite loop? – a3.14_Infinity Oct 06 '15 at 05:30
  • I'm not so sure , any improvements that could be made.? ..@a3.14_Infinity –  Oct 06 '15 at 05:44
  • This is really a matter of style. I think infinite loops have their place, but if an `if () break;` condition can be moved to the loop condition, it will probably ease readability. – PC Luddite Oct 06 '15 at 05:55
  • yeah...thats true...!! –  Oct 06 '15 at 05:56
  • 1
    By the way, `fflush()` should only be used on output streams. http://stackoverflow.com/questions/2979209/using-fflushstdin – PC Luddite Oct 06 '15 at 06:01
  • Well, this goes argumentative. But in many scenarios infinite loops are common and doesn't hurt as long as your code is doing what it is intended to do. Here's a good one - http://stackoverflow.com/questions/11256993/are-all-infinite-loops-bad – Sri Harsha Kappala Oct 06 '15 at 06:05
  • yes , will make sure that the code is good one . Thanks...@harsha –  Oct 06 '15 at 06:11

2 Answers2

0

If you're asking about style, recommendations are going to be somewhat subjective.

If you're afraid that your loops may turn into true infinite loops when undesired, then you need to do something about that. For example:

  • redesign your code to be an explicit state machine with clearly documented and implemented conditions for when the state changes and when it remains the same
  • think through the various error conditions that may occur (and thus potentially cause hanging in a specific state indefinitely) and handle them the best you can, also think of those conditions that are fatal and can't be handled gracefully (do you crash and burn? do you log an error? do you show it on some dash board? do you eject the driver's seat with the driver? do you dial an emergency phone number if applicable? etc etc), incorporate that into the state machine
  • review the code with your peers (maybe even hire industry experts for this)
  • implement and use tests (unit/integration/scenario-based/scalability/stress/security/etc etc)
Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
0

The trouble with any loops, be they while(TRUE) or while(condition), is their tendency to have a sneak infinite loop condition - like this one.

OP's code depends on 2 results of scanf("%d",...,: 1 and not 1.

If user enters "123", all is good. scanf() returns 1, loop exits, we all leave work and have a pint.

If user enters "abc", scanf() returns 0, code does a fflush(stdin) to empty the stdin. (This is really UB, but let us pretend it works.) Code loops, prompts again, our drinks get warm, but hopefully we will eventually enter digits.

But let us imagine the user closed stdin - maybe code had re-directed input and scanf() eventually returns EOF. Code loops, but fflush(stdin) does not re-open stdin, and scanf() again returns EOF - and again and again - true infinite loop - code will not pause for input and just says ""Invalid input. Try again." which translates into "dumb guy, dumb guy, dumb guy ...". Looks like the crew will start their brews without us.

Moral of the story: loop when code is working as intended (User entered good data). Watch out for loops when functions loops on the unexpected.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256