📜  C#– if语句

📅  最后修改于: 2021-05-29 18:20:18             🧑  作者: Mango

在C#中,if语句用于指示将根据给定布尔表达式的值执行哪个语句。如果布尔表达式的值为true,则if语句将执行给定的then语句,否则它将控制权返回给if语句之后的下一个语句。

  • 在C#中,您可以根据需要将if语句与then语句或else语句一起使用。
  • 然后,语句包含一个或多个语句,这些语句用花括号({})括起来。对于单个语句,花括号可以是可选的,如果大括号{}不与if语句一起使用,则仅将其旁边的语句视为与if语句相关联。
  • then语句可以是任何类型/类型,例如可以包含另一个if-else语句

句法:

if(condition)
{
    then statement;
}

流程图:

C#-如果声明

让我们在给定示例的帮助下讨论if语句:

范例1:

C#
// C# program to demonstrate
// if statement
using System;
  
class GFG{
    
static public void Main()
{
      
    // Declaring and initializing variables
    string x = "GeeksforGeeks";
    string y = "GeeksforGeeks";
      
    // If statement
    if (x == y)
    {
        Console.WriteLine("Both strings are equal..!!");
    }
      
    // If statement
    if (x != y)
    {
         Console.WriteLine("Both strings are not equal..!!");
    }
}
}


C#
// C# program to demonstrate nested
// if statement 
using System;
  
class GFG{
static public void Main()
{
      
    // Declaring and initializing variables
    string emp_name = "Rohit";
    int salary = 10000;
      
    // If statement
    // Here if condition checks 
    // the employee name is equal to rohit
    if (emp_name == "Rohit")
    {
          
        // Nested if statement
        // Here if the salary of Rohit 
        // is greater than 50000 then 
        // he is eligible to pay tax
        // Otherwise not eligible
        if (salary > 50000)
        {
            Console.WriteLine("Eligible to pay tax");
        }
        else
        {
            Console.WriteLine("Not Eligible");
        }
    }
}
}


C#
// C# program to illustrate if statement 
// Using AND, OR, NOT operators
using System;
  
class GFG{
  
static public void Main()
{
      
    // Declaring and initializing variables
    int x1 = 15;
    int x2 = 18;
    int x3 = 20;
      
    // If statement
    // Using AND operator
    if (x1 > 20 && x2 > 20)
    {
        Console.WriteLine("Enter group A");
    }
      
    // If statement
    // Using OR operator
    if (x1 < 30 || x3 == 20)
    {
        Console.WriteLine("Enter group B");
    }
      
    // If statement
    // Using NOT operator
    if (!(x1 > 20 && x2 > 20))
    {
        Console.WriteLine("Enter group C");
    }
}
}


输出:

Both strings are equal..!!

说明:在上面的示例中,我们有两个字符串,即x和y。现在,在第一,if语句,我们字符串x比较字符串y和比较的结果是真。因此,然后执行then块并输出“两个字符串都相等.. !! ”。现在在第二个if语句中,我们检查两个字符串不相等,但是字符串相等,因此,此if语句的then块将不执行。

范例2:

C#

// C# program to demonstrate nested
// if statement 
using System;
  
class GFG{
static public void Main()
{
      
    // Declaring and initializing variables
    string emp_name = "Rohit";
    int salary = 10000;
      
    // If statement
    // Here if condition checks 
    // the employee name is equal to rohit
    if (emp_name == "Rohit")
    {
          
        // Nested if statement
        // Here if the salary of Rohit 
        // is greater than 50000 then 
        // he is eligible to pay tax
        // Otherwise not eligible
        if (salary > 50000)
        {
            Console.WriteLine("Eligible to pay tax");
        }
        else
        {
            Console.WriteLine("Not Eligible");
        }
    }
}
}

输出:

Not Eligible

范例3:

C#

// C# program to illustrate if statement 
// Using AND, OR, NOT operators
using System;
  
class GFG{
  
static public void Main()
{
      
    // Declaring and initializing variables
    int x1 = 15;
    int x2 = 18;
    int x3 = 20;
      
    // If statement
    // Using AND operator
    if (x1 > 20 && x2 > 20)
    {
        Console.WriteLine("Enter group A");
    }
      
    // If statement
    // Using OR operator
    if (x1 < 30 || x3 == 20)
    {
        Console.WriteLine("Enter group B");
    }
      
    // If statement
    // Using NOT operator
    if (!(x1 > 20 && x2 > 20))
    {
        Console.WriteLine("Enter group C");
    }
}
}

输出:

Enter group B
Enter group C