Loop of repetition in C

Asked

Viewed 454 times

0

Hello, I’m a beginner in programming and I have a question: I’m making a program that has a menu and a registration to be performed by the user. I would like to know how to make the menu of the program always appear after finishing the registration, instead of finishing the program. I will use the while or while?

3 answers

8

Esoteric strategies

To Reply from @Ruy Neto deals with the main. But the question is not limited to practical resolutions, it resembles another exercise. Since it is an exercise, let’s exercise?

goto

int main() {
  int op;

  goto INICIO;

SAIDA:
  saudacoes_tchau();

  return 0;
INICIO:
  saudacoes_oi();
VOLTA:

  op = le_operacao();
  trata_operacao(op);
  if (op) {
    goto VOLTA;
  } else {
    goto SAIDA;
  }

}

The goto will force an unconditional jump to some place pointed in the code1. In case, to VOLTA, the command immediately after saudacoes_oi()


1: in the case of C, and in some other languages that support goto, the "place pointed in the code" is not totally arbitrary; it needs to be within the same function. If I am not mistaken, in Pascal, the goto could go to any arbitrary point in the system, leaving the flow of code more macaronic than "normal"; fortunately C does not allow it, has the restriction of being in the same function


Direct function recursion, tail condition

void funcao_menu() {
  int op;
  op = le_operacao();
  trata_operacao(op);

  if (op) {
    funcao_menu();
  }
}

int main() {
  saudacoes_oi();
  funcao_menu();
  saudacoes_tchau();
  return 0;
}

Recursively calling the function to handle the available operations in the menu. Note that here I always run the whole function to, then choose whether to call, on the tail, from the function.

Indirect function recursion, function pointer

void funcao_menu() {
  void (*proxima_chamada)();
  int op;

  proxima_chamada = &saudacoes_tchau;
  op = le_operacao();
  trata_operacao(op);

  if (op) {
    proxima_chamada = &funcao_menu;
  }
  (*proxima_chamada)();
}

int main() {
  saudacoes_oi();
  funcao_menu();

  return 0;
}

Now, the choice is made between calling the tchau or the function itself.

Direct function recursion, condition in argument

void funcao_menu(int old_op) {
  int op;
  if (old_op) {
    op = le_operacao();
    trata_operacao(op);

    funcao_menu(op);
  }
}

int main() {
  saudacoes_oi();
  funcao_menu(1);
  saudacoes_tchau();
  return 0;
}

Here, who defines whether there will be recursion or not is the function parameter. If a true argument is passed, it is executed; otherwise it closes the loop.

Indirect recursion to function, mutually recursive functions

void funcao_menu();

void trata_operacao_chama_funcao_menu(int op) {
  if (op) {
    trata_operacao(op);
    funcao_menu();
  }
}

void funcao_menu() {
  trata_operacao_chama_funcao_menu(le_operacao());
}

int main() {
  saudacoes_oi();
  funcao_menu();
  saudacoes_tchau();
  return 0;
}

trata_operacao_chama_funcao_menu flame funcao_menu what call trata_operacao_chama_funcao_menu.

Recursion using system calls

For this, I will remove the salutation from oi, because it’s hard to define when it should be done.

int main(int argc, char **argv) {
  int op = le_operacao();

  if (op) {
    trata_operacao(op);
    return system(argv[0]);
  }
  return 0;
}

5

Hello, you can use any repeat loop to make an infinite loop, follow 3 examples:

1 - Com for.

for(;;){
//comandos
}

2 - Com while.

while(true){
//comandos
}

3 - With do while.

do{
//comandos
}while(true);

To exit the loop, use the following command with a conditional:

break;
  • Missed with goto and with recursion

  • 1

    That is why I have said three examples, dear colleague.

  • then I will write more esoteric methods for loops of repetition

  • 1

    Please do it now.

  • Thanks for the tip !!

  • no break has to put a condition too?

  • Could you send the code to take a look and help me ?

  • Some parts are not working

  • Break; for a loop at the time it is executable, it is common for you to use a conditional to execute it. Example: read infinite names until user type "#", you will need a break along with a conditional.

  • Post your questions on the site for other people to see and help you, not necessarily me or not.

Show 5 more comments

1

In this case, the ideal is to use do-while because you want to show the menu before asking for the user option:

int opcao = 0;

do {
   printf("\n0 - SAIR");
   printf("\n1 - Op 1");
   ...
   scanf("%d", &opcao);
} while (opcao != 0);

Browser other questions tagged

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