在生产环境中测试,无水印。
随时随地满足您的需求。
获得30天的全功能产品。
几分钟内就能启动并运行。
在您的产品试用期间,全面访问我们的支持工程团队。
在本教程中,我们将解析if
和else
语句的概念,以及如何在您的C#程序中有效地使用它们。 我们还将探讨布尔表达式和条件运算符等相关概念。 那么,让我们直接进入正题吧!
if
语句是编程中的基本概念。 它用于根据特定条件在代码中做出决定。 C# 中 if 语句的基本语法如下:
if (Boolean expression)
{
// Statements to execute if the Boolean expression is true
}
if (Boolean expression)
{
// Statements to execute if the Boolean expression is true
}
If Boolean expression Then
' Statements to execute if the Boolean expression is true
End If
if 语句检查给定的布尔表达式是否计算为true
。 如果是这样,则执行语句块内的代码(大括号内的代码)。 如果布尔表达式的计算结果为false
,则语句块内的代码将被跳过。
现在,如果您想在 if 条件为假时执行其他代码,该怎么办? 这就是可选的 else 语句发挥作用的地方。 C# 中 if-else
语句的语法如下:
if (Boolean expression)
{
// Statements to execute if the Boolean expression is true
}
else
{
// Statements to execute if the Boolean expression is false
}
if (Boolean expression)
{
// Statements to execute if the Boolean expression is true
}
else
{
// Statements to execute if the Boolean expression is false
}
If Boolean expression Then
' Statements to execute if the Boolean expression is true
Else
' Statements to execute if the Boolean expression is false
End If
在上述情况下,如果布尔表达式的计算结果为true
,则执行if块中的代码。 如果计算结果为false
,则执行else块中的代码。
让我们来看一个使用 C# if-else 语句的真实例子。 想象一下,您正在编写一个检查某人是否有投票资格的程序。 在大多数国家,投票年龄为 18 岁。
下面的示例演示了如何使用 if-else 语句来确定投票资格:
using System;
class Program
{
static void Main(string [] args)
{
int age = 21;
if (age >= 18)
{
Console.WriteLine("You are eligible to vote!");
}
else
{
Console.WriteLine("Sorry, you are not eligible to vote.");
}
}
}
using System;
class Program
{
static void Main(string [] args)
{
int age = 21;
if (age >= 18)
{
Console.WriteLine("You are eligible to vote!");
}
else
{
Console.WriteLine("Sorry, you are not eligible to vote.");
}
}
}
Imports System
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim age As Integer = 21
If age >= 18 Then
Console.WriteLine("You are eligible to vote!")
Else
Console.WriteLine("Sorry, you are not eligible to vote.")
End If
End Sub
End Class
在上述代码中,我们首先声明一个名为age
的整数变量,并将其赋值为21。然后,我们使用if-else语句检查年龄是否大于或等于18。如果条件为真,程序会在控制台上打印“你有资格投票!” 如果是假的,则打印 "对不起,您没有投票资格"。
在 C# 中,您可以使用各种类型的布尔表达式来创建更复杂的条件。 一些常用的条件运算符包括
==
:等于!=
:不等式<
:小于>
: 大于<=
:小于或等于>=
:大于或等于
让我们来看一个例子。 假设您想编写一个程序,检查一个数字是正数、负数还是零。 下面的代码片段使用 if 语句和条件运算符来实现这一目的:
using System;
class Program
{
static void Main(string [] args)
{
int number = 0;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}
}
}
using System;
class Program
{
static void Main(string [] args)
{
int number = 0;
if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}
}
}
Imports System
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim number As Integer = 0
If number > 0 Then
Console.WriteLine("The number is positive.")
ElseIf number < 0 Then
Console.WriteLine("The number is negative.")
Else
Console.WriteLine("The number is zero.")
End If
End Sub
End Class
在上面的例子中,我们首先声明一个名为number
的整数变量,并将其赋值为0。然后我们使用if
语句检查这个数字是否大于0。对于为真的情况,我们打印“这个数字是正数”。对于为假的情况,我们继续执行else if
语句,检查这个数字是否小于0。如果这个条件为真,我们打印“这个数字是负数”。最后,如果之前的条件都不满足,我们进入else
块,打印“这个数字是零”。
在某些情况下,您可能需要同时检查多个条件。 C# 提供的逻辑运算符可以帮助您实现这一目标。 最常用的逻辑运算符有
&&
:逻辑与**`
**:逻辑或
!
:逻辑非
让我们来看一个在 if 语句中使用逻辑运算符的例子。 想象一下,您正在编写一个程序,以确定某人是否有资格享受商店的特别折扣。 折扣适用于年龄在65岁或以上的老年人或年龄在18岁至25岁之间的学生。 下面的代码片段演示了如何使用带有逻辑运算符的 C# if-else 语句来确定折扣资格:
using System;
class Program
{
static void Main(string [] args)
{
int age = 23;
bool isStudent = true;
if ((age >= 65)
(isStudent && (age >= 18 && age <= 25)))
{
Console.WriteLine("You are eligible for the discount!");
}
else
{
Console.WriteLine("Sorry, you are not eligible for the discount.");
}
}
}
using System;
class Program
{
static void Main(string [] args)
{
int age = 23;
bool isStudent = true;
if ((age >= 65)
(isStudent && (age >= 18 && age <= 25)))
{
Console.WriteLine("You are eligible for the discount!");
}
else
{
Console.WriteLine("Sorry, you are not eligible for the discount.");
}
}
}
Imports System
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim age As Integer = 23
Dim isStudent As Boolean = True
If (age >= 65)(isStudent AndAlso (age >= 18 AndAlso age <= 25)) Then
Console.WriteLine("You are eligible for the discount!")
Else
Console.WriteLine("Sorry, you are not eligible for the discount.")
End If
End Sub
End Class
在上述代码中,我们首先声明一个名为age
的整数变量和一个名为isStudent
的布尔变量。 然后,我们使用带有逻辑运算符的 if-else 语句来检查该人是否符合折扣条件。 如果年龄为65岁或以上,或者如果该人是18至25岁的学生,程序将打印“您有资格享受折扣!” 否则,它将打印“抱歉,您没有资格享受折扣。”
既然您已经牢牢掌握了 C# 的 if-else 语句,让我们探讨一个涉及 IronPDF 库 的实际应用,它允许您在 C# 应用程序中无缝处理 PDF 文件。
IronPDF 是一个功能强大的 .NET 库,允许您在 C# 应用程序中创建、编辑和提取 PDF 文件内容。
在本示例中,我们将创建一个简单的 PDF 发票生成器,根据客户所在地适用不同的税率。 这一场景为使用 if-else 语句提供了绝佳的机会。
首先,通过 NuGet 安装 IronPDF,运行以下命令:
Install-Package IronPdf
接下来,让我们创建一个简单的程序,为不同地区的客户生成不同税率的发票:
using System;
using IronPdf;
class Program
{
static void Main(string [] args)
{
string customerLocation = "Europe";
double taxRate;
if (customerLocation == "USA")
{
taxRate = 0.07;
}
else if (customerLocation == "Europe")
{
taxRate = 0.20;
}
else
{
taxRate = 0.15;
}
double productPrice = 100.0;
double totalTax = productPrice * taxRate;
double totalPrice = productPrice + totalTax;
string invoiceContent = $@"
<h1>Invoice</h1>
<p>Product Price: ${productPrice}</p>
<p>Tax Rate: {taxRate * 100}%</p>
<p>Total Tax: ${totalTax}</p>
<p>Total Price: ${totalPrice}</p>
";
var pdf = new ChromePdfRenderer();
var document = pdf.RenderHtmlAsPdf(invoiceContent);
document.SaveAs("Invoice.pdf");
}
}
using System;
using IronPdf;
class Program
{
static void Main(string [] args)
{
string customerLocation = "Europe";
double taxRate;
if (customerLocation == "USA")
{
taxRate = 0.07;
}
else if (customerLocation == "Europe")
{
taxRate = 0.20;
}
else
{
taxRate = 0.15;
}
double productPrice = 100.0;
double totalTax = productPrice * taxRate;
double totalPrice = productPrice + totalTax;
string invoiceContent = $@"
<h1>Invoice</h1>
<p>Product Price: ${productPrice}</p>
<p>Tax Rate: {taxRate * 100}%</p>
<p>Total Tax: ${totalTax}</p>
<p>Total Price: ${totalPrice}</p>
";
var pdf = new ChromePdfRenderer();
var document = pdf.RenderHtmlAsPdf(invoiceContent);
document.SaveAs("Invoice.pdf");
}
}
Imports System
Imports IronPdf
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim customerLocation As String = "Europe"
Dim taxRate As Double
If customerLocation = "USA" Then
taxRate = 0.07
ElseIf customerLocation = "Europe" Then
taxRate = 0.20
Else
taxRate = 0.15
End If
Dim productPrice As Double = 100.0
Dim totalTax As Double = productPrice * taxRate
Dim totalPrice As Double = productPrice + totalTax
Dim invoiceContent As String = $"
<h1>Invoice</h1>
<p>Product Price: ${productPrice}</p>
<p>Tax Rate: {taxRate * 100}%</p>
<p>Total Tax: ${totalTax}</p>
<p>Total Price: ${totalPrice}</p>
"
Dim pdf = New ChromePdfRenderer()
Dim document = pdf.RenderHtmlAsPdf(invoiceContent)
document.SaveAs("Invoice.pdf")
End Sub
End Class
在本代码示例中,我们使用 if-else 语句根据客户所在地确定适当的税率。 我们使用IronPDF从HTML字符串创建PDF发票。 在C#中,我们可以利用C#列表来存储和操作项目,例如产品价格。
在整个教程中,我们介绍了 C# if-else 语句的基础知识,探讨了各种条件运算符和逻辑运算符,并研究了现实生活中的示例,以便更好地理解这一概念。 我们甚至展示了一个使用强大IronPDF库的实际应用,该库提供免费试用和许可选项。
请记住,在掌握编程概念时,练习至关重要。不断尝试不同的场景,应用新发现的 if-else 语句知识和其他相关概念。