if语句

1630 更新于: 2021-07-16 读完约需 10 分钟

前言

一般地,几乎都有的程序设计语言都有流程控制,流程控制包括:条件判断语句,循环控制语句,跳转语句等。

例如:if语句、swith语句、while语句、do…while语句、for语句、foreach语句、break语句、continue语句、goto语句、return语句、异常处理语句等,其中foreach语句和异常语句是C#语言新增加的控制语句。

本节主要介绍if语句

if(作决定/判断)

if语句是一个条件分支语句,语法格式为:

if (condition)
{
    //语句或者语句块
}

以下是判断语句常规的流程图:

if只有在conditiontrue(真)的时候才会执行其中的语句或者语句块,示例:

int a = 70;
if(a > 60)
{
    Console.WriteLine("及格")
}

以上示例中,变量a的值为70,if语句的条件表达a>60是成立的,所以会执行语句块中的语句,即在控制台输出”及格”的结果。

C#语言中if语句的规则和约定:

  • if 是小写的
  • if 后面跟一个括号将表达式括起来,括号中可以是结果为布尔的表达式或者布尔值
  • 一般情况下使用花括号将语句块包裹起来
  • 特殊情况下,即只有一条语句时,可以不使用花括号
  • 如果有花括号,其后不跟分号作为结束
  • 如果条件是布尔值,可不使用==号来简写
if (success == true)
{
}

可以简写成:

if (success)
{
}

为假时:

if (success != true)
{
}

可以简写成:

if (!success)
{
}

为假的几种写法:

bool success = true;
if(success != true)
{
}

或者

if(success == false)
{
}

或者

if(!success)
{
}

多个逻辑运算或者关系运算

逻辑与运算

bool doorIsOpen = true;
bool gridIsEmpty = true;
int gridWidth = 80;
int gridHeight = 60;
int packageWidth = 100;
int packageHeight = 80;

// 逻辑与运算
if(doorIsOpen && gridIsEmpty && gridWidth > packageWidth && gridHeight >packageHeight)
{
    //可以将物品放进寄存柜
}

//逻辑或运算
//比如:去办理宽带业务,有可能是带身份证和身份证复印件
// 也有可能是带身份证或者身份证复印件
// 和50元现金

bool hasIdCard = true;
bool hasIdCardCopy = true;
bool hasCash = false;
bool cashAmount = 50;
if((hasIdCard || hasIdCardCopy) && hasCash)
{
    //可以办理
}

// 或者写成
if((hasIdCard || hasIdCardCopy) && cashAmount >=50)
{
    //可以办理
}

if … else …

if ... else ...语句是if语句的更进一步,语法格式为:

if (condition)
{
    // 表达式结果为true时执行
    //语句或者语句块
}
else{
    // 表达式结果为false时执行
    //语句或者语句块
}

只有在if条件值为false(假)时,才会执行else中的语句或者语句块,示例:

int a = 70;
if(a > 60)
{
    Console.WriteLine("及格")
}
else
{
    Console.WriteLine("不及格");
}

if… else if

多个if同时使用并且只能有一个条件满足:

if (condition1)
{
当条件 1 为 true 时执行的代码
}
else if (condition2)
{
当条件 2 为 true 时执行的代码
}
else
{
当条件 1 和 条件 2 都不为 true 时执行的代码
}

示例代码:

Console.WriteLine("请输入你的选择(1,2,3):")
string myChoice = Console.ReadLine();
if(score == 1)
{
    string message = "不及格";
    Console.WriteLine(message)
}
else if(score >= 60 && score < 70)
{
    string message = "及格";
    Console.WriteLine(message)
}
else if(score >= 70 && score < 90)
{
    string message = "良好";
    Console.WriteLine(message)
}
else if(score >= 90 && score < 100)
{
    string message = "优秀";
    Console.WriteLine(message)
}
else
{
    string message = "天才";
    Console.WriteLine(message)
}

简化以上示例代码

嵌套的if语句

if(condition1)
{
    if(condition2)
    {
    }
    else
    {
    }
}

三元运算

我们已经在前面的章节中讲解了条件运算符 (? :),可以用来替代 if…else 语句。它的一般形式如下:

Boolean Expression ? First Statement : Second Statement;

如你所见,这个语法包含了三个部分,其中第一部分为问号(?)前的表达式,这个表达式返回布尔值。第二部分为问号(?)后和冒号(:)前的First Statement(语句)。第三部分为和冒号(:)Second Statement(语句)。请注意,冒号的使用和位置。

? 表达式的值是由 Expression 决定的。如果 Expression 为真,则计算 First Statement 的值,结果即为整个 ? 表达式的值。如果 Expression 为假,则计算 Second Statement 的值,结果即为整个 ? 表达式的值。

三元运算示例一:

int x = 20, y = 10;
string result = x > y ? "x大于y" : "x小于或者等于y";
Console.WriteLine(result);

三元运算的返回值可以是任意的数据类型,比如可以返回:字符,字符串,数字,引用类型等等。

实例练习题:比较两个数大小,并输出较大的那个。

三元运算表达式实现代码:

int x = 20, y = 10;
int result = x > y ? x : y;

Console.WriteLine(result);

if语句实现代码:

int x = 2, y = 10;
int result = 0;
if(x > y)
{
    result = x;
}
else if( x < y){
    result =  y;
}
Console.WriteLine(result);

版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。

本文永久链接码友网 » C#程序设计基础(入门篇) » if语句 分享:

发表评论

登录用户才能发表评论, 请 登 录 或者 注册