Repeat Loop in C

0

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

    
asked by anonymous 29.09.2017 / 02:25

4 answers

8

Esoteric strategies

The @Ruy Neto answer is about the principal. But the question is not limited to practical resolutions, it reminds one more exercise. Since it's 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 to in the code 1 . In case, for VOLTA , the command soon after saudacoes_oi()

1: in the case of C, and in some other languages that support goto , the "place indicated in the code" is not totally arbitrary; it must be within the same function. If I'm not mistaken, in Pascal,% w /% could go to any arbitrary point in the system, leaving the code stream more macaronic than "normal"; luckily C does not allow this, has the constraint of being in the same function

Direct recursion of function, 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 operations available in the menu. Note that here I always run the entire function for, so choose if I should make the call, in the tail, of the function.

Indirect recursion of function, 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 goto or the function itself.

Direct recursion of the function, condition in the 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, close the loop.

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

tchau calls trata_operacao_chama_funcao_menu calling funcao_menu .

Recursion using system calls

For this, I'm going to remove the trata_operacao_chama_funcao_menu greeting 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;
}
    
29.09.2017 / 05:09
5

Hello, you can use any loop repetition for an infinite loop, follow 3 examples:

1 - As for.

for(;;){
//comandos
}

2 - With while.

while(true){
//comandos
}

3 - With while.

do{
//comandos
}while(true);

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

break;
    
29.09.2017 / 02:29
1

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

int opcao = 0;

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

I advise you to use the while, because it will execute the loop code at least once regardless of the condition.

    int end;
    do{

    printf("\n\nDigite 1 para continuar ou digite qualquer outro ");
    printf("numero para sair\n\n");
    scanf("%d", &end);

    }while (end == 1);
    
02.10.2017 / 03:01