C# الدورة

C# Conditions and Loops

دروس Python 3 للمبتدئين.

if / else if / else

int score = 85;
if (score >= 90)
    Console.WriteLine("A");
else if (score >= 80)
    Console.WriteLine("B");
else if (score >= 70)
    Console.WriteLine("C");
else
    Console.WriteLine("F");

Ternary Operator

int x = 10;
string result = (x % 2 == 0) ? "even" : "odd";
Console.WriteLine(result); // "even"

switch Statement

string day = "Monday";
switch (day) {
    case "Saturday":
    case "Sunday":
        Console.WriteLine("Weekend!");
        break;
    default:
        Console.WriteLine("Weekday");
        break;
}

switch Expression (C# 8+)

string type = day switch {
    "Saturday" or "Sunday" => "Weekend",
    _ => "Weekday"
};
Console.WriteLine(type);

for Loop

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

foreach Loop

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

// With List
var fruits = new List<string> { "Apple", "Banana", "Cherry" };
foreach (var fruit in fruits) {
    Console.WriteLine(fruit);
}

while and do-while

int count = 0;
while (count < 3) {
    Console.WriteLine(count++);
}

int x = 5;
do {
    Console.WriteLine(x--);
} while (x > 0);