Inhalt der Lektion

Mit if und else entscheiden

Prüfe eine Bedingung, um festzustellen, welcher Schritt als Nächstes ausgeführt wird.

Ein einfaches Beispiel

Am Anfang2 < 3
Ergebnis1
Program.cs
using System;

if (2 < 3) {
    Console.WriteLine(1);
} else {
    Console.WriteLine(0);
}
Ausgabe
1
Anzeigen

Beispiele aus dem Alltag

Eine Familie braucht 4 Sitzplätze, aber nur 3 sind frei. Gib 1 aus, wenn alle Platz haben, sonst 0.

Am Anfang4 → 3
Ergebnis0
Program.cs
using System;

int needed = 4;
int free = 3;
if (needed <= free) {
    Console.WriteLine(1);
} else {
    Console.WriteLine(0);
}
Ausgabe
0
Beispiel ausführen
Terminal
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson
Offizielle Dokumentation