Condition
if-else operator
string color = "black";
if (color == "black")
{
Console.WriteLine("It's black.");
}
else if (color == "white")
{
Console.WriteLine("It's white.");
}
else {
Console.WriteLine("It's other color.");
}
Conditional Operator
int saleAmount = 1001;
int discount = saleAmount > 1000 ? 100 : 50;
Console.WriteLine($"Discount: {discount}");
Scope
bool flag = true;
if (flag)
{
int value = 10;
Console.WriteLine($"Inside the code block: {value}"); // Prints value.
}
Console.WriteLine($"Outside the code block: {value}"); // Gives error because value is declared inside the if code block.
Switch
string fruit = "apple";
switch (fruit)
{
case "apple":
Console.WriteLine($"App will display information for apple.");
break;
case "banana":
Console.WriteLine($"App will display information for banana.");
break;
case "cherry":
Console.WriteLine($"App will display information for cherry.");
break;
default:
Console.WriteLine($"App will not display information about any fruit.");
break;
}
Loop
Foreach
string[] names = { "Rowena", "Robin", "Bao" };
foreach (string name in names)
{
Console.WriteLine(name); // "Rowena", "Robin", "Bao"
}
For
for (int i = 0; i < 10; i++)
{
if (i > 5) {
break;
}
Console.WriteLine(i); // 1, 2, 3, 4, 5
}
Do-While
Random random = new Random();
int current = 0;
do
{
current = random.Next(1, 11);
if (current >= 8) {
continue;
}
Console.WriteLine(current);
} while (current != 7);
While
Random random = new Random();
int current = random.Next(1, 11);
while (current >= 3)
{
Console.WriteLine(current);
current = random.Next(1, 11);
}
Console.WriteLine($"Last number: {current}");