比较运算符基本上用于比较两个操作数。
比较运算符基于比较返回真或者假值。
比较运算符使用某些条件语句和循环构造,例如循环,if ..else。
运算符 | 名称 | 示例 |
---|---|---|
< | 小于 | x < 10 |
> | 大于 | x > 10 |
<= | 小于等于 | x <= 10 |
>= | 大于等于 | x >= 10 |
== | 等于 | x == 10 |
!= | 不等于 | x != 10 |
C#比较运算符例子
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Comparison_Operator { class Program { static void Main(string[] args) { int num1, num2; //Accepting two inputs from the user Console.Write("Enter first number\t"); num1 = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter second number\t"); num2 = Convert.ToInt32(Console.ReadLine()); //Processing comparison //Check whether num1 is greater than or not if (num1 > num2) { Console.WriteLine("{0} is greater than {1}", num1, num2); } //Check whether num2 is greater than or not else if (num2 > num1) { Console.WriteLine("{0} is greater than {1}", num2, num1); } else { Console.WriteLine("{0} and {1} are equal", num1, num2); } Console.ReadLine(); } } }
日期:2020-04-11 22:50:17 来源:oir作者:oir