else if
else if operatori
Birinchi shart False bo‘lganda yangi shartni belgilash uchun else if operatoridan foydalaning.
Sintaksis
if (condition1)
{
// block of code to be executed if condition1 is True
}
else if (condition2)
{
// block of code to be executed if the condition1 is false and condition2 is True
}
else
{
// block of code to be executed if the condition1 is false and condition2 is False
}
Misol
int time = 22;
if (time < 10)
{
Console.WriteLine("Good morning.");
}
else if (time < 20)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
}
// Outputs "Good evening."
Misol izohi
Yuqoridagi misolda time (22) 10 dan katta, shuning uchun birinchi shart False bo‘ladi. else if operatoridagi keyingi shart ham False, shuning uchun else shartiga o‘tamiz, chunki condition1 ham, condition2 ham False — va ekranga "Good evening" matnini chiqaramiz.
Biroq, agar vaqt 14 bo‘lganida, dasturimiz "Good day." matnini chiqargan bo‘lardi.
W3Schools Pathfinder
Yutuqlaringizni kuzating – bu bepul!
