Fox-3
Fox-3

Reputation: 483

how to jump back to the top of a program

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
//
//
//


}

Upvotes: 1

Views: 39801

Answers (6)

Rahul Vashisth
Rahul Vashisth

Reputation: 81

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

Upvotes: -2

pablo1977
pablo1977

Reputation: 4433

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;
  }

Upvotes: 0

CoderDake
CoderDake

Reputation: 1547

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
}

Upvotes: 0

uml&#228;ute
uml&#228;ute

Reputation: 31274

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;
}

Upvotes: 5

Lundin
Lundin

Reputation: 213513

int main (void)
{
  bool keep_looping = true;

  while (keep_looping)
  {
    // code

    if(done_with_stuff)
    {
      keep_looping = false;
    }
  }
}

Upvotes: 2

unwind
unwind

Reputation: 399753

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.

Upvotes: 2

Related Questions