5. Operators(if, else if, ternary operator, switch)

zahro_11eecc01a1cfe5142b9

Zahro

Posted on October 11, 2024

5. Operators(if, else if, ternary operator, switch)

a) Quyidagi kodni tushuntiring:

int x = 10;

if (x < 5) 
{
    Console.WriteLine("Less than 5");
} 
else if (x == 10) 
{
    Console.WriteLine("Equal to 10");
} 
else 
{
    Console.WriteLine("Greater than 5");
}
Enter fullscreen mode Exit fullscreen mode

Bu kodda x 10 ga teng. Shuning uchun dastlab if (x < 5) sharti noto'g'ri bo'ladi va bu blok ishlatilmaydi.

Keyin, else if (x == 10) sharti tekshiriladi va bu to'g'ri bo'lgani uchun, "Equal to 10" ekranga chiqariladi.

else qismi esa ishlatilmaydi, chunki oldingi shart bajarildi.

b) Ternary operatorning sintaksisi qanday? Misol keltiring.

Sintaksis: shart ? ifTrue : ifFalse

c) Quyidagi kodda switch qismida qaysi natija chiqadi?

int day = 3;

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

    default:
        break;
}
Enter fullscreen mode Exit fullscreen mode

Kodda case 3 bo'lganda break komandasi darhol ishlaydi va Console.WriteLine bloki bajarilmaydi. Shuning uchun hech qanday natija chiqmaydi.

💖 💪 🙅 🚩
zahro_11eecc01a1cfe5142b9
Zahro

Posted on October 11, 2024

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

Sign up to receive the latest update from our blog.

Related