C#语言学习--基础部分(四) --关系运算符,判断语句

1.==,!=,<,<=,>,>=

2.逻辑运算符:&&,||

3.运算符的优先级

4.if语句,级联的IF语句

eg:WPF DEMO Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SelectionDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Compare_Click(object sender, RoutedEventArgs e)
        {
            int diff = dateCompare(first.SelectedDate.Value, second.SelectedDate.Value);
            info.Text = "";
            show("first ==second", diff==0);
            show("first !=second", diff != 0);
            show("first < second", diff < 0);
            show("first <=second", diff <= 0);
            show("first > second", diff > 0);
            show("first >=second", diff >= 0);

        }
        public void show(string exp,bool result) {
            info.Text += exp;
            info.Text += ":"+result.ToString();
            info.Text += "\r\n";
        }
        public int dateCompare(DateTime leftHandSide, DateTime rightHandSide)
        {
            int result;
            if (leftHandSide.Year > rightHandSide.Year)
            {
                result= 1;
            }
            else if (leftHandSide.Year < rightHandSide.Year)
            {
                result = -1;
            }
            else  if (leftHandSide.Month > rightHandSide.Month)
            {
                result = 1;
            }
           else if (leftHandSide.Month < rightHandSide.Month)
            {
                result = -1;
            }
            else if (leftHandSide.Date > rightHandSide.Date)
            {
                result = 1;
            }
             else if (leftHandSide.Date < rightHandSide.Date)
            {
                result = -1;
            }
            else
            {
                result = 0;
            }
               
           
            return result;
        }

    }
}

5.switch语句:

 a.case语句后只能跟int,string类型等原始数据类型

   b.case语句只能跟值固定的表达式(只能跟具体值,不能跟变量值)

   c.case语句后面的表达式必须唯一,不能出现两个case语句拥有相同的值.

 eg:1.WPF DEMO Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SwitchStatement
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void copy_Click(object sender, RoutedEventArgs e)
        {
            target.Text = "";
            string from = source.Text;
            for (int i = 0; i != from.Length; i++)
            {
                char current = from[i];
                copyOne(current);
            }
        }
        private void copyOne(char current)
        {
            switch (current)
            {
                case '>':
                    target.Text += "&gt;";
                    break;
                case '<':
                    target.Text +="&lt;";
                    break;
                case '\"':
                    target.Text +="&#34;";
                    break;
                case '\'':
                    target.Text += "&#39;";
                    break;
                default:
                    target.Text += current;
                    break;
            }
        }
    }
}

2.Console DEMO:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SwitchDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            int day;
            string dayName;
            day =int.Parse( Console.ReadLine());
            switch (day)
            {
                case 0:
                    dayName = "Sunday";
                    break;
                case 1:
                    dayName = "Monday";
                    break;
                case 2:
                    dayName = "Tuesday";
                    break;
                case 3:
                    dayName = "Wednesday";
                    break;
                case 4:
                    dayName = "Thursday";
                    break;
                case 5:
                    dayName = "Friday";
                    break;
                case 6:
                    dayName = "Saturday";
                    break;
                default:
                    dayName="Unkown";
                    break;

            }
            Console.WriteLine(dayName);

        }
    }
}

 

转载于:https://www.cnblogs.com/sharpenabc/archive/2012/08/12/2635576.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值