A small question

This is a discussion on A small question within the C Programming forums, part of the General Programming Boards category; I'm a bit of a novice programmer in C and C++ right now, having learnt programming for about 5-6 months.... ...

  1. #1
    Novice
    Join Date
    Apr 2004
    Posts
    2

    A small question

    I'm a bit of a novice programmer in C and C++ right now, having learnt programming for about 5-6 months....
    I want to ask how to stay in the execution mode for C for as long as you wish.....instead of running it once and then stopping....I guess the obvious answer to that would be ask the user to give an EOF (End-Of-File) character to stop the execution...or use counter controlled methods....

    Is there any other method??

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    int main (void)
    {
      int termflag=0;
      while ( !termflag )
      {
      /* Do stuff and have termflag change to 1 once the user does something to indicate they want to quit */
      }
      return 0;
    }

  3. #3
    Novice
    Join Date
    Apr 2004
    Posts
    2
    Oh but isn't that another way of counter-controlled looping??

    Thanks anyway. I'll try it.

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    You are looking for this i guess...
    Code:
     
     
    int main(void)
    {
     
    int count=10;
    int i;
     
    for(i=0;i<10;i++)
    {
     
    // do your stuff here .. will run for 10 times
     
    }
     
     
    return 0;
    }

    but if you want the user to decide when to exit you can have something like

    Code:
    int main(void)
    {
    int option=0;
     
    while(option!=5)
    {
     
    // do your stuff
     
    scanf("%d",option);
     
     
    }
     
     
    return 0;
    }

    so the code will run untill user enter 5 when asked for input..

  5. #5
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    or you could use an infinite for loop and break it in a specified condition :
    Code:
    #include<stdio.h>
    
    int main(){
          for( ; ; ){
              ...
              if(<condition>)
                       break;         /* breaks the loop */
              ...
            }
    return 0;
    }
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Quote Originally Posted by Brain Cell
    or you could use an infinite for loop and break it in a specified condition :
    Code:
    #include<stdio.h>
    
    int main(){
          for( ; ; ){
              ...
              if(<condition>)
                       break;         /* breaks the loop */
              ...
            }
    return 0;
    }
    Yuck!
    Code:
    #include<stdio.h>
    
    int main()
    {
        while(1)
        {
            ...
            if(<condition>)
                break;         /* breaks the loop */
            ...
        }
        return 0;
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    WaltP :
    Will you explain why is it "yuck"?? if you don't know why then you shouldn't have commented on it in the first place
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >WaltP:
    >Will you explain why is it "yuck"??
    I'm inclined to agree. An empty for loop is my preference for an infinite loop because I don't know of a compiler that gives me a warning when I try to use it. This is not so for your suggested while loop using a constant. And of course, since both are valid C, the only issue is one of style. Unless you know something that I don't of course.
    My best code is written with the delete key.

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >Unless you know something that I don't of course.
    Like that's gonna happen. Prelude knows everything about C/C++.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  10. #10
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    You may want to use some kind of sleep() variant to conserve processor time.

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Quote Originally Posted by Brain Cell
    WaltP :
    Will you explain why is it "yuck"?? if you don't know why then you shouldn't have commented on it in the first place
    Of course I know why. What makes you think I don't? I posted code that shows what I would use if I must, leaving you to compare and see if you can figure out why.

    But my reasoning is a for loop is designed to start a loop indicator, process a comparison, and change the loop indicator. It's used to loop thru a number of specified iterations.

    A while loop on the other hand is designed with a simple comparison in mind. And therefore less overhead (conceptually) than a for loop for an infinte loop.

    If they were meant to be interchanagable, why have while loop at all? The designers of C would simply make you use
    Code:
    for ( ; i < 10; )
    instead. It is functionally equivalent to
    Code:
    while (i < 10)
    Therefore
    Code:
    while (1)  // instead of
    for (;;)
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    To find the answer we have to see the assembly equalent for both....... any one interested in finding out(i dont knwo how assembly)..

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Using GCC's -S option to convert the following two programs to assembly:
    #1
    Code:
    int main(void)
    {
      for(;;);
      return 0;
    }
    #2
    Code:
    int main(void)
    {
      while(1);
      return 0;
    }
    The only difference was that #2 had an extra label. It kinda like in C if you had:
    Code:
    label 1:
    label 2:
    goto 2;
    Once I turned it into object code, number 2 was one byte larger then number 1.

  14. #14
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    K&R2 uses for(;;)

    as in

    #define forever for(;;) /* infinite loop */

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,320
    I remember reading about while (1) having a redundant test for the true value, while for (;;) was automatically recognised as an intentional infinite loop, at least on some compilers.

    Based on the publishing date of the book (early 1990s, I think), I thought it only applied to old compilers, and now both would be equivalent.
    Funny that GCC didnt take that into account.

    Anyway, partially because of what I read early on, I prefer using the for (;;) variant.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  2. Hi all, I have a small question...
    By Pandora in forum Windows Programming
    Replies: 3
    Last Post: 03-16-2003, 05:21 AM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM
  4. A small Question
    By CodeJerk in forum C++ Programming
    Replies: 2
    Last Post: 11-20-2002, 08:08 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

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