C# if statement
aif statementConsists of a boolean expression followed by one or more statements.
Syntax
In C#ifSyntax of the statement:
if(boolean_expression)
{
/* 如果布尔表达式为真将执行的语句 */
}
If the Boolean expression istrue, then the code block within the if statement will be executed. If the boolean expression isfalse, then the first set of code after the if statement ends (after the closing brace) will be 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();
}
}
}
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 above code is compiled and executed, it will produce the following result:
a 小于 20 a 的值是 10other extensions