Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C# if Statement

C# if Statement

C# Decision C# Decision Making

A if statement if statement consists of a Boolean expression followed by one or more statements.

Syntax

In C#, if statement syntax:



if(boolean_expression)

{

   /* statement to execute if the boolean expression is true */

}

If the Boolean expression is true, then the code block inside the if statement is executed. If the Boolean expression is false, then the first set of code after the if statement ends (after the closing brace) is executed.

Flowchart

Example

Example

using

System

;

namespace

DecisionMaking

{

   

   

class

Program

   

{

       

static

void

Main

(

string

[

]

args

)

       

{

           

/* Local variable definition */

           

int

a

=

10

;

           

/* Use an if statement to check a boolean condition */

           

if

(

a

<

20

)

{

/* If the condition is true, output the following statement */

                Console

.

WriteLine

(

"a is less than 20"

)

;

           

}

            Console

.

WriteLine

(

"The value of a is {0}"

, a

)

;

            Console

.

ReadLine

(

)

;

       

}

   

}

}

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



a 小于 20

a 的值是 10

C# Decision C# Decision Making