why not 1,2,3,4,5 is output when I expected 1,2,3,4,5,6 for below codes

This is a discussion on why not 1,2,3,4,5 is output when I expected 1,2,3,4,5,6 for below codes within the C Programming forums, part of the General Programming Boards category; The below code results only 1,2,3,4,5 as output. I wonder why not "6" as it is written if==6 http://s2.postimage.org/ywhcikrrd/c_example.gif...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    20

    why not 1,2,3,4,5 is output when I expected 1,2,3,4,5,6 for below codes

    The below code results only 1,2,3,4,5 as output.

    I wonder why not "6" as it is written if==6

    http://s2.postimage.org/ywhcikrrd/c_example.gif
    Last edited by learning_grc; 09-11-2011 at 08:44 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    How about you actually post your code...

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Actually post your code here using [ code ] tags.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Double Header... Hi Andrew!

  5. #5
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    The reason the code doesn't print "6" is that it breaks out of the loop when i==6, and the break statement is before the printf.
    Code:
    while(!asleep) {
       sheep++;
    }

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by CommonTater View Post
    Double Header... Hi Andrew!
    Lol....I know, I was waiting for tabstop or somebody to swing in and make it a trifecta.


    EDIT: Oh, the link has been fixed. Did anyone else click on it? That is a waste of someone's time....
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Not only that, but you've got an animated trace of the flow of program control through the code. So just watch to the end and you'll see what happens!

    EDIT: Sorry AH and CT, was slow.

    ALSO EDIT: I don't know why I'm initialing everybody tonight either.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by tabstop View Post
    Not only that, but you've got an animated trace of the flow of program control through the code. So just watch to the end and you'll see what happens!
    Now you are asking for way too much.
    Quote Originally Posted by tabstop View Post
    EDIT: Sorry AH and CT, was slow.

    ALSO EDIT: I don't know why I'm initialing everybody tonight either.
    I will let it slide this time TP.......
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Did I recognize a DOS IDE there? And what compiler uses that?

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    Really nice .gif you have there. Did you bother to look at it?

    In short, look at your

    Code:
    do
    {
      if (i==6)
        break;
      printf(//what you are printing);
      i = i + 1
    }
    The question is, what are you telling the program to do at the
    Code:
    if (==6)
    point?

    In C, if you are including 2 or more lines with a if statement you need the open and close brackets surrounding the body of code. Yes C will pick up the first line, but that is it.

    What I am seeing is you told your program to break out of the do/while loop when it hits 6. IF you want it to print 6, then you simply need to whisper into the ear of your program and ask nicely. The relationship between man and C is a very special one. C will do everything you tell it to do, no more no less.

    A great way I've found to cycle though code that leaves no question to what belongs to what, is like

    Code:
    #include <stdio.h>
    
    int main()
    {
      int i;
      for (i=0; i<6; i++)
      {
        printf("WAIT FOR IT....\n");
      }
    
      if (i == 6)
      {
        printf("I am a C programming GOD!\n");
      }
    
      printf("I made you read a silly a message for a total of (%d) times!\n", i);
      return 0;
    }
    AS you can see the for() and if() both contain 1 line code commands, but there is no question where that line of code belongs.

    If I were to change for() to [code]{
    int i;
    for (i=0; i<6; i++)
    printf("WAIT FOR IT....\n");
    printf("Hellow World!\n");
    [code] The "Hellow World!" will print out only once.
    Last edited by Strahd; 09-11-2011 at 09:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array output not as expected
    By Futomara in forum C Programming
    Replies: 26
    Last Post: 11-04-2010, 12:24 PM
  2. zip codes
    By missny in forum C Programming
    Replies: 10
    Last Post: 09-16-2005, 03:39 AM
  3. non expected output
    By c++.prog.newbie in forum C Programming
    Replies: 2
    Last Post: 09-27-2004, 06:41 PM
  4. VK codes
    By Hunter2 in forum Windows Programming
    Replies: 10
    Last Post: 08-24-2002, 10:58 AM
  5. converting scan codes to ascii codes
    By stupid_mutt in forum C Programming
    Replies: 11
    Last Post: 01-25-2002, 04:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21