C#中的if语句

来源:互联网 发布:长歌正太捏脸数据 编辑:程序博客网 时间:2024/05/21 09:43

if语句的使用方法:

  1. if (condition)   *这里的condition可以是一个bool类型的值,也可以是最终值为bool类型的表达式。
    statements ;    *statements是如果condition为真是需要执行的语句,如果不为真则跳过这段语句向后执行。
  2. statements 如果是一条语句则不需要在statements的起始位置添加一对"{}",如果statements为多条语句则必须使用"{}"将这段语句括起来。
eg: IfDemo.cs
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();}}}



原创粉丝点击