if (shart) operator1; else operator2;
Agar shart rost bo’lsa, operator1 bajariladi, agar yolg’on bo’lsa,
operator2 bajariladi. Shuni alohida takidlab o’tish lozimki, agarda siz shart
yolg’on bo’lganda dasturingiz hech bir ish bajarmasligini xohlasangiz,
operator2 ni yozmasligingiz mumkin.Bunda dastur if … else dan so’ng yozilgan
kod bo’yicha o’z ishini davom ettiradi. Agarda operator1 yoki operator2 dan
so’ng bajarilishi lozim bo’lgan amallar soni 1 tadan ortiq bo’lsa ular figurali {}
qavslar orasida yozilishi lozim.
3-Misol. ntural son va haqiqiy son berilgan. Bir sikldan foydalanibquyidagi ning 1 dan gacha bo’lgan barcha darajalarni chiqaruvchi va yig’indisini hisoblovchi dastur tuzilsin. Shart operatoridan foydalanilmasin.
Dasturning C# dagi ko’rinishi:
=using System;
namespace For_3
{
classProgram
{
publicstaticvoidMain(string[] args)
{
double a,s=1,m=1; int n,k;
Console.Write("Sonni kiriting: a=");
a=int.Parse(Console.ReadLine());
Console.Write("a ning topish kerak bo'lgan darajani kiriting: n=");
n=int.Parse(Console.ReadLine());
for (k=1;k<=n;k++)
{
s*=a;Console.WriteLine(a+" ning "+k+"-darajasi: "+s);
m+=Math.Pow(-1,k)*s;
}
Console.Write("Yig'indi: "+m);
Console.ReadKey(true);
}
}
}
Natija:
Sonni kiriting: a=2
A ning topish kerak bo`lgan darajani kiriting: n=10
2 ning 1-darajasi : 2
2 ning 2-darajasi : 4
2 ning 3-darajasi : 8
2 ning 4-darajasi : 16
2 ning 5-darajasi : 32
2 ning 6-darajasi : 64
2 ning 7-darajasi : 128
2 ning 8-darajasi : 256
2 ning 9-darajasi : 512
2 ning 10-darajasi : 1024
For takrorlash operatori bu amallarni
birta instruksiyaga birlashtiradi. Uning ko`rinishi (strukturasi) quyidagicha.
For ((inisializatsiya(nomlash) ); [ifoda] ; [i ni ortirish])
{
Instruksiya
}
Windows ilovasida tuzilgan dasturlar.
Amallar:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace amallar
{
publicpartialclassForm1 : Form
{
int a, b;decimal c;
public Form1()
{
InitializeComponent();
}
privatevoid button1_Click(object sender, EventArgs e)
{
a = int.Parse(textBox1.Text);
b = int.Parse(textBox2.Text);
if (radioButton1.Checked) { c = a + b; label1.Text = Convert.ToString(c); }
if (radioButton2.Checked) { c = a - b; label1.Text = Convert.ToString(c); }
if (radioButton3.Checked) { c = a * b; label1.Text = Convert.ToString(c); }
if (radioButton4.Checked) { c = a / b; label1.Text = Convert.ToString(c); }
}
privatevoid button2_Click(object sender, EventArgs e)
{
Close();
}
}
}
CHiqish natijasi.
Do'stlaringiz bilan baham: |