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

169 阅读1分钟

「这是我参与2022首次更文挑战的第9天,活动详情查看:2022首次更文挑战」。

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中。

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

实践出真知。

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

欢迎访问个人博客 guanchao.site

欢迎访问小程序:

在这里插入图片描述