Return to condition start when prompted

Asked

Viewed 187 times

1

I can not use function, I wanted to know how to return to a certain line of code when the number 0 is pressed, example:

system(color 7);
printf("Teste");
printf("Teste01");
printf("Pressione 0 para reiniciar");
scanf("%d", &jog);

Now how do I get it to type 0, the run back to the line of system()? With function would be easy, but I can not use so as I will do?

3 answers

3


Although not the best option, one option is the goto.

voltaAqui:
... código
goto voltaAqui;
  • Why wouldn’t it be the best option? goto is flow control :P

  • 1

    @Marciano.Andrade I believe that creating functions is better :)

  • I also think creating function makes the code cleaner etc. But that’s what I wanted, thank you

2

You can place the code inside a loop and check whether the variable’s value jog is different from 0. If it is, you interrupt the loop:

int jog;

while(1){

  // código...

  printf("Pressione 0 para reiniciar");
  scanf("%d", &jog);

  if(jog) break;
}
  • 1

    Usually I use this one, without the break. But it was going to leave the bigger code, the goto came well to me

2

for (;;) {
    /* codigo */
}

or

while (1) {
    /* codigo */
}

or

do {
    /* codigo */
} while (1):

Browser other questions tagged

You are not signed in. Login or sign up in order to post.