What’s the point of continuing flow control?

Asked

Viewed 159 times

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 execution the continue command will move the execution to the next loop iteration without executing lines of code after continue.

Exit:

Macoratti

Peter

  • How does the continue?
  • In which situations its use is useful?
  • Because the output does not print the name Miriam?
  • 2

    I think it is dup even if it is another language, because it is identical: https://answall.com/q/80589/101

2 answers

3


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

In which situations its use is useful?

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

Because the output did not print the name Miriam?

Precisely because when nome is equal to Miriam the code says go to the next loop, ignoring everything that comes after.


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
}

3

How the continue works?

Simply ignore the current iteration.

In which situations its use is useful?

Much depends on the business rule.
You can check out more here.

Because the output did not print the name Miriam?

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

Browser other questions tagged

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