第一章 C#语法基础-----三目运算符

三目运算符也称为运算符,根据结果来反馈

如果条件为真? 则为X:否则为Y

int   num = 10 == 10 ? 1 : 2

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            //是否可以用登录 0 表示可以登录,1 代表不可以登录
            int canLogin = 1;

            string userName = "123";
            string passWord = "abc";

            canLogin = (userName == "1234" && passWord == "abc") ? 0:1;

            string adminName = userName == "Admin" ? "admin":"none";
            Console.ReadKey();
        }
    }
}

if(如果):用来判断条件是否成立,即是否为true

分支语句   不能单独使用

else if(其他可能性):if  不成立,但是满足我的条件,则执行else:以上条件都不满足执行

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            //判断条件为true则执行{}代码,为false不执行
            //if (false)
            //{
            //  Console.WriteLine("if 输出语句"); 
            //}

            //if(1 == 1)
            //{
            //    Console.WriteLine("if 语句输出");
            //}

            //int num = 4;
            //if(num > 3)
            //{
            //    Console.WriteLine("num > 3");
            //}
            //else if(num > 4)
            //{
            //    Console.WriteLine("num > 4");
            //}
            //else if(num > 5)
            //{
            //    Console.WriteLine("num > 5");
            //}

            //bool isLogin = true;
            //if (isLogin)
            //{
            //    //执行登录
            //}
            //else
            //{
            //    //执行未登录的
            //}

            //跳过就会执行
            //当一个条件满足后,执行该条件代码块中的代码,不会运行之后条件中的判断条件代码
            int num = -2;
            if((num += 1) < 0)
            {
                Console.WriteLine(num);
            }
            else if((num += 1) < 2)
            {
                Console.WriteLine(num);
            }
            else
            {
                Console.WriteLine(num);
            }
           
            Console.ReadKey();
        }
    }
}

switch语句

逻辑语句switch的基本语法

switch中case与break语句

switch相关短路机制

switch:指定检查 的变量

case:表示这个变量一种值的情况

        break:这个case运行结束了

default:所有case都不能满足,则运行

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

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            //用来判断是否
            if (true)
            {

            }

            //用来测试以下变量的多种可能性
            string name = "张三";
            switch (name)
            {
                // case 变量预期的值

                case "张三":
                    //要执行的语句
                    Console.WriteLine("张三");
                    //跳出
                    break;
                case "李四":
                    Console.WriteLine("李四");
                    break;
                case "abcd":
                    Console.WriteLine("王五");
                    break;
                default:
                    Console.WriteLine("default");
                    break;
            }
            
            Console.ReadKey();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_41392061

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值