DEV Community

Sabrina
Sabrina

Posted on

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

a) Quyidagi kodda if, else if, else qismi qanday ishlaydi?

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

Javob : Agar x qiymati 5dan kam bo'lsa "Less than 5" degan gapni chiqaradi.Agar x qiymati 10ga teng bo'lsa "Equal to 10" degan gapni chiqaradi.Boshqa holatda esa "Greater than 5" chiqaradi

b) Ternary operatorning sintaksisi qanday ?
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("Wednesday");
         break;
     break;

    default:
         Console.WriteLine("Invalid day");
    break;
 }

Enter fullscreen mode Exit fullscreen mode

Top comments (0)