It was a little while since I last programmed and I have seem to forgotten if it's acceptable to use an empty "for loop" for creating an infinite loop?
for(;;)
Currently I use this method in a program to make it repeatedly ask the user to enter two numeric values one for each double variable in the program. The programs then calls a function and calculates a sum of these two pairs of numbers.
To terminate the program i have "if" statements that check if the user input value is zero, If the value is zero the program terminates using an "Return 0;" argument.
The program checks each user input value if it's zero directly after the value has been assigned to the variable.
So to the real question: Is this a correct way to make my program do what i described? Or is there a more/better/accepted way of programming this?
And secondly is there anything wrong with use the "Return 0" argument the way i did in this program?
If you thinks it's hard to understand what I'll wrote or meant please reply, and I will take more time to write everything.
Empty Statement in a for Loop Here, the initialization block of the for loop contains nothing, hence an empty statement. For creating a loop that runs infinitely, we can use empty statements. However, if we use break statements inside the body of the loop, then the loop can terminate.
However, in a for loop, any of the three fields can be left blank. If you leave init blank, then there is no initialization phase. You just start with the cond. If you leave update blank, then there is no update phase.
An empty loop is a loop that doesn't contain any executable statement, whereas, an infinite loop is a loop that runs an infinite number of times. An empty loop contain only one empty statement. They are mostly used to produce time breaks. for(int a=0;a<10;a++); An infinite loop on the other hand continues forever.
What you're doing is perfectly fine, and an idiomatic way of writing and exiting an infinite loop.
I always use while(true)
for infinite loops
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With