1

I have a large block of code inside main and would like to return to the top of main and run through again if a certain variable in 'code' is true. How would I do this?

#include <stdio.h>

void main(void)
{

// if varable in code is true return to here



//code
//
//
//


}
Fox-3
  • 483
  • 1
  • 5
  • 11
  • 4
    `while - continue` loop? – Rohan Oct 03 '13 at 13:16
  • Just call `main();` again? (Watch out for infinite recursion) – Kninnug Oct 03 '13 at 13:17
  • 1
    `TOP:` ... `if (variable) goto TOP;` – technosaurus Oct 03 '13 at 13:18
  • @Kninnug explicitly calling `main()` is not allowed by the C standard. – Sneftel Oct 03 '13 at 13:19
  • 1
    @Ben calling `main` is allowed in C, but not in C++. But it doesn't look like a good design for this problem. – Yu Hao Oct 03 '13 at 13:21
  • `void main(void)` is not C standard, it should be `int main(void)` with `return 0`. – Adam Stelmaszczyk Oct 03 '13 at 13:22
  • @AdamStelmaszczyk That is not correct. [More info here.](http://stackoverflow.com/questions/5296163/why-is-the-type-of-the-main-function-in-c-and-c-left-to-the-user-to-define/5296593#5296593) – Lundin Oct 03 '13 at 13:25
  • 2
    `DOINGITWRONG:` ... `if(broken) goto DOINGITWRONG;` – Salgar Oct 03 '13 at 13:26
  • @Lundin Standard says *It shall be defined with a return type of int*. – Adam Stelmaszczyk Oct 03 '13 at 13:29
  • @AdamStelmaszczyk Read that link again. It does indeed say so, in a subclause of the chapter _hosted environment_. If your C program is not running in a hosted environment, it may return anything. – Lundin Oct 03 '13 at 13:30
  • @Lundin We don't know if his program will be running in a hosted environment or not. Please also notice that he is a beginner. I think it's better to advice him to write `int main(void)`. You also have done it in your answer ;) – Adam Stelmaszczyk Oct 03 '13 at 13:42
  • @AdamStelmaszczyk If you don't know, then you cannot give advise. There are countless of embedded systems beginners out there and there is no telling what system this person is using. – Lundin Oct 03 '13 at 13:45

6 Answers6

3
int main(void)
{
  int gotostart=0; 
  // if varable 'gotostart' in code is true return to here
  beginning:

  // code [...]

  if(gotostart)
    goto beginning;

  return 0;
}

as Armen has rightly pointed out, goto deserves some warning. the most popular ist Dijsktra's GOTO statements considered harmful, as it is somewhat counterproductive to structured programming.

a more structured approach would be:

int main(void)
{
  int gotostart=0; 

  do { // if varable 'gotostart' in code is true return to here

    // code [...]

  } while(gotostart)

  return 0;
}
umläute
  • 28,885
  • 9
  • 68
  • 122
  • This is a good answer but unless you warn against using goto and provide a more structured alternative, you will keep receiving downvotes – Armen Tsirunyan Oct 03 '13 at 13:19
  • 1
    There really is no good reason to suggest `goto` for this problem when there are perfectly good alternatives, also see [GOTO still considered harmful?](http://stackoverflow.com/questions/46586/goto-still-considered-harmful) – Shafik Yaghmour Oct 03 '13 at 13:20
  • The second solution will never execute the code in the loop. Either use a `do .. while` loop or initialize `gotostart` to `1` – Virgile Oct 03 '13 at 13:52
2

Remove the code from main() and put it in a function:

static void my_function(void)
{
  /* lots of stuff here */
}

Then just call it:

int main(void)
{
  my_function();
  if(condition)
    my_function();
  return 0;
}

This is way cleaner than using a loop, in my opinion, since the use case was not really "loop-like". If you want to do something once or twice, break it out into a function then call the function once or twice. As a bonus, it also gives you a great opportunity to introduce a name (the function name) for the thing that your program is doing, which helps make the code easier to understand.

unwind
  • 391,730
  • 64
  • 469
  • 606
2
int main (void)
{
  bool keep_looping = true;

  while (keep_looping)
  {
    // code

    if(done_with_stuff)
    {
      keep_looping = false;
    }
  }
}
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    Good, except a while/do-while loop only checks at the beginning/end of the loop, a goto can check at multiple points. Probably could use a continue/break example. – technosaurus Oct 03 '13 at 13:24
  • @technosaurus If you need to check at multiple points, something in your program design is likely flawed. In that case you should implement the program as a state machine (switch statement or array of function pointers). – Lundin Oct 03 '13 at 13:29
0

The easiest way to achieve what you are talking about would probably to use the while loop as mentioned but do this:

while(true){
    if(something){
     continue;
     } // then use break when you want to leave the loop all together
}
CoderDake
  • 1,497
  • 2
  • 15
  • 30
0

If your program repeats ever the same schema, then a while() loop is the best approach.
But if your program is a little cumbersome, perhaps you would prefer a goto statement, in order to jump to the label you desire:

  int main(void) {
            // Initial stuff

      jump_point:

           // Do more stuff

      if (some-condition)
           goto jump_point;

           // ... ...

      return 0;
  }

I think you should have to design your program in a way that a natural and clear loop is executed:

  int main(void) {

       while(terminate-condition-is-false) {

           // Do all your stuff inside this loop

      }

      return 0;
  }
pablo1977
  • 4,281
  • 1
  • 15
  • 41
-2

You can use a goto statement:

//Assume here is your starting point of your program
start: //we have declared goto statement for beginning


//Assume here is your ending point
goto start; //Here is that show go back to first position
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42