三目运算符也称为运算符,根据结果来反馈
如果条件为真? 则为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();
}
}
}