3 Nisan 2013 Çarşamba

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... 
        }  }  }