Doubt

0 Shreyash Gupta · January 3, 2015
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

int main()
{

  int num = 1;

 do{
    if(num == 7 || num == 9){

        num++;
      continue;
   }

    printf("%d is available\n", num );
    num++;

 }while(num<= 10);
  return 0;
}



Bucky,
In your C Programming Tutorial#30 : continue, you gave us the example of the above program. However, it works fine with or without the continue function. Can you please give me another example of a program with continue function?

Post a Reply

Replies

Oldest  Newest  Rating
0 Laura Lee · January 3, 2015
Krootushas, you're missing the point of Shreyash's post and ignoring what the code is doing underneath.

It's simple, they want to avoid 7 and 9. You don't need to return to the top of the loop by using "continue" to do that.
You can just increment the variable ultimately avoiding 7 and 9.

I have stated this before, you can avoid every possible situation that requires the use of continue. Making it practically obsolete and perhaps bad practice?

Take a look at the following code and the output it produces.
#include <stdio.h>

int main()
{
    
    int num = 1;
    
    do{
        if(num == 7 || num == 9)
        {
            num++;
        }
        
        printf("%d is available\n", num );
        num++;
        
    }while(num<= 10);
    return 0;
}

1 is available
2 is available
3 is available
4 is available
5 is available
6 is available
8 is available
10 is available

Same result with the continue statement. Why? Because there is no need to use continue to return to the top of the loop. If we want to avoid 7 and 9 we can do so. In fact, we can even change the code so it does this.
#include <stdio.h>

int main()
{
    
    int num = 1;
    
    do
    {
        if( (num != 7) && (num != 9) )
            printf("%d is available\n", num );
        num++;
        
    }while(num<= 10);
    return 0;
}


1 is available
2 is available
3 is available
4 is available
5 is available
6 is available
8 is available
10 is available

Producing the exact same result as the above two scenerios only it's shorter code, cleaner and easier to understand. 

So to anyone who uses continue, just stop using it. It's literally redundant. Anyone who tries to encourage usage of continue should stop. 
0 Laura Lee · January 4, 2015
Krootushas give me an example code where continue is absolutely mandatory, and I will prove to you it isn't. 
You will never come across a piece of code where continue is absolutely necessary.
So I don't have much faith in this quote.
"But sometimes there is no other way... "
  • 1

C

103,543 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator