Ma’ruza mashg`ulot uchun


Download 0.85 Mb.
bet18/49
Sana19.06.2023
Hajmi0.85 Mb.
#1600219
1   ...   14   15   16   17   18   19   20   21   ...   49
Bog'liq
1-semestr maruzalar

Listing 4.1. Nishonga otilgan o‘qlar bo‘yicha ochkoni hisoblash dasturi

If..else konstruksiyasi ichida yana bir nechta If..else konstruksiyasin qo‘llash mumkin. Lekin bu dasturni murakkablashib ketishiga olib kelishi mumkin.


Ichma-ich joylashgan If..else konstruksiyasiga misol tariqasida 4.1 listingda kiritilgan 3 ta sondan eng katta va eng kichigini topish dasturi keltirilgan.

using System;
namespace uch_sondan_topish
{
class Program
{
static void Main(string[] args)
{
double a, b, c, max=0, min=0;
//string smax, smin;
Console.Write("A=");
a = double.Parse(Console.ReadLine());
Console.Write("B=");
b = double.Parse(Console.ReadLine());
Console.Write("C=");
c = double.Parse(Console.ReadLine());
if ((a == b) && (b == c))
Console.WriteLine("a, b, c sonlari o'zaro teng!");
else
{
if (a > b)
{ max = a; min = b;}
else if (a < b)
{max = b; min = a;} //****2
else
{max = b; min = b;}
//***********************
if (max < c)max = c;
else if (min > c)min = c;
else if (max == c)max = c;
else if (min == c)min = c;
}
Console.WriteLine("MAX=" + max + ", MIN=" + min + ",\n ");
Console.ReadKey();
}
}
}



Listing 4.2. 3 ta sondan eng katta va eng kichigini topish dasturi
4.2.Qisqa shartli operator
Ushbu operator bilan biz avvalgi ma’ruzalarimizda tanishib o‘tgan edik. Ushbu operatorni ternar operator ham deb ataladi. Odatda ternar operatoridan shartga mos ravishda bajariluvchi operatorlar soni bittadan ko‘p bo‘lmaganda foydalanish qulaydir.
Sintaksisi: <shart ? [rost bo‘lsa, operator]:[yolg‘on bo‘lsa, operator]>
Masalan:
int i=10;
Console.WriteLine(i==10 ? “i = 10”:“i <> 10”);

Console.WriteLine(i<20 ? “i < 20”:“i >= 20”);





Download 0.85 Mb.

Do'stlaringiz bilan baham:
1   ...   14   15   16   17   18   19   20   21   ...   49




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling