Thread: looping

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Unhappy looping

    I can't get my program to loop properly. Here's the code:

    #include <stdio.h>

    main()

    {
    char a;

    printf("Enter a letter\n");

    for ( ; ; )

    scanf("%c",&a);

    printf("\nThe decimal value of the character you entered is %d\n\n",a);


    }


    What am I doing wrong?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Perhaps something more like this is what you want:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char a;
      
      printf("Enter a letter\n");
      
      for ( ; ; ) {
        scanf("%c%*c",&a);
        printf("\nThe decimal value of the character you entered is %d\n\n",a);
      }
      
      return 0;
    }
    Multiline loops must be surrounded by a block.

    -Prelude
    My best code is written with the delete key.

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    i dont want to hijack this thread but i always wondered what
    that
    Code:
    for ( ; ; )
    statement actually does.
    anyone?

    thanks

  4. #4
    Evil Sock Puppet MadHatter's Avatar
    Join Date
    Nov 2002
    Posts
    176
    it's an infinate loop. you can leave out any of the paremetes(?) of a for statement.. if you have nothing to stop the loop (the second paremeter) then it never stops.. putting the other two in is then pointless, so you do for( ; ; ) for an infinate loop.
    If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >anyone?
    It's an infinite loop, there's no condition to stop it and no need for initialization or modification of a counter.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Why does the scanf statement have to be scanf("%c%*c",&a); instead of scanf("%c",&a);?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why does the scanf statement have to be scanf("%c%*c",&a); instead of scanf("%c",&a);?
    Because otherwise you'll loop twice for each letter, once for the letter and once for the return key. The %*c flag tells scanf to read a single character and discard it. Since we know it's the return key we can safely get rid of it for the sake of pretty output.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Originally posted by MadHatter
    it's an infinate loop. you can leave out any of the paremetes(?) of a for statement.. if you have nothing to stop the loop (the second paremeter) then it never stops.. putting the other two in is then pointless, so you do for( ; ; ) for an infinate loop.
    Yes, but the real question is: which is better? while(1) or for( ; ; )? I'm sure there will be a huge debate about this on the General Discussions board.

  9. #9
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    ...

    Yes, but the real question is: which is better? while(1) or for( ; ; )? I'm sure there will be a huge debate about this on the General Discussions board.
    for(; ; ) = We haven't any condition here, so I don't think that the computer every time check the condition.

    while(1) = We have a condition here, so the computer will check always the condition (or not?).


    I really don't no if in this case:
    Code:
    for(; ; ) {
        printf("Blah");
        printf("\nBleh");
       a = b*b;
    }
    The computer check any condition....
    He just jump to the start, right?

    I don't think that for(; ; ) is better than while(1) and vice-versa.

  10. #10
    Registered User
    Join Date
    Dec 2002
    Posts
    27
    while(1) is 8 letters
    for( ; ; ) is 7 letters...

    "Can i really learn this? If i cant, why bother?"

  11. #11
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    I think that it takes around a nanosecond shorter for me to understand what while(TRUE) is doing, than for(; so I use the while statement. It look better too.

    Originally posted by C-learning
    while(1) is 8 letters
    for( ; ; ) is 7 letters...

    You had to hit 10 keys to write for( ; ; ) and only 8 to write while(1)


  12. #12
    Registered User
    Join Date
    Dec 2002
    Posts
    27
    Thats true.
    But i was never good at math...

    "Can i really learn this? If i cant, why bother?"

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >which is better? while(1) or for( ; ; )?
    It's a matter of preference, but

    while ( 1 )

    will often cause a warning about a constant loop condition while

    for ( ; ; )

    doesn't have this problem.

    -Prelude
    My best code is written with the delete key.

  14. #14
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    No, No, No..

    You had to hit 10 keys to write for( ; ; ) and only 8 to write while(1)
    you forgot the SHIFT
    Look, you had to hit 12 keys hehe

    SHIFT+9= (
    SHIFT+0 = )


  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Enough of the pointless comments. If you want this thread reopened, PM me or another mod...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 06:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM