Function prints while list

Asked

Viewed 160 times

2

How do I do this function with the loop while?

void imprime(lista* recebida){
 lista* aux;
  for(aux=recebida; aux!=NULL; aux=aux->prox){
   printf("Informacao %d\n",aux->info);
  }
}
  • Any reason for this? It’s not good like this?

  • I intend to do with while to understand well.

  • Wow what happened? Gone @bigown

  • 2

    ?!?!?! What’s gone?

3 answers

5


You should use what is most suitable for each situation. The for may seem the same as a while, and it’s until you get to use continue, there is a difference because in the for the statement which must be executed in each repetition will always be executed no matter what happens, while in the while one continue will cause it to not run, and probably not what you want. In this case you do not have continue then it’ll work.

The for is composed of three parts, the first is an initialization, so just put it before everything, the second is the condition and it is she who should be placed in the while, and finally the last is the "step", which must be repeated every time and you must put inside the while in the last line of the execution block.

void imprime(lista* recebida) {
    lista* aux = recebida;
    while (aux!=NULL) {
        printf("Informacao %d\n",aux->info);
        aux=aux->prox;
    }
}

I put in the Github for future reference.

You have other questions on the subject:

1

The for is a type of tie that is more convenient than the while, but this is the do while are more primitive than the first.

As you can see, the for is composed of four "parts":

for («inicialização»; «teste»; «pós-passo») { «corpo do laço» }

The for then executes «initialization» once, and then checks the result of «test»: if non-zero, executes «body of the loop» and «post-step» once each, in this order, and will check again the result of «test», running «body of the loop» and «post-step» until «test» zero. Then he continues with the execution after the end of the for.

Using while, therefore we would have the following equivalent structure:

«inicialização»; while («teste») { «corpo do laço»; «pós-passo» }

Using goto:

«inicialização»;
label1:
if (!«teste») goto label2;
«corpo do laço»;
«pós-passo»;
goto label1;
label2:

Why use for, then if while serve? Why the programmer, when using while, have to be careful to ensure that you write the «post-step», or runs the risk of causing an infinite loop, since the poststep is that it usually modifies the program’s state so as to ensure that the loop usually ends. In the for you write as soon as you start writing the structure and therefore tend not to forget this detail. But sometimes the loop body itself is already in charge of ensuring that we won’t be inspecting the same item forever.

1

Syntax:

while (condição)
{
Instrução ou bloco de instruções;
}

Performs the repetition of an instruction block while a condition is true.

Source: http://linguagemc.com.br/o-comando-while-em-c/

In your case you could do something like this:

void imprime(lista* recebida){
     lista* aux;
     aux=aux->prox;

     while(aux!=NULL){
       printf("Informacao %d\n",aux->info);
       aux=aux->prox;
     }

 }

Browser other questions tagged

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