Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
YouTube Security VideosAnonymous Official: I'm begging you to understand this..(20.09.2026 um 21:30 Uhr)
Sichere ProgrammierungHow to Monitor Cron Jobs with a Simple HTTP Health Check(20.09.2026 um 23:14 Uhr)
Sichere ProgrammierungWhy my builds don't run on my laptop(20.09.2026 um 23:15 Uhr)
Sichere ProgrammierungDesigning offline-first when there's no server(20.09.2026 um 23:16 Uhr)
Sichere ProgrammierungSearching for Better Game Recommendations with Jev(20.09.2026 um 23:19 Uhr)
Linux Tipps & HardeningUbuntu 26.10 stops low memory from killing your desktop session(20.09.2026 um 19:55 Uhr)
YouTube Security VideosAnonymous Official: I'm begging you to understand this..(20.09.2026 um 21:30 Uhr)
Sichere ProgrammierungHow to Monitor Cron Jobs with a Simple HTTP Health Check(20.09.2026 um 23:14 Uhr)
Sichere ProgrammierungWhy my builds don't run on my laptop(20.09.2026 um 23:15 Uhr)
Sichere ProgrammierungDesigning offline-first when there's no server(20.09.2026 um 23:16 Uhr)
Sichere ProgrammierungSearching for Better Game Recommendations with Jev(20.09.2026 um 23:19 Uhr)
Linux Tipps & HardeningUbuntu 26.10 stops low memory from killing your desktop session(20.09.2026 um 19:55 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Part 4: Control Flow in C#

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

Control flow in a program determines the order in which instructions are executed. In C#, these decisions and repetitions are managed with specific structures that allow you to create dynamic and flexible programs. In this lesson, we will explore the main control flow structures: decisions and loops.

1. Decision Structures

Decision structures allow the program to execute different blocks of code depending on a condition. The most common ones in C# are if, else if, and else.

1.1 if

The if block evaluates a condition and executes the code inside the block if the condition is true.

int number = 10;
if (number > 5)
{
    Console.WriteLine("The number is greater than 5.");
}

1.2 else if

This is used to evaluate additional conditions if the initial condition is not met.

int number = 5;
if (number > 10)
{
    Console.WriteLine("The number is greater than 10.");
}
else if (number == 5)
{
    Console.WriteLine("The number is equal to 5.");
}

1.3 else

The else block is executed if none of the previous conditions are met.

int number = 3;
if (number > 10)
{
    Console.WriteLine("The number is greater than 10.");
}
else if (number == 5)
{
    Console.WriteLine("The number is equal to 5.");
}
else
{
    Console.WriteLine("The number is not greater than 10 nor equal to 5.");
}

2. Loop Structures

Loops or repetition structures allow you to execute a block of code multiple times, making repetitive tasks easier.

2.1 for

The for loop is ideal when you know the exact number of iterations.

for (int i = 0; i < 5; i++)
{
    Console.WriteLine($"Iteration {i}");
}

2.2 while

The while loop executes the code as long as the condition is true.

int counter = 0;
while (counter < 5)
{
    Console.WriteLine($"Counter: {counter}");
    counter++;
}

2.3 do-while

Similar to while, but it ensures that the block of code is executed at least once.

int counter = 0;
do
{
    Console.WriteLine($"Counter: {counter}");
    counter++;
} while (counter < 5);

2.4 foreach

This is used to iterate through collections such as arrays or lists in a simple way.

int[] numbers = {1, 2, 3, 4, 5};
foreach (int number in numbers)
{
    Console.WriteLine(number);
}

3. Practical Exercises

3.1 Calculate the Factorial of a Number

The factorial of a positive integer n is the product of all positive integers less than or equal to n.

int number = 5;
int factorial = 1;
for (int i = 1; i <= number; i++)
{
    factorial *= i;
}
Console.WriteLine($"The factorial of {number} is {factorial}.");

3.2 Consecutive Sums

Calculate the sum of the first n natural numbers.

int n = 10;
int sum = 0;
for (int i = 1; i <= n; i++)
{
    sum += i;
}
Console.WriteLine($"The sum of the first {n} numbers is {sum}.");

3.3 Check if a Number is Prime

A prime number is one that is only divisible by 1 and itself.

int number = 7;
bool isPrime = true;
for (int i = 2; i < number; i++)
{
    if (number % i == 0)
    {
        isPrime = false;
        break;
    }
}
if (isPrime)
{
    Console.WriteLine($"{number} is a prime number.");
}
else
{
    Console.WriteLine($"{number} is not a prime number.");
}
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Part 4: Control Flow in C#

Thematisch verwandte Begriffe: Part, Control, Flow · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-93956 | A flaw has been found in olivier-ls PHP-FTS up to 1.1.2. Affected by thi…
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