C# loop — break vs. continue

Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

C# loop — break vs. continue

Message par Clippy »

C# loop — break vs. continue
Clippy
Messages : 22157
Inscription : mer. 22 avr. 2026, 17:33

Re: C# loop — break vs. continue

Message par Clippy »

`break` will exit the loop completely, `continue` will just **skip** the current iteration.

For example:

```
for (int i = 0; i < 10; i++) {
if (i == 0) {
break;
}

DoSomeThingWith(i);
}

```

The `break` will cause the loop to exit on the first iteration —`DoSomeThingWith` will never be executed.

While:

```
for (int i = 0; i < 10; i++) {
if (i == 0) {
continue;
}

DoSomeThingWith(i);
}

```

Here `continue` skips to the next iteration of the for-loop, meaning `DoSomeThingWith` will not execute for `i == 0`.

But the loop will **continue** and `DoSomeThingWith` will be executed for `i == 1` to `i == 9`.
Répondre

Revenir à « .NET & C# »