What is the purpose of flow control to continue? [duplicate]

4

I found this code explaining the flow control continue .

    string []  nomes = new string[] { "Macoratti", "Miriam", "Pedro"};
    foreach (string nome in nomes)
    {
        if (nome == "Miriam")
            continue;
        Console.WriteLine (nome);
    }
  

The continue command is also used in loops ( while, for, etc. ) when in   executing the continue command will move the execution to the next   iteration in the loop without executing the lines of code after continue.

Output:

  

Macoratti

     

Pedro

  • How does continue work?
  • In what situations is your usage useful?
  • Why does the output not print the name Miriam ?
asked by anonymous 24.05.2017 / 18:50

2 answers

3

It serves to control the flow of the loop. It makes the execution go to the next loop, that's all.

  

In what situations is its use useful?

When you intend to skip the rest of the code in the loop and move on to the next.

  

Why the output did not print the name Miriam?

Precisely because when nome is equal to Miriam the code tells you to go to the next loop, ignoring everything that comes later.

An example in JS, to be clear.

var numero = 0;
while(numero < 10)
{
    numero++;
    console.log("-"); // Sempre será impresso

    if(numero == 5) // Se numero = 5, vai pro próximo laço
        continue;

    console.log(numero); // Só será impresso quando numero for diferente de 5
}
    
24.05.2017 / 19:00
3
  

How does it continue?

Simply ignore the current iteration.

  

In what situations is its use useful?

It depends a lot on the business rule. Home You can check out more here .

  

Why the output did not print the name Miriam?

I did not print, just because in the iteration where the name is equal to Miriam, it uses continue that jumps to the next iteration, in this case, Pedro.

    
24.05.2017 / 19:02