C# if语句

if语句由一个布尔表达式,后跟一个或多个语句组成。

语法

C# 中if语句的语法是:

if(boolean_expression)
{
   /* statement(s) will execute if the boolean expression is true */
}

如果布尔表达式(boolean_expression)求值计算为true,则执行if语句中的代码块。 如果布尔表达式(boolean_expression)求值计算结果为false,则执行if语句结束后的第一组代码(在闭合大括号之后)。

流程图

例子

using System;
namespace DecisionMaking
{
   class Program
   {
      static void Main(string[] args)
      {
         /* local variable definition */
         int a = 1;
         /* check the boolean condition using if statement */
         if (a < 10)
         {
            /* if condition is true then print the following */
            Console.WriteLine("a is less than 10");
         }
         Console.WriteLine("value of a is : {0}", a);
         Console.ReadLine();
      }
   }
}

当编译和执行上述代码时,会产生以下结果:

a is less than 10;
value of a is : 1

上一篇: C#决策结构 下一篇: C#循环