C#(八)之判断语句IF SWITCH

53 篇文章 1 订阅
22 篇文章 0 订阅

C#中的判断语句IF/SWITCH

1:IF / ELSEIF / ELSE
If 满足条件择执行;
多个elseif时,其中有一个满足条件,那个之后的elseif都不会执行;
else上面都不满足时执行;

C#支持三元运算符,用法同于PHP

bool a = true;
            bool c = true;
            bool b = false;
            bool d = false;
            int num = 0;
            //if
            if(!(a || c)){
                //Console.WriteLine(1);
            }
            if(!(a || b)){
                //Console.WriteLine(2);
            }
  
            if(!(a || d)){
                num = 1;
            }else if(!(b || d)){
                num ++;
            }
            //Console.WriteLine(num);
  
            //三元运算符
            string rts = "";
            rts =  num == 1 ? "1" : "2";
            Console.WriteLine(rts);

2:Switch:这个按照正常语法写就可以了,不要忘记写break;

还有一种用法成为switch循环,就是switch中你想要的case判断都走一遍。

使用goto,具体用法,参照下边实例

// switch goto
            Console.WriteLine("box sizes:1=small, 5=medium, 10=large");
            Console.WriteLine("请选择");
            string s = Console.ReadLine();
            int i = int.Parse(s);
            int cc = 0;
            switch (i)
            {
                case 1:
                    cc += 1;
                    break;
                case 5:
                    cc += 5;
                    goto case 1;     //重要
                case 10:
                    cc += 10;
                    goto case 1;
                default:
                    Console.WriteLine("无效的输入。请选择1,5,or 10");
                    break;
            }
            Console.WriteLine("谢谢!您的花费={0}$", cc);

上代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace gc
{
    class Program
    {
        /* C#主要的运行函数,就是main函数 */
        static void Main(string[] args)
        {
            bool a = true;
            bool c = true;
            bool b = false;
            bool d = false;
            int num = 0;
            //if
            if(!(a || c)){
                //Console.WriteLine(1);
            }
            if(!(a || b)){
                //Console.WriteLine(2);
            }
  
            if(!(a || d)){
                num = 1;
            }else if(!(b || d)){
                num ++;
            }
            //Console.WriteLine(num);
  
            //三元运算符
            string rts = "";
            rts =  num == 1 ? "1" : "2";
            //Console.WriteLine(rts);
  
  
            //switch
            //Console.WriteLine("请输入一个1~7的数字:");
            //int number = Convert.ToInt32(Console.ReadLine());
            int number = 1;
            string str = "";
            switch(number){
                case 0:
                    str = "星期日";
                    break;
                case 1:
                    str = "星期一";
                    break;
                case 2:
                    str = "星期二";
                    break;
                case 3:
                    str = "星期三";
                    break;
                case 4:
                    str = "星期四";
                    break;
                case 5:
                    str = "星期五";
                    break;
                case 6:
                    str = "星期六";
                    break;
                default:
                    str = "朕也不知道啊";
                    break;
            }
            //Console.WriteLine(str);
  
            // switch goto
            Console.WriteLine("box sizes:1=small, 5=medium, 10=large");
            Console.WriteLine("请选择");
            string s = Console.ReadLine();
            int i = int.Parse(s);
            int cc = 0;
            switch (i)
            {
                case 1:
                    cc += 1;
                    break;
                case 5:
                    cc += 5;
                    goto case 1;     //重要
                case 10:
                    cc += 10;
                    goto case 1;
                default:
                    Console.WriteLine("无效的输入。请选择1,5,or 10");
                    break;
            }
            Console.WriteLine("谢谢!您的花费={0}$", cc);
  
        }
    }
}

好啦,通过实践我们知道了:

if、if-else的用法与PHP是相同的。

switch-case的基本用法逻辑也是与php相同的,

但是C#中多了一个switch循环,使用goto

If-else if 的用法和PHP不一样:C#中与else if前的if或者elseif的语句为真,就不会再继续进入下一个else if中。

好不好用,一不一样,得我自己动手试了才行,你说好用,你说一样,那不行。

实践出真知。

有好的建议,请在下方输入你的评论。

欢迎访问个人博客
https://guanchao.site

欢迎访问小程序:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值