20. Operators( if, else if, else, ternaty operator, switch)

zahro_11eecc01a1cfe5142b9

Zahro

Posted on October 16, 2024

20. Operators( if, else if, else, ternaty operator, switch)

a) Quyidagi kodning natijasini ayting:

int temperature = 30;

if (temperature > 25)
{
    Console.WriteLine("Hot");
}
else if (temperature > 15)
{
    Console.WriteLine("Warm");
}
else
{
    Console.WriteLine("Cold");
}
Enter fullscreen mode Exit fullscreen mode

Natija: Hot

b) switch operatoridan foydalanib, quydagi if-else kodini switch operatoriga o`zgartiring:

int day = 3;

switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Invalid day");
break;
}

c) Ternary operator yordamida kodni qisqartirish:

int a = 10;
string result = (a > 5) ? "Greater than 5" : "Less than or equal to 5";

💖 💪 🙅 🚩
zahro_11eecc01a1cfe5142b9
Zahro

Posted on October 16, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related