C# if else if yapısı etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
C# if else if yapısı etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

3 Nisan 2013 Çarşamba

10 - İf Else İf... Baba ve 20 Yaş Farkı...

Form tasarımı aşağıdaki gibi olacaktır...

Program çalıştığında ise aşağıdaki gibi sonuç verecektir...
Program kodları aşağıdaki gibi olacaktır... 
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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {           
             InitializeComponent();        
        }

        private void button1_Click(object sender, EventArgs e)
        {
          // Çocukların yaşlarının toplamı ile babanın yaşını kıyaslayan program?
            // Kıyaslama yapmadan önce her çocuk ile baba arasında 20 yaş olması gerekmektedir...

            // Yaşlar girdiriliyor... 
            int c1 = Convert.ToInt32(textBox1.Text);
            int c2 = Convert.ToInt32(textBox2.Text);
            int c3 = Convert.ToInt32(textBox3.Text);
            int b = Convert.ToInt32(textBox4.Text);

            // Çocukların yaşları ile babanın yaşı arasında 20 yaş farkı olması kıyaslanıyor... 
            if ((c1 + 20) < b && (c2 + 20) < b && (c3 + 20) < b)   
            {
                int toplam = c1 + c2 + c3; // Çocukların yaşları toplanıyor... 
                if (toplam < b) // Babanın yaşı ile çocukların yaşları toplamı kıyaslanıyor... 
                    MessageBox.Show("Babanın Yaşı Büyük");
                else if (toplam > b)
                    MessageBox.Show("Çocukların Yaşı Büyük");
                else if (toplam == b)
                    MessageBox.Show("Yaşlar Eşit");
                else // Her hangi bir hata durumu kıyaslanıyor... 
                    MessageBox.Show("HATA...");
            }

            else
                MessageBox.Show("Çocukların her birinin babadan en az 20 yaş küçük olması gerekmektedir...");  
        }
    } 
}



9 - İf Else İf... Mevsimler...

Form tasarımı aşağıdaki gibi olacaktır... 

Program çalıştığında ise aşağıdaki gibi bir sonuç alınacakıtr... 

Programın kodları aşağıdadır... 


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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Girilecek olan ayın numarasına göre mevsimleri söyleyecek olan program?
            int ay = Convert.ToInt32(textBox1.Text);

            // Eğer 1-12 arası sayı girdirilmiyorsa hata mesajı verdiriyoruz... 
            if (ay <= 12 && ay >= 1)
            {
                // Mevsimlere göre işlemlerimizi yapıyoruz... 
                if (ay >= 3 && ay <= 5)
                   MessageBox.Show("İLKBAHAR...");
                else if (ay >= 6 && ay <= 8)
                    MessageBox.Show("YAZ...");
                else if (ay >= 9 && ay <= 11)
                    MessageBox.Show("SONBAHAR...");
                else if (ay == 12 || ay == 1 || ay == 2) // Kış mevsiminde 3 şart olduğu için durum biraz daha farklı...
                    MessageBox.Show("KIŞ...");
            }
            else
                MessageBox.Show("HATA...");
        }
    }
}

8 - İf Else İf... Birden Fazla Şart Kontrolü...

Form tasarımı aşağıdaki gibi olacaktır... 

Programın çalışır hali aşağıdaki gibi olacaktır... 

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {            InitializeComponent();       }

        private void button1_Click(object sender, EventArgs e)
        {
            // Yaşa göre yorum yapacak olan program?
            string ad = textBox1.Text;
            string soyad = textBox2.Text;
            int yas = Convert.ToInt32(textBox3.Text);

            // Örneğin 5 girilirse çocuk olarak yorum yapacağız... 
            if (yas >= 0 && yas <= 4)
                MessageBox.Show("Yaş = " + yas + " Yaş Grubu = Bebek");
            else if (yas >= 5 && yas <= 12)
                MessageBox.Show("Yaş = " + yas + " Yaş Grubu = Çocuk");
            else if (yas >= 13 && yas <= 18)
                MessageBox.Show("Yaş = " + yas + " Yaş Grubu = Ergen");
            else if (yas >= 19 && yas <= 30)
                MessageBox.Show("Yaş = " + yas + " Yaş Grubu = Genç");
            else if (yas >= 31 && yas <= 45)
                MessageBox.Show("Yaş = " + yas + " Yaş Grubu = Yetişkin");
            else if (yas >= 46 && yas <= 65)
                MessageBox.Show("Yaş = " + yas + " Yaş Grubu = Orta Yaş");
            else if (yas >= 66 && yas <= 100)
                MessageBox.Show("Yaş = " + yas + " Yaş Grubu = Yaşlı");
            else
                MessageBox.Show("Girilen değer yanlış bir değerdir... ");

// if else if yapısında unutulmaması gereken durum şartlardan sadece bir tanesinin çalıştığıdır... 
        }  }  }

7 - İf Else İf... Dört İşlemde Seçim...

Form tasarımı aşağıdaki gibi olacaktır... 

Program çalıştığında ise aşağıdaki gibi bir sonuç ile karşılaşılacaktır...

Programın kodları ise aşağıdaki gibidir... Kodlar yine hesapla butonunun içerisine yazılmıştır... 
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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Girilecek olan sayılara seçime göre dört işlemi yaptıracak olan programdır... 
            double s1, s2;
             s1 = double.Parse(textBox2.Text);
             s2 = double.Parse(textBox3.Text);

             string islem = textBox1.Text; // Buraya girilen seçimi aldırıyoruz... 
             if (islem == "/")
                 MessageBox.Show("Bölme İşleminin Somucu = " + (s1 / s2));
            else if (islem=="*")
                 MessageBox.Show("Çarpma İşleminin Somucu = " + (s1 * s2));
            else if (islem=="-")
                 MessageBox.Show("Çıkarma İşleminin Somucu = " + (s1 - s2));
            else if (islem=="+")
                 MessageBox.Show("Toplama İşleminin Somucu = " + (s1 + s2));
        }
    }
}



6 - İf Else İf... İki Sayı Kıyaslama...

Form tasarımı aşağıdaki gibi olacaktır...

Program çalıştığında aşağıdaki gibi ekran görüntüsü oluşacaktır... Kodlarımızı yine hesapla butonunun içerisine yazıyoruz... 

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Girilecek iki sayı içerisinde büyük ve küçük olanları bulan program?

            int sayi1 = Convert.ToInt32(textBox1.Text);
            int sayi2 = Convert.ToInt32(textBox2.Text);
            
            // İf else if yapısını kullanıyoruz... 
            if (sayi1 == sayi2)
                MessageBox.Show("Sayılar Birbirine Eşit");
            else if (sayi1 > sayi2)
                MessageBox.Show(sayi1+   " Sayısı Daha Büyük... "+sayi2+  " Sayısı Daha Küçük...");
            else if (sayi1 < sayi2)
                MessageBox.Show(sayi2+   " Sayısı Daha Büyük..."+sayi1+   " Sayısı Daha Küçük...");
        }
    }
}