if语句的使用方法:
- if (condition) *这里的condition可以是一个bool类型的值,也可以是最终值为bool类型的表达式。
statements ; *statements是如果condition为真是需要执行的语句,如果不为真则跳过这段语句向后执行。 - statements 如果是一条语句则不需要在statements的起始位置添加一对"{}",如果statements为多条语句则必须使用"{}"将这段语句括起来。
using System;
namespace IfDemo
{
class Program
{
static void Main()
{
Console.WriteLine("Please input a string:");
string input = Console.ReadLine();
if(input == "")
{
Console.WriteLine("You typed in an empty string!");
return ;
} else if(input.Length < 10) //这里的 else if 可以根据需要添加多个
{
Console.WriteLine("The string had less than 10 characters.");
} else if(input.Length < 10)
{
Console.WriteLine("The string had at least 10 but less than 20 characters.");
}else { //这里的else 只能有一个
Console.WriteLine("The string had more than 20 characters.");
}
Console.WriteLine("The string was {0}",input);
Console.ReadKey();
}
}
}