C# 中的 Else If

WBOY
发布: 2024-09-03 15:09:34
原创
630 人浏览过

C# 还支持条件语句。这些语句基本上在某人想要执行一组语句并且如果特定条件失败则执行另一组语句时使用。因此,当我们有多组语句并且我们希望根据场景或基于条件执行这些语句时,它非常有用。这主要用于决策场景。

语法:

if (some statement) {
}
else if (other statement) {
}
else {
(other statement)
}
登录后复制

C# 中 Else If 的流程图

这是C#中else if语句的流程图,如下所示:

C# 中的 Else If

C# 中的 Else If 是如何工作的?

例如,我们想根据学生获得的分数来显示成绩。

  • 得分超过 80% 的学生获得 A 级。
  • 60分以上且80分以下的学生为B级。
  • 同样,超过 40 分且低于 60% 的学生为 C 级,低于 40 分的学生为 D 级。
  • 所以在这些类型的场景(决策)中,我们使用了 If-else-if 语句来帮助开发人员得出结果。

在 C# 中实现 Else If 的示例

下面的示例展示了如何在 C# 中实现 else-if。

示例#1

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace elseif
{
class Program
{
static void Main(string[] args)
{
int p = 15;
if (p == 20)
{
Console.WriteLine("Value of p is equal to 20");
}
else if (p> 20)
{
Console.WriteLine("Value of p is greater than 20");
}
else
{
Console.WriteLine("Value of p is less than 20");
}
Console.ReadLine();
}
}
}
登录后复制

代码说明:在上面的示例中,根据条件使用 if else-if 语句。如果 p 的值等于 20,则显示该值等于 20 的输出,否则如果 p 的值大于 20,则显示不同的输出。如果两者都不满足,则显示该值小于 20。

输出:

C# 中的 Else If

示例#2

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace elseif
{
class Program
{
static void Main(string[] args)
{
int a = 30, b = 20;
if (a > b)
{
Console.WriteLine("Value of a is greater than b");
}
else if (a < b)
{
Console.WriteLine("Value of a is less than b");
}
else
{
Console.WriteLine("Value of a is equal to b");
}
Console.ReadLine();
}
}
}
登录后复制

代码说明:在上面的例子中,变量a和b的值被初始化。如果a的值大于b,则显示a的值较大,否则如果b的值较大,则显示a的值较小。如果上述两个条件都不成立,则 a 的显示值等于 b。

输出:

C# 中的 Else If

示例 #3

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace elseif
{
class Program
{
static void Main(string[] args)
{
int x = -1; int y = 20; int z;
if (x < 0 && y < 0)
{
Console.WriteLine("Both x and y are negative.");
}
else if (x < 0 || y < 0)
{
if (y > 0 && y <= 20)
{
z = x + y;
Console.WriteLine("Sum: {0}", z);
}
Console.WriteLine("One of them is negative");
}
else
{
Console.WriteLine("Both x and y are positive.");
}
Console.ReadKey();
}
}
}
登录后复制

代码说明: 在上面的例子中,||和 && 运算符也与语句一起使用。 Else if 语句还可以在循环中包含其他语句,称为嵌套语句。

输出:

C# 中的 Else If

示例#4

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace elseif
{
class Program
{
static void Main(string[] args)
{
int marks = 65;
if (marks >= 80)
{
Console.WriteLine("Student has passed with higher first class");
}
else if (marks >= 60)
{
Console.WriteLine("Student has passed with first class");
}
else if (marks >= 40)
{
Console.WriteLine("Student has passed with second class");
}
else
{
Console.WriteLine("Student has failed");
}
Console.ReadLine();
}
}
}
登录后复制

代码说明:上面的例子中,根据获得的分数使用了多个else if语句。

输出:

C# 中的 Else If

结论

当我们只想在某个条件为真时执行一段代码时,或者当我们想要执行某些步骤取决于某些要求时,则需要这些条件决策。条件语句在 C Sharp 中用于决策。

以上是C# 中的 Else If的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!