Inhalt der Lektion

Eine Summe Schritt für Schritt bilden

Starte bei null und zähle jede Zahl der Reihe nach zur bisherigen Summe dazu.

Ein einfaches Beispiel

Am Anfang2 + 4
Ergebnis6
Program.cs
using System;

int total = 0;
total += 2;
total += 4;
Console.WriteLine(total);
Ausgabe
6
Anzeigen

Beispiele aus dem Alltag

Drei Klassen spenden 5, 2 und 4 Bücher. Wie viele Bücher kann die Bibliothek auf ihre Regale stellen?

Am Anfang5 + 2 + 4
Ergebnis11
Program.cs
using System;

int[] books = new int[] { 5, 2, 4 };
int total = 0;
int i = 0;
while (i < 3) {
    total += books[i];
    i += 1;
}
Console.WriteLine(total);
Ausgabe
11
Beispiel ausführen
Terminal
dotnet new console --output Lesson
cp Program.cs Lesson/Program.cs
dotnet run --project Lesson
Offizielle Dokumentation