C#_分支—if语句

使用if语句可以有条件的执行某段代码

if的语法

1
2
3
if(<test>)
code executed if <test> is true;// 如果只有一行代码也可以不需要{}这个括号,有多行代码就要加上{}
先执行if括号里的test,如果满足条件,则执行if的下一行代码

实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _19
{
class Program
{
static void Main(string[] args)
{
bool var = true;
if (var)//如果满足条件,则执行下面这段代码
Console.WriteLine("-------");//执行这段

Console.WriteLine("if语句后面的语句");
Console.ReadKey();

}
}
}