Listing 4.3-Ternar: Kiritilgan sonni 2 dan 5 gacha bo‘lgan sonlarga qoldiqsiz bo‘linish yoki bo‘linmasligini ko‘rsatuvchi dasturini.
2-Misol.Kiritilgan ikki sonni solishtirish uchun if..else va ternar operatorlaridan foydalanib dastur tuzing.
using System;
namespace kattasini_topish_consol
{
class Program
{
static void Main(string[] args)
{
l1:
Console.Write("Birinchi sonni kiriting: ");
int a = int.Parse(Console.ReadLine());
Console.Write("Ikkinchi sonni kiriting: ");
int b=int.Parse(Console.ReadLine());.
//Qisqa shart operatori bilan a va b larni teng emasligini tekshiramiz
Console.WriteLine(a!=b ?
a+" soni "+b+" soniga teng emas":
a+" soni "+b+" soniga teng");
//Agar a son b songa tengligi yolg‘on bo‘lsa,
if (!(a==b)) {
if(a>b) Console.WriteLine(a+" > "+b);
else Console.WriteLine(a+" < "+b);
}
//Bitta bo‘sh qator tashlash
Console.WriteLine();
//Kerakli tugmalar bosish haqida xabar chiqarish
Console.Write("\n Takrorlash uchun R ni bosing\n Dasturdan chiqish uchun X ni bosing;");
char x;
l0:
//x ga bosilgan tugma qiymatini o‘zlashtirish
x=Console.ReadKey().KeyChar;
if ((x == 'x') || (x == 'X'))
goto l2; // x tugma bosilsa l2 metkaga o‘tish
else if ((x == 'r') || (x == 'R'))
{
Console.Clear();
goto l1; //r tugma bosilsa l1 metkaga o‘tish
}
goto l0; //Boshqa tugmalar bosilsa l0 metkaga o‘tilsin.
l2:; // dastur oxiri, tugashi
}
}
}
|
Do'stlaringiz bilan baham: |