C# if...else statement

C# conditionals

aif statementcan be followed by an optionalelse statementThe else statement executes when the boolean expression is false.

Syntax

In C#if...elseThe syntax of the statement:

if(boolean_expression)
{
   /* 如果布尔表达式为真将执行的语句 */
}
else
{
  /* 如果布尔表达式为假将执行的语句 */
}

If the boolean expression istrue, then executeifcode within the block. If the boolean expression isfalse, then executeelseCode within the block.

Flowchart

C# 中的 if...else 语句

Example

Example

using System;

namespace DecisionMaking
{
   
    class Program
    {
        static void Main(string[] args)
        {

            /* local variable definition */
            int a = 100;

            /* Check boolean condition */
            if (a < 20)
            {
                /* If the condition is true, output the following statement */
                Console.WriteLine("a is less than 20");
            }
            else
            {
                /* If the condition is false, output the following statement */
                Console.WriteLine("a is greater than 20");
            }
            Console.WriteLine("The value of a is {0}", a);
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

a 大于 20
a 的值是 100

if...else if...else statement

aifthe statement can be followed by an optionalelse if...elsestatement, which can be used to test multiple conditions.

When using if...else if...else statements, the following points need to be noted:

  • An if can be followed by zero or one else, and it must come after any else if.
  • An if can be followed by zero or more else ifs, and they must come before the else.
  • Once an else if matches successfully, the other else ifs or else will not be tested.

Syntax

In C#if...else if...elseThe syntax of the statement:

if(boolean_expression 1)
{
   /* 当布尔表达式 1 为真时执行 */
}
else if( boolean_expression 2)
{
   /* 当布尔表达式 2 为真时执行 */
}
else if( boolean_expression 3)
{
   /* 当布尔表达式 3 为真时执行 */
}
else 
{
   /* 当上面条件都不为真时执行 */
}

Example

Example

using System;

namespace DecisionMaking
{
   
    class Program
    {
        static void Main(string[] args)
        {

            /* local variable definition */
            int a = 100;

            /* Check boolean condition */
            if (a == 10)
            {
                /* If the if condition is true, output the following statement */
                Console.WriteLine("The value of a is 10");
            }
            else if (a == 20)
            {
                /* If the else if condition is true, output the following statement */
                Console.WriteLine("The value of a is 20");
            }
            else if (a == 30)
            {
                /* If the else if condition is true, output the following statement */
                Console.WriteLine("The value of a is 30");
            }
            else
            {
                /* If none of the above conditions is true, output the following statement */
                Console.WriteLine("No matching value");
            }
            Console.WriteLine("The exact value of a is {0}", a);
            Console.ReadLine();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

没有匹配的值
a 的准确值是 100

C# conditionals

other extensions