Thread: another question

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    51

    another question

    another example to work out in my book. here is what i got so far.

    Here is a simple way to test the effect of a continue statement in the body of a for loop. What is printed?

    I ran the below code and it is fast hard to see... i think it is printing 324.... but when i replace the continue statement with Break instead of continue to take a look i see 124... The book i am working with sucks bad... can someone please explain this. I dont understand why it did not print the 3 with the break command in there.

    For (putchar('1'); putchar('2'); putchar('3')) {
    putchar('4');
    continue;
    putchar('5');




    Code:
    #include <stdio.h>
    int main(void)
    {
    	for (putchar('1'); putchar('2'); putchar('3')) {
    		putchar('4');
    		continue;
    		putchar('5');
    	}
    }

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    The 3 didn't get printed because that's being done in the incrementation part of the for loop and you break the loop before finishing even one iteration.
    If you want to catch the output print it to a file instead, or just Ctrl-C the program after having it alive for a short while.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Still not getting it... i see at the beginning it would print to the screen

    1, 2, it skips 3 for some reason , prints 4.... then continue in a loop,,,

    i think it is printing 243 after stopping after a while.. or is it 324 because i see reminants of the beginning implementation part..

    i need a caveman to explain this to me..

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Modify the program a little to see what's going on
    Code:
    #include <stdio.h>
    int main(void)
    {
        int cou = 0;
        for (putchar('1'); putchar('2'); putchar('3')) {
            putchar('4');
            cou++;
            if ( cou > 10 ) break;
            continue;
            putchar('5');
        }
    }
    Kurt

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    It's like this:

    A for loop has four parts, the initialization, the condition, the update, and the body.
    Code:
    for(initialization; condition; update) {
      // the body
    }
    The way it works is, first it runs the initialization. Then it checks the condition. Then it runs the body, updates the variables, and checks the condition again. If it's still true, it repeats. It only initializes the first time. There for, when you have:
    Code:
    	for (putchar('1'); putchar('2'); putchar('3')) {
    		putchar('4');
    		continue;
    		putchar('5');
    	}
    the first time through, it outputs the 1. That's the initialization. Then it outputs the two. That's the condition. Then it outputs the 4. That's the body. Then, it hits the continue, which sends it to the end brace. It outputs the 3. That's the update. And then it repeats outputting the condition, body, and update. Your expected output would look something like:
    Code:
    124324324324324324324324
    ...and so on. Now, if you put a break in there instead of continue. It outputs the initial 124, and instead of going to the end of the loop and updating, it just exits the loop completely. Leaving you with just the initial 124.
    Last edited by SlyMaelstrom; 04-15-2006 at 07:06 AM.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Now if only the book explained it as clearly as you did. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM