在C#中,我们知道如果条件为true,则执行if语句,否则它将不会执行。但是,如果条件为假,我们想打印/执行某些操作,该怎么办。这里谈到的else语句。如果给定条件为假,则将其他语句与if语句一起使用以执行一些代码块。换句话说,在if-else语句中,如果给定条件的值为true,则执行if条件,或者如果给定条件的值为false,则执行else条件。
- 其他语句可以包含一个或多个 大括号{}中的语句。如果else语句仅包含 单个语句,则花括号是可选的。
- 其他语句的语句可以是任何类型/类型的像它可能包含另一个if-else语句。
句法:
if(condition)
{
// code if condition is true
}
else
{
// code if condition is false
}
流程图:
范例1:
C#
// C# program to demonstrate
// if-else statement
using System;
class GFG{
static public void Main()
{
// Declaring and initializing variables
string x = "Geeks";
string y = "GeeksforGeeks";
// If-else statement
if (x == y)
{
Console.WriteLine("Both strings are equal..!!");
}
// else statement
else
{
Console.WriteLine("Both strings are not equal..!!");
}
}
}
C#
// C# program to demonstrate if-else statement
using System;
class GFG{
static public void Main()
{
// Declaring and initializing variables
int x = 10;
int y = 100;
// If-else statement
if (x >= y)
{
Console.WriteLine("x is greater than y");
}
// else statement
else
{
Console.WriteLine("x is not greater than y");
}
}
}
C#
// C# program to demonstrate short-hand
// of if-else statement
using System;
class GFG{
static public void Main()
{
// Declaring and initializing variables
string x = "Geeks";
string y = "GeeksforGeeks";
// Short-hand if-else statement
string result = (x == y) ? "Both strings are equal" : "Not equal";
// Display result
Console.WriteLine(result);
}
}
C#
// C# program to demonstrate nested
// Ternary Operator
using System;
class GFG{
static void Main(string[] args)
{
int a = 23, b = 90;
string result = a > b ? "a is greater than b" :
a < b ? "a is less than b" :
a == b ? "a is equal to b" : "Invalid";
Console.WriteLine(result);
}
}
输出:
Both strings are not equal..!!
范例2:
C#
// C# program to demonstrate if-else statement
using System;
class GFG{
static public void Main()
{
// Declaring and initializing variables
int x = 10;
int y = 100;
// If-else statement
if (x >= y)
{
Console.WriteLine("x is greater than y");
}
// else statement
else
{
Console.WriteLine("x is not greater than y");
}
}
}
输出:
x is not greater than y
if-else语句的简写
C#还提供了if-else语句的简便实现,该语句也称为三元运算符(?:),因为它包含三个操作数。它基本上用于用单行替换多行代码。根据布尔表达式的值,它将返回两个值之一。
句法:
variable_name = (condition) ? TrueExpression : FalseExpression;
在这里,如果给定条件为true,则将执行TrueExpression语句。或者,如果给定条件为false,则将执行FalseExpression语句。
注意: C#还支持嵌套三元运算符。
范例1:
C#
// C# program to demonstrate short-hand
// of if-else statement
using System;
class GFG{
static public void Main()
{
// Declaring and initializing variables
string x = "Geeks";
string y = "GeeksforGeeks";
// Short-hand if-else statement
string result = (x == y) ? "Both strings are equal" : "Not equal";
// Display result
Console.WriteLine(result);
}
}
输出:
Not equal
范例2:
C#
// C# program to demonstrate nested
// Ternary Operator
using System;
class GFG{
static void Main(string[] args)
{
int a = 23, b = 90;
string result = a > b ? "a is greater than b" :
a < b ? "a is less than b" :
a == b ? "a is equal to b" : "Invalid";
Console.WriteLine(result);
}
}
输出:
a is less than b