Thread: What could be the real life example of continue statement ?

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    68

    What could be the real life example of continue statement ?

    I did google search but I couldn't found the real life example of continue statement in c programming.

    I have looked some example but I can't figure if the statement are useful in program

    Lets look this program from the book

    Code:
    // C program to explain the use  // of continue statement  
    #include <stdio.h> 
      
    int main() { 
        // loop from 1 to 10  
        for (int i = 1; i <= 10; i++) {  
      
            // If i is equals to 6,  
            // continue to next iteration  
            // without printing  
            if (i == 6)
    		{			
                continue;  
            } 
            else
    		{
                // otherwise print the value of i  
                printf("%d ", i);  
    		}
        }  
      
        return 0;  
    }
    Program gives output
    1 2 3 4 5 7 8 9 10

    If I remove continue statement from the code. there is nothing change happen in program out

    It gives same result as it was
    1 2 3 4 5 7 8 9 10

    What could be the real life example of continue statement ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,856
    That's because you've separated the logic into two branches, and in the if branch, control will continue to the next iteration regardless of whether you have the continue statement there. Try:
    Code:
    // C program to explain the use  // of continue statement
    #include <stdio.h>
    
    int main() {
        // loop from 1 to 10
        for (int i = 1; i <= 10; i++) {
    
            // If i is equals to 6,
            // continue to next iteration
            // without printing
            if (i == 6)
            {
                continue;
            }
    
            // otherwise print the value of i
            printf("%d ", i);
        }
    
        return 0;
    }
    Now, the printf will always print if you don't continue to the next iteration early with the continue statement, as you can see from trying this example, and again with the continue commented out.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by laserlight View Post
    That's because you've separated the logic into two branches, and in the if branch, control will continue to the next iteration regardless of whether you have the continue statement there. Try:
    Not sure

    Code:
    // C program to explain the use  // of continue statement#include <stdio.h>
    
    
    int n = 3;
     
    int main() {
        // loop from 1 to 5
    	
        for (int i = 0; i < 5; i++) {
     
            // If i is equals to 3,
            // continue to next iteration
            // without printing
            if (i == n)
            {
    			 printf("i ==6 \n");
                continue;
            }
    		
    		printf("i \n");
        }
     
        return 0;
    }
    if I comment continue statement or If I use continue statement so both condition program give the same output

    i
    i
    i
    i ==6
    i
    i

    What's wrong with my thinking ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,856
    Program with continue:
    Code:
    #include <stdio.h>
    
    int n = 3;
    
    int main() {
        // loop from 1 to 5
    
        for (int i = 0; i < 5; i++) {
    
            // If i is equals to 3,
            // continue to next iteration
            // without printing
            if (i == n)
            {
                printf("i ==6 \n");
                continue;
            }
    
            printf("i \n");
        }
    
        return 0;
    }
    Output:
    Code:
    i 
    i 
    i 
    i ==6 
    i
    Program without continue:
    Code:
    #include <stdio.h>
    
    int n = 3;
    
    int main() {
        // loop from 1 to 5
    
        for (int i = 0; i < 5; i++) {
    
            // If i is equals to 3,
            // continue to next iteration
            // without printing
            if (i == n)
            {
                printf("i ==6 \n");
            }
    
            printf("i \n");
        }
    
        return 0;
    }
    Output:
    Code:
    i 
    i 
    i 
    i ==6 
    i 
    i
    As you can see, the outputs are different. Well, maybe you cannot see, but that's because you chose a terrible way of producing output such that you end up playing "spot the difference" rather than being able to see the difference at a glance. (Hint: the second output has an additional "i")

    So, what's wrong with your thinking is that you thought it was a good idea to make things difficult for yourself instead of choosing to generate output that would be obviously different.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    302
    You must have read about the "break" keyword before reading about the "continue" keyword. As soon as "break" is encountered, the compiler essentially skips to the next executable statement. As soon as "continue" is encountered, instead of skipping to the next executable statement, the compiler skips that entire iteration. So, dry run the code in your mind/paper and see what the program to should be doing.

    Code:
    int main (void)
    {
          std::cout << "Numbers from 0-9: ";
    
          for (int i = 0; i < 10; i++)
                std::cout << i << " ";
    
          int Num;
    
          std::cout << std::endl << "Enter a number between 0-9 that you don't want to see: "; std::cin >> Num;
    
          std::cout << std::endl << "Numbers from 0-9: ";
    
          /* It depends where you place the continue statement - 1 */
          for (int j = 0; j < 10; j++)
          {
                if (j == Num) continue;
    
                std::cout << j << " ";
          }
    
          std::cout << std::endl << "Numbers from 0-9: ";
    
          /* It depends where you place the continue statement - 2 */
          for (int k = 0; k < 10; k++)
          {
                std::cout << k << " ";
    
                if (k == Num) continue;
          }
    
          std::cout << std::endl << "Numbers from 0-9: ";
    
          /* break statement instead */
          for (int l = 0; l < 10; l++)
          {
                if (l == Num) break;
    
                std::cout << l << " ";
          }
    
          return 0;
    }
    Take it as an exercise and first try to predict the output yourself and write it down. The solution is below. Does it make sense now?

    OUTPUT:

    Code:
    Numbers from 0-9: 0 1 2 3 4 5 6 7 8 9
    Enter a number between 0-9 that you don't want to see: 5
    
    Numbers from 0-9: 0 1 2 3 4 6 7 8 9
    Numbers from 0-9: 0 1 2 3 4 5 6 7 8 9
    Numbers from 0-9: 0 1 2 3 4

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Zeus_ View Post
    You must have read about the "break" keyword before reading about the "continue" keyword.
    Thank you
    break statement
    Code:
     #include <stdio.h> 
    int n = 3;
     
    int main() {
        // loop from 1 to 5
        for (int i = 0; i < 5; i++) {
    		
    		    if (i == n) 
    				break;
     
                printf("i = %d \n", i);
        }
     
        return 0;
    }
    i = 0
    i = 1
    i = 2


    continue statement
    Code:
    #include <stdio.h>
     
    int n = 3;
     
    int main() {
        // loop from 1 to 5
        for (int i = 0; i < 5; i++) {
    		
    		    if (i == n) 
    				continue;
     
                printf("i = %d \n", i);
        }
     
        return 0;
    }
    i = 0
    i = 1
    i = 2
    i = 4

  7. #7
    Registered User
    Join Date
    Aug 2019
    Location
    inside a singularity
    Posts
    302
    It's nice that you understand how it works now. A real life example of where you might wanna use continue is when you're deleting records in a file using a temporary file. You iterate and copy all the contents of one file to the other except for the one which you want to be deleting (where you'd be using a check in your iterations and as soon as the check results to true, you continue instead of writing into the temp file). Then you delete the old file and rename the temporary file to whatever the old file was called.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C in Real Life
    By Adak in forum C Programming
    Replies: 6
    Last Post: 08-30-2010, 10:04 PM
  2. Design Outside the Box, aka Games in real life
    By Mario F. in forum General Discussions
    Replies: 21
    Last Post: 02-27-2010, 12:21 PM
  3. amw real life cryptology zodiac
    By kryptkat in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 12-17-2007, 09:31 AM
  4. How would the online-world be in real life?
    By Magos in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 06-02-2005, 02:11 PM
  5. real life...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 11-17-2001, 07:30 PM

Tags for this Thread