Control Flow and Loops
int xp = 45;
if (xp >= 50)
{
Console.WriteLine("Level up!");
}
else
{
Console.WriteLine($"Keep going, {50 - xp} XP to go.");
}
for (int i = 1; i <= 5; i++)
{
Console.WriteLine($"Lesson {i}");
}
foreach (var lang in new[] { "C#", "Python", "JavaScript" })
{
Console.WriteLine(lang);
}
The pieces
if / else branch on a boolean condition.
for (init; condition; increment) is best when you know how many times to loop.
foreach (var item in collection) visits every element of an array or collection without needing an index, use it whenever you don't need the index itself.