Thread: Code to explain use of continue statement

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    56

    Code to explain use of continue statement

    I am trying to figure out what happens when we use continue statement in code. I have written two code to see the difference but both gives same output

    consider this situation with continue statement
    Code:
    #include <stdio.h>
    
    int main()
     {
       int x; 
       for ( x = 0; x < 10; x++ )
    	   if ( x == 4)
    	   { 
             continue;
    		   printf("x = %d \n", x);
    	   }
    	   
    	   else
    	   {
    		  printf(" x = %d \n", x);  
    	   }
       return 0;
    }
    x = 0
    x = 1
    x = 2
    x = 3
    x = 5
    x = 6
    x = 7
    x = 8
    x = 9

    without continue statement

    Code:
    #include <stdio.h>
    
    
    int main()
     {
       int x; 
       for ( x = 0; x < 10; x++ )
    	   if ( x == 4)
    	   { 
    //         continue;
    		   printf("x = %d \n", x);
    	   }
    	   
    	   else
    	   {
    		  printf(" x = %d \n", x);  
    	   }
       return 0;
    }
    x = 0
    x = 1
    x = 2
    x = 3
    x = 4
    x = 5
    x = 6
    x = 7
    x = 8
    x = 9

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,362
    The output is obviously different. Ok, maybe not that obvious, but that's because you printed so many lines when just a few critical ones would have done the job.
    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
    May 2021
    Posts
    56
    Quote Originally Posted by laserlight View Post
    The output is obviously different. Ok, maybe not that obvious, but that's because you printed so many lines when just a few critical ones would have done the job.
    I wanted to see what happens when continuous statement use in code In both code I don't understand what happens if I use continuous keyword in code.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,362
    You've been told to make use of learning material like introductory books that would explain these to you. Do that.

    If you want to do exploratory learning, well, that's not necessarily wrong, but you need to be sufficiently observant to learn. Over here you didn't notice a difference when a young child doing all the counting they can do would have spotted it.
    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
    May 2021
    Posts
    56
    Quote Originally Posted by laserlight View Post
    You've been told to make use of learning material like introductory books that would explain these to you. Do that.

    If you want to do exploratory learning, well, that's not necessarily wrong, but you need to be sufficiently observant to learn. Over here you didn't notice a difference when a young child doing all the counting they can do would have spotted it.
    There is no detailed example given in the book, only theoretical explanation which was not enough for me to understand. That was the reason I wrote my own code

  6. #6
    Registered User
    Join Date
    Oct 2019
    Posts
    46
    Quote Originally Posted by Rahul11 View Post

    x = 0
    x = 1
    x = 2
    x = 3
    x = 5
    x = 6
    x = 7
    x = 8
    x = 9


    x = 0
    x = 1
    x = 2
    x = 3
    x = 4
    x = 5
    x = 6
    x = 7
    x = 8
    x = 9
    Lazy low hanging fruit.

    Counting from 1 to 10 could turn into a problem for a programmer?

  7. #7
    Registered User
    Join Date
    May 2021
    Posts
    56
    Quote Originally Posted by ghoul View Post
    Lazy low hanging fruit.

    Counting from 1 to 10 could turn into a problem for a programmer?
    Thanks I understood, When condition is true, the value of variable is being skipped in the first example

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    869
    Quote Originally Posted by Rahul11 View Post
    There is no detailed example given in the book, only theoretical explanation which was not enough for me to understand. That was the reason I wrote my own code
    If the "book" you are using to learn C does not have a detailed explanation of such a simple concept such as "continue", then you need a much better book, such as one of the three I have specified earlier!

    What "Book" are you actually using???

  9. #9
    Registered User
    Join Date
    Oct 2019
    Posts
    46
    Quote Originally Posted by Rahul11 View Post

    Thanks I understood, When condition is true, the value of variable is being skipped in the first example
    That is correct.

    With a continue statement you instruct the program to skip any of the code that would have been executed during the current run of the most adjacent loops.

    Basically, it passes control back to the closest 'for' or 'while' loop. You will probably also come across is its cousin 'break' which stops execution of the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Explain this IF statement, please.
    By chronicinquiry in forum C Programming
    Replies: 2
    Last Post: 11-07-2020, 06:17 AM
  2. Replies: 6
    Last Post: 11-29-2019, 12:22 PM
  3. continue statement
    By Ganoosh in forum C++ Programming
    Replies: 9
    Last Post: 07-16-2005, 03:52 PM
  4. continue statement causing an unexpected error
    By drb2k2 in forum C++ Programming
    Replies: 2
    Last Post: 04-15-2003, 06:46 AM
  5. continue statement
    By minesweeper in forum C++ Programming
    Replies: 4
    Last Post: 12-08-2002, 05:07 AM

Tags for this Thread