empty for-loops purpose
May 11, 2012 at 7:05pm UTC
Is it good to use empty for-loops as such:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
struct node *read_numbers (void )
{
struct node *first = NULL;
int n;
printf ("Enter a series of integers (0 to terminate); " );
for (;;)
{
scanf ("%d" , &n);
if (n == 0)
return first;
first = add_to_list (first, n);
}
}
I found in C book, or is same purpose as a while loop so just loops until it enters the if-statement...
Last edited on May 11, 2012 at 7:06pm UTC
May 11, 2012 at 7:12pm UTC
i get it, it's just a loop so difference is rather than evaulate condition in the for loop header or while loop header, it needs to evaulate condition inside loop and we must have a return or break to exit loop...neat such as:
1 2 3 4 5 6 7 8 9 10 11
int counter = 0;
for (;;)
{
if ( counter == 10 )
{
cout << endl << "hello" << endl;
break ;
}
else
cout << counter++ << " " ;
}
Topic archived. No new replies allowed.