Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungKI half beim Finden: iOS 27 schließt mehr als 100 Sicherheitslücken(21.09.2026 um 06:00 Uhr)
Sichere ProgrammierungWhat Is Rowhammer? How Can Repeated Memory Access Flip Bits in RAM?(21.09.2026 um 07:12 Uhr)
Sichere Programmierungnpm publish Ignores .gitignore: The .npmignore Override Rule(21.09.2026 um 07:15 Uhr)
Sichere ProgrammierungAphelion Editor - A free node-based video / VFX editor(21.09.2026 um 07:21 Uhr)
Sichere ProgrammierungGovernance Attack Surface Review: OKX(21.09.2026 um 07:31 Uhr)
Sichere ProgrammierungJSM Portal Request Create Property Panel Submit(21.09.2026 um 07:34 Uhr)
Reverse Engineeringsearch instructions assembly easy (X86,RISCV,AARCH64,etc)(20.09.2026 um 15:44 Uhr)
Sichere ProgrammierungKI half beim Finden: iOS 27 schließt mehr als 100 Sicherheitslücken(21.09.2026 um 06:00 Uhr)
Sichere ProgrammierungWhat Is Rowhammer? How Can Repeated Memory Access Flip Bits in RAM?(21.09.2026 um 07:12 Uhr)
Sichere Programmierungnpm publish Ignores .gitignore: The .npmignore Override Rule(21.09.2026 um 07:15 Uhr)
Sichere ProgrammierungAphelion Editor - A free node-based video / VFX editor(21.09.2026 um 07:21 Uhr)
Sichere ProgrammierungGovernance Attack Surface Review: OKX(21.09.2026 um 07:31 Uhr)
Sichere ProgrammierungJSM Portal Request Create Property Panel Submit(21.09.2026 um 07:34 Uhr)
Reverse Engineeringsearch instructions assembly easy (X86,RISCV,AARCH64,etc)(20.09.2026 um 15:44 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

31. C# (continue)

Reagiere als Erste:r — dein Feedback zählt!

0. The Essence of continue

continue immediately stops the current iteration and moves to the next one.

Important distinction:

  • ❌ It does not terminate the loop
  • ⭕ It only skips the current cycle

Execution jumps directly to the next iteration.

1. Simple Example — Skip Multiples of 3

Goal

Print numbers from 0 to 20, but exclude multiples of 3.

Full Runnable Code

using System;

class Program
{
    static void Main()
    {
        for (int i = 0; i <= 20; i++)
        {
            if (i % 3 == 0)
            {
                continue;
            }

            Console.WriteLine(i);
        }

        Console.WriteLine("Loop finished.");
        Console.ReadKey();
    }
}

Execution Flow

Example when i = 6

6 % 3 == 0 → true
continue executes
Console.WriteLine(i) is skipped
loop proceeds to i++

So 6 is not printed.

The loop continues normally afterward.

Without continue

The same logic can be written like this:

if (i % 3 != 0)
{
    Console.WriteLine(i);
}

Both work.

However, the continue version follows a specific style:

Filter out exceptional cases first,
then let the normal logic run below.

This style is often called a guard clause approach.

2. break vs continue

Keyword Current iteration Entire loop
break Stops Stops
continue Stops Continues

break Example

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

    Console.WriteLine(i);
}

Output:

0
1
2
3
4

The loop stops completely.

continue Example

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

    Console.WriteLine(i);
}

Output:

0
1
2
3
4
6
7
8
9

Only 5 is skipped.

3. Practical Scenario — Protecting User Input

Consider this code:

int number = int.Parse(input);

If the user enters:

abc

The program throws:

FormatException

and crashes.

Goal

We want to:

  • Ignore invalid input
  • Ask again
  • Prevent program crashes

Safe Example Using continue

using System;

class Program
{
    static void Main()
    {
        string input;
        int userNumber = 0;

        do
        {
            Console.WriteLine("Enter number > 10 or type 'stop':");
            input = Console.ReadLine();

            if (input == "stop")
            {
                break;
            }

            bool isNumeric = true;

            foreach (char c in input)
            {
                if (!char.IsDigit(c))
                {
                    isNumeric = false;
                    break;
                }
            }

            if (!isNumeric)
            {
                Console.WriteLine("Invalid input.");
                continue;
            }

            userNumber = int.Parse(input);

        } while (userNumber <= 10);

        Console.WriteLine("Loop ended.");
        Console.ReadKey();
    }
}

Execution Flow Analysis

User enters "abc"

isNumeric = false
continue executes
int.Parse is skipped
loop starts next iteration

Result:

The program does not crash.

4. Why break Did Not Cause an Initialization Error

Key idea:

break completely exits the loop.

Example:

if (input == "stop")
{
    break;
}

The code after this inside the loop is never executed.

So the compiler does not require all variables to be assigned.

But continue Is Different

continue jumps back to the loop condition.

Therefore:

All possible paths must still maintain valid variable states.

Otherwise the compiler will report errors.

Structural Difference

From a control-flow perspective:

break

exit loop
continue execution outside

continue

skip current iteration
return to loop condition
start next iteration

5. When continue Is Useful

Use continue when:

  • many invalid conditions exist
  • you want to filter early
  • you want cleaner control flow

Typical pattern:

if (invalidCondition)
{
    continue;
}

// main logic below

This separates:

  • exception cases
  • normal execution

Final Summary

break exits the entire loop.
continue skips only the current iteration and moves to the next one.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten 31. C# (continue)

Thematisch verwandte Begriffe: continue · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-94109 | openEQUELLA versions before 2026.1.0 contain a remote code execution vul…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick